'operator priority problem about Reverse Polish Notation Calculator
#include <stdio.h>
#include <stdlib.h> /* for atof() */
#define MAXOP 100 /* max size of operand or operator */
#define NUMBER '0' /* signal that a number was found */
int getop(char []);
void push(double);
double pop(void);
/* reverse Polish calculator */
main()
{
int type;
double op2;
char s[MAXOP];
while ((type = getop(s)) != EOF) {
switch (type) {
case NUMBER:
push(atof(s));
break;
case '+':
push(pop() + pop());
break;
case '*':
push(pop() * pop());
break;
case '-':
op2 = pop();
push(pop() - op2);
break;
case '/':
op2 = pop();
if (op2 != 0.0)
push(pop() / op2);
else
printf("error: zero divisor\n");
break;
case '\n':
printf("\t%.8g\n", pop());
break;
default:
printf("error: unknown command %s\n", s);
break;
}
}
}
#define MAXVAL 100
int sp = 0;
double val[MAXVAL];
void push(double f)
{
if(sp < MAXVAL)
val[sp++]=f;
else
printf("error:stack full, cant push %g\n",f);
}
double pop(void)
{
if(sp>0)
return val[--sp];
else
{
printf("error: stack empty\n");
return 0.0;
}
}
#include<ctype.h>
int getch(void);
void ungetch(int);
int getop(char *s)
{
char c;
while ((*s = c = getch()) == ' ' || c == '\t')
;
*(s + 1) = '\0';
if (!isdigit(c) && c != '.')
return c;
if (isdigit(c))
{
while (isdigit(*s++ = c = getch()))
;
}
if (c == '.')
{
while (isdigit(*s++ = c = getch()))
;
}
*s = '\0';
if (c != EOF)
ungetch(c);
return number;
}
char buf[30];
char *Bufp = buf;
int getch(void)
{
return (Bufp > buf) ? *(--Bufp) : getchar();
}
void ungetch(int c)
{
if (c != EOF)
*Bufp++ = c;
else
printf("no space\n”);
}
the result as follows. enter image description here
when i changed this section.
int getch(void);
void ungetch(int);
int getop(char *s)
{
char c;
while ((*s = c = getch()) == ' ' || c == '\t')
;
*(s + 1) = '\0';
if (!isdigit(c) && c != '.')
return c;
if (isdigit(c))
{
while (isdigit(*++s = c = getch()))
;
}
if (c == '.')
{
while (isdigit(*++s = c = getch()))
;
}
*s = '\0';
if (c != EOF)
ungetch(c);
return number;
}
the result was correct. the result was as followenter image description here
So i know there must be difference between
while (isdigit(*++s = c = getch()))
and
while (isdigit(* s++ = c = getch()))
this problem must be relative to the operator priority. But I still didn't understand the reason why it happened. would you like to help me? or could you tell me the similar question? because i have been in search of it for a long time.😭
——————————————————————The above problem was already solved.The new question was as follows.
at first,Thank you guys for helping me and giving me new direction about above problem. But i still had question. For
while (isdigit(*++s = c = getch()))
i use better understandable way to achieve the same effect
while (c = getch())
{
++s;
if (isdigit(*s = c))
;
else
break;
}
The same way for
while (isdigit(* s++ = c = getch()))
the better understandable codes
while (c = getch())
{
if (isdigit(*s = c))
{
s++;
}
else
break;
}
the thing i didn't understand is * s++ = c which is equal to * (s++) = c. why isn't the statement same as the following codes?
s++;
*s = c;
the operator++'s precedence is higher than * and=, right?
Solution 1:[1]
oh, i got it. A operator which has higher precedence doesn't necessarily mean it happens first. especially, for post increment, it doesn't happen until variable has effective value(or the value has been used).
So, first we get a character for getch(),and then assign it to c variable. next, for* s++ = c, although it is equal to * (s++) = c, increment doesn't happen until s pointer variable have its effective value. execute the statement *s = c.
finally, if *s is a digit, do the s++.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | neil guo |
