Docsity

Stack ADT for Arithmetic Expressions: Simple Calculator and Infix to Postfix Conversion - , Study notes of Computer Science

Toggle sidebar
bg1
CSc 1254: Lecture # 16
An Application Using The Stack ADT Operations
October 27, 2009
•Designing a Simple Calculator
•Evaluating Postfix Expressions
•Converting Infix to Postfix Expressions
•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 ADT,
we are not going to make any assumptions regarding the ADT’s implemen-
tation. The strategy we will employ is to first convert the infix arithmetic
expression to a postfix expression. The stack operations work more naturally
with postfix expressions. Evaluating infix expression is a more involved prob-
lem. For the purpose of this lecture we will restrict ourselves to the following
binary operations: *, /, +, -, and %. Exponentiation and unary operators
will be disallowed to make our lives somewhat easy! Here is a summary of
our simplifying assumptions:
•The input is a syntactically correct postfix expression.
•No unary operators are present.
•No exponentiation is allowed.
•Operands are positive integers.
1
pf3
pf4

Partial preview of the text

Download Stack ADT for Arithmetic Expressions: Simple Calculator and Infix to Postfix Conversion - and more Study notes Computer Science in PDF only on Docsity!

CSc 1254: Lecture # 16

An Application Using The Stack ADT Operations

October 27, 2009

  • Designing a Simple Calculator
  • Evaluating Postfix Expressions
  • Converting Infix to Postfix Expressions
  • 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 ADT, we are not going to make any assumptions regarding the ADT’s implemen- tation. The strategy we will employ is to first convert the infix arithmetic expression to a postfix expression. The stack operations work more naturally with postfix expressions. Evaluating infix expression is a more involved prob- lem. For the purpose of this lecture we will restrict ourselves to the following binary operations: *, /, +, -, and %. Exponentiation and unary operators will be disallowed to make our lives somewhat easy! Here is a summary of our simplifying assumptions:

  • The input is a syntactically correct postfix expression.
  • No unary operators are present.
  • No exponentiation is allowed.
  • Operands are positive integers.

2 Evaluating Postfix Expressions

Here is the pseudocode algorithm that evaluates postfix 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 Op { //evaluate and push the result Operand2 = top of stack Pop the stack Operand1 = top of stack Pop the stack Result = Operand1 Op Operand Push Result onto stack } }

Upon termination of the algorithm, the value of the expression will be on the top of the stack.

3 Converting Infix to Postfix Expressions

Here is a high-level description of what we must do to convert an expression written in infix notation to one written in postfix notation.

  1. When you encounter an operand, append it to the output expression PE.
  2. Push each ”(” onto the stack S.
  3. When you encounter an operator, if S is empty, push the operator onto the S. However, if S is not empty, pop operators of greater or equal precedence off S and append them to PE. Stop when you encounter ”(” or an operator of lower precedence.
  4. When you encounter ”)”, pop operators off the stack and append them to the end of PE, until you encounter the matching ”(”.

// append to PE the operators remaining in the stack while (S is not empty) { PE = PE.(top of S) pop S }