Home > Net >  operator priority problem about Reverse Polish Notation Calculator
operator priority problem about Reverse Polish Notation Calculator

Time:04-14

#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.

  • Related