问题
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;
}
回答1:
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".
来源:https://stackoverflow.com/questions/31630268/k-and-r-reverse-polish-notation