Assignment 7
Evaluating Post-Fix Expressions
A text file contains a series of post-fix expressions, one on each line.
The text file may have any legal Windows name.
The numbers in this post-fix expression are in the range [0..9] and
are each represented by a single character (digit).
The operations are all binary operations. They are +,
-, *, / which represent addition, subtraction, multiplication,
and division.
Each post-fix expression ends with the # character.
Write a program that would
-
Read and evaluate each expression.
-
On a text file of the user's choice,
-
On the first line of the output file write your name.
-
On the second line of the output file write the title of this project
-
Follow this with a blank line.
-
Then write each expression, one expression per line.
-
On the same line as the expression, write
-
" has a value of " followed by the value of the expression if the expression
is valid.
-
" error -- two few operands for operator" if an empty stack is found when
performing an operation
-
" error -- expression incomplete" if the stack contains more than one value
when the expression has terminated.
-
" error -- illegal character found" if the expression contains an illegal
character.
-
" error -- division by zero" if an attempt to divide by zero occurs.
Put the source code for this program, including all included files, in
a folder on a diskette. Also in this folder should be a sample input
file. No other files should be in this folder.
When you have your oral presentation for Examination III the source
code and the sample input file will be copied to your instructors hard
drive and executed. It will be executed using your sample input file.
It will also be executed using an input file prepared by your instructor.
Sample input file
12+#
67-#
47*#
92/#
56-3+8*#
377-/#
845*#
45//#
56P-+#
#
123456789012345678901234567890+++++++++++++++++++++++++++++#
Resulting output file
12+# has a value of 3
67-# has a value of -1
47*# has a value of 28
92/# has a value of 4
56-3+8*# has a value of 16
377-/# error -- division by zero
845*# error -- expression incomplete
45//# error -- two few operands for operator
56P-+# error -- illegal character found
# error -- no value left on stack
123456789012345678901234567890+++++++++++++++++++++++++++++#
has a value of 135
Programming Concepts
Reading until the end of the file
while ( !InFile.eof())
{ InFile.getline(S, 80, '\n');
// process line
}
Basic loop for evaluating a string containing a post-fixed expression
int j = 0;
while ( Expression[j] != '#')
{
// handle the character
j++;
}
Declaring error constants
const int DivisionByZero = -32768;
const int IncompleteError = -32767;
Basic statement for handling a character in a post-fixed expression
switch (Expression[j]) {
case '0':
case '1':
...
...
case '9':
ExpStack.push(Expression[j]
- '0');
break;
case '+';
if(!ExpStack.empty())
{
Operand1 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
if (!ExpStack.empty())
{
Operand2 = ExpStack.top();
ExpStack.pop();}
else
return OperatorError;
ExpStack.push(Operand1+Operand2);
break;
case '-':
...
case '*':
...
case '/':
...
default:
return
IllegalCharacter;
}