K和R反向波兰语符号 [英] K and R Reverse Polish Notation

查看:315
本文介绍了K和R反向波兰语符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法弄清楚函数的调用方式.

输入1 2 3 + + [Enter]//注意输入之间有空格

输出6//这是正确的

1->当程序编译while语句调用函数getop时.

2->在getop()函数中,它将调用getch()函数,然后依次调用getchar(),因此在此步骤中,它将读取1作为输入并将其返回.

3->现在,它会检查c是否为数字,是否为真,因此它将再次调用getch()来读取空间,返回其值,现在它会检查其是否为数字,并且其值被评估为false,然后它移到下一条语句.

4->最后将执行ungetch(),将1保存在其缓冲区中

在这一步上,我无法弄清楚如何读取输入以及getch和ungetch的用途

#define MAXOP 100
#define NUMBER '0'

int getop(char[]);
void push(double);
double pop(void);

 main()
 {
      int type;
      double op2;
      char s[MAXOP];
      while((type=getop(s))
      {
          switch(type)
          {
            //Here all operation are performed as push pop addition etc.
            //This part of code is simple
          }
      }

对推入和弹出功能的定义很容易,所以我没有写

#include<ctype.h>
int getch(void);
void ungetch(int);

int getop(char s[]) {
     int i,c;
     while((s[0]=c=getch())==' '||c=='\t');
     s[1]='\0';
     if(!isdigit(c)&&c!='.')
          return c;
     i=0;
     if(isdigit(c))
         while(isdigit(s[++i]=c=getch()));
     if(c=='.')
         while(isdigit(s[++i]=c=getch()));

     s[i]='\0';
     if(c!=EOF)
         ungetch(c);
     return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];
int bufp=0;

int getch(void) {
    return (bufp>0)?buf[--bufp]:getchar();
}

void ungetch(int c) {
    if(bufp>=BUFSIZE)
        printf("ungetch:too many character\n");
    else
        buf[bufp++]=c;
}

解决方案

4->最后执行ungetch(),将1保存在其缓冲区中

否,对ungetch的调用传递了c,该点此时包含空格' '.因此getop返回NUMBERs"1",并且未处理的输入是buf中的" "(或者更确切地说是buf = { ' ', ... }bufp = 1)和stdin中的"2 3 + +\n"./p>


ungetch将字符添加到buf. getch如果不为空,则从buf中删除并返回字符;如果buf为空,则直接从stdin读取(通过getchar).

这两个功能的目的是能够未读"字符,即能够确定在读取字符后您实际上还不想处理该字符,因此您将其放回原位(返回)下次您阅读输入内容时).这使您可以在输入中向前看".

例如,当读取类似"42+..."的输入时,首先需要提取数字42.为此,您首先要读取字符'4'.但是您不能在此停下来,因为在第一个数字之后,可能还会有更多的数字.因此,您读了下一个字符'2',这很好,因为它也是一个数字.但是然后您按下+,它不是数字,也不是数字的一部分.因此,此时您停止处理输入,因为您知道完整的数字为42,但是您需要了解刚刚阅读的+.您需要保留它,以便下一个输入操作可以返回它(否则,我们将以静默方式删除它,这会使用户感到非常困惑).因此,您呼叫ungetch('+')并继续处理42,知道下一个getch()将接您刚刚放回的+.


我无法告诉您实际的计算是如何完成的,因为您没有向我们展示该代码,但是根据您的说法,这部分代码很简单".

Unable to figure out how function are getting called.

Input 1 2 3 + + [Enter] //Note there is a space between input

Output 6 //which is correct

1 -> when program compile while statement calls function getop(s).

2 -> In getop() function it will call getch() function which in turn call getchar() so at this step it will read 1 as input and return it.

3 -> Now it checks if c is digit or not which is true so it will again call getch() which read space,return its values,now it checks if it is digit or not which is evaluated as false,then it moves to next statement.

4 -> at last ungetch() will be executed which save 1 in its buffer

At this step I am unable to figure it out how input is getting read and what is the use of getch and ungetch

#define MAXOP 100
#define NUMBER '0'

int getop(char[]);
void push(double);
double pop(void);

 main()
 {
      int type;
      double op2;
      char s[MAXOP];
      while((type=getop(s))
      {
          switch(type)
          {
            //Here all operation are performed as push pop addition etc.
            //This part of code is simple
          }
      }

definition for push and pop function is easy so I am not writing it

#include<ctype.h>
int getch(void);
void ungetch(int);

int getop(char s[]) {
     int i,c;
     while((s[0]=c=getch())==' '||c=='\t');
     s[1]='\0';
     if(!isdigit(c)&&c!='.')
          return c;
     i=0;
     if(isdigit(c))
         while(isdigit(s[++i]=c=getch()));
     if(c=='.')
         while(isdigit(s[++i]=c=getch()));

     s[i]='\0';
     if(c!=EOF)
         ungetch(c);
     return NUMBER;
}

#define BUFSIZE 100

char buf[BUFSIZE];
int bufp=0;

int getch(void) {
    return (bufp>0)?buf[--bufp]:getchar();
}

void ungetch(int c) {
    if(bufp>=BUFSIZE)
        printf("ungetch:too many character\n");
    else
        buf[bufp++]=c;
}

解决方案

4 -> at last ungetch() will be executed which save 1 in its buffer

No, the call to ungetch passes c, which at this point contains a space, ' '. So getop returns NUMBER, s is "1", and the unprocessed input is " " in buf (or rather buf = { ' ', ... }, bufp = 1) and "2 3 + +\n" in stdin.


ungetch adds characters to buf. getch removes and returns characters from buf if it's not empty; if buf is empty, it reads straight from stdin (via getchar).

The purpose of these two functions is to be able to "unread" characters, i.e. to be able to decide that after reading a character you don't actually want to process it yet, so you put it back (to be returned the next time you read input). This allows you to "peek ahead" in the input.

For example, when reading input like "42+..." you first need to extract the number 42. To do that, you first read the character '4'. But you can't stop there because after the first digit, there could be more digits following. So you read the next character, '2', which is good, because it's also a digit. But then you hit +, which is not a digit and not part of the number. So at this point you stop processing input because you know the complete number is 42, but you need to something about the + you just read. You need to preserve it so the next input operation can return it (otherwise we'd just silently drop it, which would be very confusing to the user). So you call ungetch('+') and continue processing 42, knowing that the next getch() will pick up the + you just put back.


I can't tell you how the actual calculation is done because you didn't show us that code, but according to you "this part of code is simple".

这篇关于K和R反向波兰语符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆