Question
C progam that calculates reverse polish notation Create a program that calculates reverse polish expressions for any integers with the following operators: Addition (+) Subtraction
C progam that calculates reverse polish notation Create a program that calculates reverse polish expressions for any integers with the following operators: Addition (+) Subtraction (-) Multiplication (*) Division (/) Remainder (%) XOR (^) Bitwise AND (&) Bitwise OR (|) Unary Not (~) Example Reverse Polish expressions: 123 21 + 567 432 - * o Algebraic/In-fix Notation: (123 + 21) * (567 - 432) o Answer: 19440
The program takes as a single line of input a reverse polish expression. After reading the entire line of input and processing it, the program outputs the result as a single integer value.
If there are any errors, the program outputs an error message and should then terminate using exit(EXIT_FAILURE) from stdlib.h.
Example: Enter Expression (Reverse Polish): 9 6 & 2 127 & | 127 Result: 0
Part 1: Stack Module
In the stack module, youll be implementing the stack functionality, as we discussed in class. The beginning of stack.c includes prototypes which your function definitions should match.
Make sure that this file has the following functionality: push pop empty As you see fit, feel free to include one or more of the following additional functions if you need to use them: peek size
The stack module has two important features that should be hidden from other modules to avoid breaking the data structure. Make sure that the underlying storage and global variables for the stack are only visible within the same file using the static keyword.
Make sure that the functions are visible because that is how other modules will interact with this module.
Part 2: Calc Module
The calc module is responsible for doing the arithmetic associated with the reverse polish notation calculator.
It only needs to have one function implemented, void eval(char); o The argument to this function is going to be an operator extracted from input. o This function should take the operator, the top two operands on the stack, and evaluate the three of them together, similar to how Homework 6 was done. ? In the case of the unary not operator (~), instead of two operands, the eval function should evaluate it with only the top operand. o The result of the evaluation should be pushed onto the stack and not returned.
This module does need to make use of the functions in the stack module, so it should declare those functions as external using the extern keyword.
If the necessary stack functions fail (return false) the calc module should print an appropriate error message and exit(EXIT_FAILURE) from stdlib.h.
Part 3: Main Module
This is the primary piece of the program responsible for the overall operation. This is where the high-level logic for the Reverse Polish Notation program goes.
The main function should be the only function in this file.
This function should iterate over every token from the input using the io module. Once all input has been read, it should check for the result in the stack module. If the stack module contains more than one result, or less than one result, the program should print an error message and exit without printing an erroneous result. If everything is hunky-dory, it should print the result as mentioned earlier.
Since this module needs to access functions from every other module, make sure to extern them.
Io.c is already given for us which is the code below
#include
int parseoperand(int* op) { return fscanf(stdin, "%d", op) == 1; }
int parseoperator(char* op) { return fscanf(stdin, "%[+-*/%^&|~]", op) == 1; }
int endofstream() { return feof(stdin); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started