Expressioneval - Expression evaluation assignment. Using 2...

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

View Full Document Right Arrow Icon
Expression evaluation assignment. Using 2 stacks you can evaluate an infix expression in 1 pass without converting to postfix first: Algorithm 1. Create an empty operator stack 2. Create an empty operand stack 3. Repeat 3.1 Get the next token in the infix string 3.2 If next token is an operand, place it on the operand stack 3.3 If next token is an operator 3.3.1 Evaluate the operator (next op) Until at the end of the string 4. while operator stack is not empty pop operator and operands (left and right), evaluate left operator right and push result onto operand stack 5. Pop result from operator stack Algorithm with further refinement. data fields: 2 stacks boolean inNumber; // processing a number int numValue; // value of number being processed. public int scanInfixString(String infix) boolean inNumber = false; int numVal = 0; for each character position in infix { nextChar = charAt(i); if nextChar is a digit processDigit(nextChar); inNumber = true; else if nextChar is an operator processOperator(nextChar);
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Expressioneval - Expression evaluation assignment. Using 2...

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

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