COMP SCI 11 WITH C++
COMP SCI 11 WITH C++ CSC 1254
Popular in Course
Popular in ComputerScienence
This 4 page Class Notes was uploaded by Mr. Molly Kessler on Tuesday October 13, 2015. The Class Notes belongs to CSC 1254 at Louisiana State University taught by W. Duncan in Fall. Since its upload, it has received 8 views. For similar materials see /class/222858/csc-1254-louisiana-state-university in ComputerScienence at Louisiana State University.
Reviews for COMP SCI 11 WITH C++
Report this Material
What is Karma?
Karma is the currency of StudySoup.
Date Created: 10/13/15
CBC 1254 Lecture 16 An Application Using The Stack ADT Operations October 27 2009 0 Designing a Simple Calculator 0 Evaluating Post x Expressions 0 Converting ln x to Post x Expressions 0 APPENDIX The Calculator Code 1 Designing a Simple Calculator In this lecture we will use the ADT stack operations to solve the problem of evaluating simple arithmetic expressions As is the case with using any ADT7 we are not going to make any assumptions regarding the ADT7s implemen tation The strategy we will employ is to rst convert the in x arithmetic expression to a post x expression The stack operations work more naturally with post x expressions Evaluating in x expression is a more involved prob lem For the purpose of this lecture we will restrict ourselves to the following binary operations 7 7 7 and Exponentiation and unary operators will be disallowed to make our lives somewhat easyl Here is a summary of our simplifying assumptions 0 The input is a syntactically correct post x expression 0 No unary operators are present 0 No exponentiation is allowed 0 Operands are positive integers 2 Evaluating Post X Expressions Here is the pseudocode algorithm that evaluates post x expressions for each token Ch in the expression if Ch is an operand Push value that operand Ch represents onto stack else Ch is an operator named Up evaluate and push the result Uperand2 top of stack Pop the stack Uperandl top of stack Pop the stack Result Uperandl Up Uperand2 Push Result onto stack Upon termination of the algorithm7 the value ofthe expression will be on the top of the stack 3 Converting In X to Post X Expressions Here is a high level description of what we must do to convert an expression written in in x notation to one written in post x notation H When you encounter an operand7 append it to the output expression PE 3 Push each 77 77 onto the stack S 00 When you encounter an operator7 if S is empty7 push the operator onto the S However7 if S is not empty7 pop operators of greater or equal precedence off S and append them to PE Stop when you encounter 77 77 or an operator of lower precedence 4 When you encounter 77777 pop operators off the stack and append them to the end of PE7 until you encounter the matching 7777 2 5 When you reach the end of the PE7 you append the remaining contents of the stack to PE Here is a pseudocode algorithm that converts an in x expression to post x mnr for each Ch in the infix form switchCh case operand append operand to the end of PE PE PE Ch represents appending break case quotquot2 push Ch onto S break case quotquot while top of S is not quotquot PE PEtop of S pop S pop S break case operator while S is not empty AND top of S is not quotquot AND PrecedenceCh lt Precedencetop of stack PE PEtop of S pop S push Ch onto S break append to PE the operators remaining in the stack while S is not empty PE PEtop of S pop S