0% found this document useful (0 votes)
10 views6 pages

Evaluate Postfix

Uploaded by

Robo rando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views6 pages

Evaluate Postfix

Uploaded by

Robo rando
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 14: Evaluate Postfix Expressions

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. Obtaining Files on Unix

1. Log in to Unix.

2. Run the setup script for Assignment 14 by typing:

setup 14

2. Files We Give You

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

To run the program, type:

./eval

2. Files You Must Write and Submit

You will write the following files and submit them to the Grade-o-Matic:

 eval.cpp - contains your evaluate() function.


 eval.h - contains the function prototype for eval() so that the main() can call it.
The file eval.cpp is described in more detail below. The header file should contain header
guards to prevent it from being included multiple times in the same source file.

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 file should contain a definition for the following function:

int evaluate(const std::string& postfix);

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:

 postfix is a valid expression with no leading whitespace.


 All operators/operands have at least one space between them.
 postfix may contain any of the operators described in Assignment 12, single character
lower case variables, or constants.
 All constants are integers.
 All exponents are >= 0.

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;
...

int evaluate(const string& postfix)


{
string op;
stringstream ss(postfix); // Create a stringstream object
// from the postfix string.

// You can now read from the stringstream as if it were standard


// input. The end of the string will be treated as the end of input.

while (ss >> op)


{
// op is a C++ string containing the next operator/operand
// in the postfix expression.

...
}

...
}

Postfix evaluation algorithm


Here is a description of the logic for evaluating a postfix expression using a stack.

 Let eval_stack be a stack used to hold operands during evaluation.


 Let postfix be a string containing the postfix expression tokens.

To evaluate the postfix expression:

Scan the postfix string from left to right, extracting and processing one token (operator/operand)
at a time:

 If the token is an integer literal, push it on the eval_stack.

 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.

3. Files You Must Submit

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: 246 + b /123


postfix: 246 b 123 / +
value: -10
infix: ( 246 + (( b /123) ) )
postfix: 246 b 123 / +
value: -10

infix: ( ( 246 + b) /123)


postfix: 246 b + 123 /
value: 0

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy