Presentation is loading. Please wait.

Presentation is loading. Please wait.

Arithmetic Expressions

Similar presentations


Presentation on theme: "Arithmetic Expressions"— Presentation transcript:

1 Arithmetic Expressions
transformation and evaluation of expressions using stacks

2 Notation for arithmetic expressions
infix – operation between operands (3 + 5) * 10 prefix – operation before operands * postfix – operation after operands * 2006 evaluating expressions

3 2006 evaluating expressions
Postfix expressions evaluating postfix: repeat { find first operation preceded by two operands evaluate and replace } 4 5 * 10 2 / + = / + = = 25 2006 evaluating expressions

4 2006 evaluating expressions
Postfix examples 3 5 1 – * 10 - * * 2006 evaluating expressions

5 2006 evaluating expressions
Postfix examples 3 5 1 – * 10 - = 3 4 * 10 - = = 2 * * = 3 2 * * = * = 15 7 * = 105 2006 evaluating expressions

6 Arithmetic in programming languages
expressions are infix (human readability) evaluation in two steps: translate from infix to postfix evaluate postfix expression both steps use stacks 2006 evaluating expressions

7 2006 evaluating expressions
infix to postfix infix expressions have rules for order of opertions: operations in parentheses multiplication and division, left to right addition and subtraction, left to right 8 + 3 * (10 – 4) / 2 – 5 2006 evaluating expressions

8 2006 evaluating expressions
infix to postfix translation to postfix must get operations in correct order 2006 evaluating expressions

9 fully parenthesized expressions
every operation is in parentheses: ( 3 – 12 ) ( 4 + ( 10 * 2 ) ) *2 ( ( ( 6 + 3) + 5 ) + 8 ) order of operations is forced by parentheses left-to-right convention ( ( 6 + 3) + ( ) ) 2006 evaluating expressions

10 infix to postfix: version 1 fully parenthesized expressions
Algorithm (assumes infix expression is correct) stack <- new CharacterStack while more input from infix expression get token switch (token) case ‘(‘: push token on stack case operand: write token to output case ‘+’,’-’,’*’,’/’: push token on stack case ‘)’: discard token pop operation from stack and write to output pop ‘(‘ from stack and discard 2006 evaluating expressions

11 infix to postfix: version 1 fully parenthesized expressions
expression stack output ( 5 * ( ( 3 * 2 ) + 8 ) ) ( ( 5 * ( ( 3 * 2 ) + 8 ) ) ( 5 ( 5 * ( ( 3 * 2 ) + 8 ) ) (* 5 ( 5 * ( ( 3 * 2 ) + 8 ) ) (*( 5 ( 5 * ( ( 3 * 2 ) + 8 ) ) (*(( 5 ( 5 * ( ( 3 * 2 ) + 8 ) ) (*(( 5 3 ( 5 * ( ( 3 * 2 ) + 8 ) ) (*((* 5 3 ( 5 * ( ( 3 * 2 ) + 8 ) ) (*((* ( 5 * ( ( 3 * 2 ) + 8 ) ) (*( * ( 5 * ( ( 3 * 2 ) + 8 ) ) (*( * ( 5 * ( ( 3 * 2 ) + 8 ) ) (*( * 8 ( 5 * ( ( 3 * 2 ) + 8 ) ) (* * 8 + ( 5 * ( ( 3 * 2 ) + 8 ) ) * 8 + *

12 expressions with precedence
every operation is in parentheses: * 2 4 - ( ) * 2 order of operations is forced by parentheses precedence of operations ( *, / before +,-) left-to-right order of operations with same precedence 2006 evaluating expressions

13 infix to postfix: version 2 expressions with precedence
Algorithm (assumes infix expression is correct) stack <- new CharacterStack while more input from infix expression get token switch (token) case ‘(‘: push token on stack case operand: write token to output case ‘+’,’-’,’*’,’/’: pop and output operations from stack until stack empty OR next symbol is ‘(‘ OR next symbol is lower precedence than token push token on stack case ‘)’: discard token pop operations from stack and write to output until next symbol is ‘(‘ pop ‘(‘ from stack and discard pop and write to output any remaining operations

14 infix to postfix: version 2 expressions with precedence
expression stack output 3 + 5 * 3 + 5 * 3 + 5 * 3 + 5 * * 3 5 3 + 5 * * 3 5 4 * + 3 * 3 * * 3 3 * * 3 5 3 * * 3 * * 4 3 5 * 4 +

15 infix to postfix: version 2 expressions with precedence
example stack output ( ) * 4 ( ( ) * ( ) * 4 ( + 3 ( ) * 4 ( ( ) * ( ) * 4 * ( ) * 4 * * 3 * ( ) 3 3 * ( ) * 3 3 * ( ) * ( 3 3 * ( ) * ( 3 5 3 * ( ) * ( 3 * ( ) * ( 3 * ( ) * *

16 2006 evaluating expressions
evaluating postfix Algorithm stack <- new DoubleStack while more input from postfix expression get token switch (token) case operand: push token on stack case ‘+’,’-’,’*’,’/’: pop two operands from stack combine with operation push result on stack pop final result from stack and output 2006 evaluating expressions

17 postfix evaluation example
postfix expression stack output 5 3 2 * 8 + * 5 5 3 2 * 8 + * 5 3 5 3 2 * 8 + * 5 3 2 * 8 + * 5 6 5 3 2 * 8 + * 5 3 2 * 8 + * 5 3 2 * 8 + * 70 5 3 2 * 8 + *


Download ppt "Arithmetic Expressions"

Similar presentations


Ads by Google