Evaluate Postfix
Evaluate Postfix
In this assignment, you will write a function that will evaluate a postfix expression using a stack.
You will not write a main() routine. One will be supplied for you and it will call your evaluate()
function.
1. Log in to Unix.
setup 14
For this assignment, you will use two files provided by the setup command on Unix and the
Grade-o-Matic: a makefile and a driver program called main.cpp that will test your
evaluate() function by passing it a series of postfix expression strings. In order to build and run
this assignment on your own local machine, you will also need your mystack.h and
mystack.cpp files from Assignment 13.
The makefile can be used to build the program for this part of the assignment by typing:
make
./eval
You will write the following files and submit them to the Grade-o-Matic:
You do not need to submit your mystack.cpp and mystack.h files from Assignment 13 to the
Grade-o-Matic. It will supply its own versions of those files (in case you never got yours to work).
eval.cpp
This function evaluates the postfix expression passed to it (which will be the result of calling the
convert() function) and returns the calculated value. You may assume the following:
In evaluating the expression you must assign the following values to any variables you encounter:
a = 0, b = 1, c = 2, etc. See Hints below for an easy way to do that.
When performing exponentiation, you must calculate the value with your own code, i.e., you must
write your own loop to calculate the value rather than calling the pow() function from the math
library.
When performing division, if you encounter a division by 0 error (i.e., if the divisor is 0), do not
attempt to divide by 0. Instead, print an error message "*** Division by 0 ***" and “push”
the result 0 onto the stack.
To process the postfix string and break it into operators / operands, you may find it useful to use
the standard library class std::stringstream to process postfix. You can create an object of
the stringstream class from a C++ string, and then use the same operators and member
functions to read input from the stringstream that you can use with any other input stream
(such as cin or an input file stream variable).
If you do use the stringstream class, your code for evaluate() will look something like this:
#include <string>
#include <sstream>
...
using std::string;
using std::stringstream;
...
...
}
...
}
Scan the postfix string from left to right, extracting and processing one token (operator/operand)
at a time:
If the token is a variable, calculate the value of the variable and push that value on the
eval_stack.
If the token is a '~' operator, get the top item from the eval_stack, pop the stack, apply
the operator, and push the result on the eval_stack.
If the token is any other operator, you will need to obtain the right operand by getting the top
item of the eval_stack and then popping the stack. Repeat those two steps to get the left
operand. Perform the arithmetic specified by the operator with the left and right operands
and then push the result on to the eval_stack.
When you reach the end of the postfix string, the final result of evaluating the postfix expression
will be the top (and only) item on the eval_stack.
You must submit your eval.h and eval.cpp files to the Grade-o-Matic for grading.
4. Output
The only output generated by your files is the "*** Division by 0 ***" message printed in
eval.cpp. The remaining output from this program is generated by the main routine supplied for
you in main.cpp. An evaluate function that works correctly will result in the following output:
infix: 2
postfix: 2
value: 2
infix: 2+a
postfix: 2 a +
value: 2
infix: (1+a) ^2
postfix: 1 a + 2 ^
value: 1
infix: ~(1+a) ^2
postfix: 1 a + ~ 2 ^
value: 1
infix: ~((1+a)^2)
postfix: 1 a + 2 ^ ~
value: -1
infix: 0/0
postfix: 0 0 /
*** Division by 0 ***
value: 0
infix: 1/0
postfix: 1 0 /
*** Division by 0 ***
value: 0
infix: 0/1
postfix: 0 1 /
value: 0
infix: c^a
postfix: c a ^
value: 1
infix: ~c^a
postfix: c ~ a ^
value: 1
infix: c^d
postfix: c d ^
value: 8
infix: ~c^d
postfix: c ~ d ^
value: -8
infix: d+1
postfix: d 1 +
value: 4
infix: d +1 *2
postfix: d 1 2 * +
value: 5
infix: ( d +1) *2
postfix: d 1 + 2 *
value: 8
infix: a-e-a
postfix: a e - a -
value: -4
infix: (a-e-a)/( ~d + 1)
postfix: a e - a - d ~ 1 + /
value: 2
infix: (a^2 + ~b ^ 2) * (5 - c)
postfix: a 2 ^ b ~ 2 ^ + 5 c - *
value: 3
infix: ~ 3 * ~( a + 1) - b / c ^ 2
postfix: 3 ~ a 1 + ~ * b c 2 ^ / -
value: 3
infix: a+b/c^d-~e*f
postfix: a b c d ^ / + e ~ f * -
value: 20
5. Hints
Testing the first character of the string op can tell you whether it is a single letter variable
name, an integer literal, or an operator.
You may find it convenient to calculate the value of each variable (a through z) simply by
using the expression var - 'a', which assumes that the single letter variable name is
stored in the variable
char var;