Write a program that evaluates a postfix expression (assume it is valid) such as 6 2 + 5 * 8 4 / - The program should read a postfix expression consisting of digits and operators into a character array. Using modified versions of the stack functions implemented earlier in this chapter, the pro- gram should scan the expression and evaluate it. The algorithm is as follows:

1) Append the null character ('\0') to the end of the postfix expression. When the null character is encountered, no further processing is necessary.
2) While '\0' has not been encountered, read the expression from left to right.
If the current character is a digit, Push its integer value onto the stack (the integer value of a digit character is its value in the computer’s character set minus the value of '0' in the computer’s character set). Otherwise, if the current character is an operator, Pop the two top elements of the stack into variables x and y.
Calculate y operator x. Push the result of the calculation onto the stack.
3) When the null character is encountered in the expression, pop the top value of the stack. This is the result of the postfix expression.
[Note: In Step 2 above, if the operator is '/', the top of the stack is 2 and the next element in the stack is 8, then pop 2 into x, pop 8 into y, evaluate 8 / 2 and push the result, 4, back onto the stack. This note also applies to operator '–'.] The arithmetic operations allowed in an expression are
+ addition
– subtraction
* multiplication
/ division
^ exponentiation
% modulus
[Note: We assume left-to-right associativity for all operators for the purpose of this exercise.] The stack should be maintained with stack nodes that contain an int data member and a pointer to the next stack node. You may want to provide the following functional capabilities:
a) function evaluatePostfixExpression that evaluates the postfix expression
b) function calculate that evaluates the expression op1 operator op2
c) function push that pushes a value onto the stack
d) function pop that pops a value off the stack
e) function isEmpty that determines if the stack is empty
f) function printStack that prints the stack



// Using a stack to evaluate an expression in postfix notation
#include
#include
#include
using namespace std;

#include "Stack.h"

// prototype
int evaluatePostfixExpression( char * const );
int calculate( int, int, char );

int main()
{
char expression[ 100 ];
char c;
int answer;
int i = 0;

cout << "Enter a postfix expression:\n";

// get input
while ( ( c = static_cast< char >( cin.get() ) ) != '\n')

if ( c != ' ' )
expression[ i++ ] = c;

expression[ i ] = '\0';
answer = evaluatePostfixExpression( expression );
cout << "The value of the expression is: " << answer << endl;
return 0; // indicates successful termination
} // end main

// evaluate the postfix notation
int evaluatePostfixExpression( char * const expr )
{
int i;
int popVal1;
int popVal2;
int pushVal;
Stack< int > intStack;
char c;

strcat( expr, ")" );

// until it reaches ")"
for ( i = 0; ( c = expr[ i ] ) != ')'; i++ )
{
if ( isdigit( expr[ i ] ) )
{
pushVal = c - '0';
intStack.push( pushVal );
intStack.print();
} // end if
else
{
popVal2 = intStack.pop();
intStack.print();
popVal1 = intStack.pop();
intStack.print();
pushVal = calculate( popVal1, popVal2, expr[ i ] );
intStack.push( pushVal );
intStack.print();
} // end else
} // end for

return intStack.pop();
} // end evaluatePostfixExpression

// do the calculation
int calculate( int op1, int op2, char oper )
{
switch( oper )
{
case '+':
return op1 + op2;
case '-':
return op1 - op2;
case '*':
return op1 * op2;
case '/':
return op1 / op2;
case '^': // exponentiation
return static_cast< int >(
pow( static_cast< float >( op1 ), op2 ) );
} // end switch statement

return 0;
} // end function calculate

Enter a postfix expression:
6 2 + 5 * 8 4 / -
The stack is:
6
The stack is:
2 6
The stack is:
6
Stack is empty
The stack is:
8
The stack is:
5 8
The stack is:
8
Stack is empty
The stack is:
40
The stack is:
8 40
The stack is:
4 8 40
The stack is:
8 40
The stack is:
40
The stack is:
2 40
The stack is:
40
Stack is empty
The stack is:
38
The value of the expression is: 38

Computer Science & Information Technology

You might also like to view...

Extending the first line of text to the left of the text block creates a ____ indent.

A. tracking B. kerning C. leading D. hanging

Computer Science & Information Technology

____ is an exception type that can be handled by a Try-Catch block.

A. TextException B. DivideByZeroException C. OverflowException D. FormatException

Computer Science & Information Technology

Which of the following function headings can be used for a recursive definition of a function to calculate the nth Fibonacci number?

A. void rFibNum(int a, int b) B. bool rFibNum(int a, int b) C. bool rFibNum(int a, int b, int n) D. int rFibNum(int a, int b, int n)

Computer Science & Information Technology

Describe the information systems used by the following functional units: sales, engineering or product development, manufacturing, marketing, sales, and customer service.

What will be an ideal response?

Computer Science & Information Technology