Please write a C program that can calculate reverse polish notation Please add s
ID: 3713239 • Letter: P
Question
Please write a C program that can calculate reverse polish notation
Please add stack.c , calc.c and main.c files to the following file(file.c) given below
Stack.C file
In the stack.c file , you will be applying he stack functionality push,pop,empty . Make sure storage and global variables for stack are only visible within the file by using 'static' variables.
Calc.c file
in the calc.c file, you will be applying arithmetic associations of reverse polish notation . It will only require one function void eval(char);
which is going to be operator extracted from user input
this function will take operator , the two operands of the stack then evaluate the three of them together
and the final result should be pushed into stack , but not actually returned !
Since ,this file will be using functions from stack.c file , please declar them as 'extern variables'.
On the failure, this should exit using (EXIT_FAILURE) which is included at stdlib.h
Main.c file
This file is responsible for overall calculation. There should be only one function named main which should go over ever token from the input from file.c file. Once everything is read it should check from the result in stack.c file. If stack.c contains 1 result or more , less that 1 result , then the program will print error message and exit . If all is ok then it should pring the correct output. Once again , since this main.c file will require to access functions from other files, declar them as extern variables.
Given file.c
int parseoperand(int* op) {
char c;
while (isspace((c = getchar())));
ungetc(c, stdin);
if (c == '+' || c == '-')
return 0;
return fscanf(stdin, "%d", op) == 1;
}
int parseoperator(char* op) {
char c;
while (isspace((c = getchar())));
if (c == '+' || c == '-') {
*op = c;
return 1;
}
ungetc(c, stdin);
return fscanf(stdin, "%[*/%^&|~]", op) == 1;
}
int endofstream() {
return feof(stdin);
}
You should add three files stack.c ,calc.c and main.c .Thank you
Example
• 120 20 + 566 431 - *
o Algebraic/In-fix Notation: (120 + 20) * (566 - 431)
o Answer: 18900
Explanation / Answer
#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 ");
break;
case ' ':
printf(" %.8g ", pop());
break;
default:
printf("error: unknown command %s ", 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 ",f);
}
double pop(void)
{
if(sp>0)
return val[--sp];
else
{
printf("error: stack empty ");
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 ==' ')
;
s[1] = '';
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] = '';
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 ");
else
buf[bufp++] = c;
}