0% found this document useful (0 votes)
48 views2 pages

Evaluating Postfix Expressions

This document discusses evaluating postfix expressions using a stack data structure. It provides an algorithm that loops through each token in a valid postfix string, pushing intermediate results onto a stack. When the loop finishes, the final result is the only value remaining on the stack. It includes sample postfix expressions and test runs of an PostfixEval.java program to evaluate them, returning a double. Improvements are suggested to implement additional operators like modulus, powers, and factorial.

Uploaded by

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

Evaluating Postfix Expressions

This document discusses evaluating postfix expressions using a stack data structure. It provides an algorithm that loops through each token in a valid postfix string, pushing intermediate results onto a stack. When the loop finishes, the final result is the only value remaining on the stack. It includes sample postfix expressions and test runs of an PostfixEval.java program to evaluate them, returning a double. Improvements are suggested to implement additional operators like modulus, powers, and factorial.

Uploaded by

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

Evaluating Postfix Expressions

3+4 34+
Infix and Postfix Notation (3 + 4) * 5
A standard expression like 3 + 4 uses infix notation. The 3+4*5
equivalent postfix form is 3 4 +. ("Post" means "after," 3*4+5*6
meaning that the operator appears after its operands.)
3*(4+5)*6
Because of the order of operations, infix notation sometimes
requires parentheses, but postfix expressions never need (3*4+5)*6
parentheses.

Algorithm for Evaluating Postfix Expressions


Assuming the input string contains a valid postfix expression (e.g., 4 5 6 * + 3 -), loop over the
string and process each token. Hint: a stack is a helpful data structure.

Intermediate results are kept on the stack as the loop repeats. When the loop ends the final result will be
the only number left on the stack.

Assignment
PostfixEval.java. At first, limit the operations to add, subtract, multiply, and divide. If we require
that every token in the input string is separated by spaces, e.g., "33 -43 + -55 65 + *", we can
evaluate decimals and negatives. Assume that the postfix string is well-formed. Use as test data the
postfix expressions below.

public static final String operators = "+ - * / % ^ !";

public static double eval(String str)


{ }
public static double eval(double a, double b, String op)
{ }
public static boolean isOperator(String op)
{ }

Test Run
Postfix --> Evaluate
3 4 5 * + 23.0
3 4 * 5 + 17.0
1.3 2.7 + -6 6 * + -32.0
33 -43 + -55 65 + * -100.0
3 4 * 5 2 / + 5 - 9.5
8 1 2 * + 9 3 / - 7.0
3 4 5 * 6 + * 78.0
3 4 5 - 6 2 * - + -10.0

Improvements (required)
Implement modulus, powers, and factorial. Use the input data as shown.
2 7 3 % + 3.0
2 7 + 3 % 0.0
2 3 ^ 8.0
2 -2 ^ 0.25
5 ! 120.0
1 1 1 1 1 + + + + ! 120.0

You might also like