9 - CMPSCI 187: Programming With Data Structures Lecture...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
CMPSCI 187: Programming With Data Structures Lecture #9: The Evaluator Program 26 September 2011
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Evaluator Program • The Problem: Postfx Evaluation • The Method: Pending Arguments on a Stack • The Driver Class Postfix • The Class PostfixEvaluator • Complications: I/O, StringTokenizer , Wrapper classes, Exceptions
Background image of page 2
The Problem: Postfx Evaluation • We have arithmetic expressions where the constants are int values and the operators are + , - , * , and / . • We are more used to infx expressions like (b*b)-(4*a*c) . • It turns out that postfx expressions like b b * 4 a * c * - are easier to evaluate with a stack -- they have no ambiguities to be resolved by operator hierarchy or parentheses. • To translate infx to postfx, fnd the highest-priority operator, so that the infx expression can be read as R op S, then write “R S op”, then translate both R and S to postfx. • We want to input the postfx expression as a string and output an int.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The Method: Pending Arguments on a Stack • We create a Stack<Integer> object to store arguments still to be used. • If our Frst input is “4”, we cannot process it yet so we push it. • If our second input is “7”, we still don’t know what to do so we push it.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 9

9 - CMPSCI 187: Programming With Data Structures Lecture...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online