Question

You are to write a program that implements a Reverse Polish Notation Calculator in C using...

You are to write a program that implements a Reverse Polish Notation Calculator in C using BISON and FLEX, You only have to edit the BISON and FLEX files.

Link to the files to start and have a general view of the program:

https://www.dropbox.com/sh/83yzs66jhftqj5b/AABZcY9Qwl84JdUFnYpQaZk9a?dl=0

Reverse Polish Notation is a mathematical notation in which every operator follows all of its operands. It is sometimes called postfix notation, and does not require any parentheses as long as each operator has a fixed number of operands.For example, this mathematical notation adds the numbers 5 and 6 together:

5 6 +

From Wikipedia:

In reverse Polish notation the operators follow their operands; for instance, to add 3 and 4, one would write "3 4 +" rather than "3 + 4". If there are multiple operations, the operator is given immediately after its second operand; so the expression written "3 − 4 + 5" in conventional notation would be written "3 4 − 5 +" in RPN: 4 is first subtracted from 3, then 5 added to it.

the algorithm to implement this type of calculator is as follows:

    while there are input tokens left:
        read the next token
        if the token is a value:
            push it onto the stack
        else ( assume the token is an operator ):
            it is already known that the operator takes N arguments
            if there are fewer than N values on the stack:
                ERROR: The user has not provided enough input values
            else:
                pop the top N values from the stack
                evaluate the operator, with the values as arguments
                push returned results (if any) back onto the stack
            end if
        end if
    end while
    if there is only one value on the stack:
        that value is the result of the mathematical expression
    else if there are more than one value on the stack:
        The user input too many values
    endif 

You are to implement a RPN calculator that supports the following operands:

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

Your Task

Finish implementing the missing operators in the bison (calc.y) and flex (calc.l) files. The tasks are denoted by the TO-DO comments. The code can then be tested by the main function also provided. So only the calc.y and calc.l have to be edited.

Thank you!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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[])
{
int i,c;

while((s[0] = c = getch()) == ' ' || c =='\t')
;
s[1] = '\0';
  
i = 0;
if(!isdigit(c) && c!='.' && c!='-')
return c;

if(c=='-')
if(isdigit(c=getch()) || c == '.')
s[++i]=c;
else
{
if(c!=EOF)
ungetch(c);
return '-';
}
  
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 characters\n");
else
buf[bufp++] = c;
}

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)

Add a comment
Know the answer?
Add Answer to:
You are to write a program that implements a Reverse Polish Notation Calculator in C using...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT