CMPS 1053 – Program 4 – Postfix Notation & Evaluation –...

advertisement
CMPS 1053 – Program 4 – Postfix Notation & Evaluation – Spring 2012
Due: Friday, March 2
You are to have 2 Stack Classes named StackInt and StackChar. Both must have the same functions
PUSH, POP, VIEW, FULL, EMPTY. Make all functions public.
You will be given 2 separate files PostEval and PostConvert, each containing 5 strings, one string per line.
(There will be a blank between each input character that you will have to skip over when processing.)
Open the first file, process the strings, then close the file. Then open the second file, process the
strings, then close the file.
Your main function will read one postfix expression, one per line, that is to be evaluated using StackInt.
Read one full line using the getline function to place the input string into an array of characters.
char postfix[80]; infile.getline(postfix,80); //p. 125
getline automatically places the null terminator (‘\0’) in the array immediately following the last
character. Then process the string by accessing the characters from the array. Print the original
postfix expression to an output file followed by the solution as shown below. Print one blank between
each character.
4 5 + 8 - evaluates to 1
Then main is to read from the second (different) file containing 5 infix expressions. Again read one line
at a time as shown above. Print the original infix expression, with 1 blank between each character. Then
using StackChar, convert the infix expression to a postfix expression, printing one blank between each
character as shown below.
4 + 5 – 8
converts to
4 5 + 8 –
For testing, use cin & cout, one string at a time, trying a variety of expressions. I will provide a data file
for you to use for turning in next week.
~~~~~~~~~~~~~~
Evaluation: Converting characters to numeric values
See textbook section 12.3 – 12.4 p. 787
#include <cctype>
char x; int val; StackInt ST;
x = Postfix [i];
if (isdigit(x)) {val = x-48;) // converts ASCII to integer
ST.Push(val);
Download