CITS2002 Systems Programming  
prev
next CITS2002 CITS2002 schedule  

Using our stack in a Reverse Polish Calculator, continued

Careful readers may have noticed that in some cases we don't actually need the integer variables val1 and val2.

We can use the 2 results returned from pop_item as arguments to push_item:


int evaluate_RPN(FILE *fp)
{
    char  line[BUFSIZ];

    while( fgets(line, sizeof line, fp) != NULL ) {
        if(line[0] == '#')
            continue;
        if(isdigit(line[0]) || line[0] == '-')
            push_item( atoi(line) );

        else if(line[0] == 'a') {
            push_item( pop_item() + pop_item() );
        }
        ....
         
    }
    return pop_item();
}

int main(int argc, char *argv[])
{
    printf("%i\n", evaluate_RPN( stdin ) );
    return 0;
}

hp65

 


CITS2002 Systems Programming, Lecture 19, p7, 3rd October 2023.