Evaluation of Expressions Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The...

19
Evaluation of Expressions Instructor : Prof. Jyh-Shing Roger Jang Designer Shao-Huan Wang The ideas are reference to the textbook “Fundamentals of Data Structures in C “.

Transcript of Evaluation of Expressions Instructor : Prof. Jyh-Shing Roger Jang Designer : Shao-Huan Wang The...

Evaluation of Expressions

Instructor : Prof. Jyh-Shing Roger Jang

Designer : Shao-Huan WangThe ideas are reference to the textbook “Fundamentals of Data Structures in C “.

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

( a b/ ( - c + d ) ) * *( )- ae c \0

Put operators to stack and numbers to array.

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

(

a

b/ ( - c + d ) ) * *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

(

a b

/

(

- c + d ) ) * *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,“(“ push on any operators and pop when “)” push to stack.

Not high order than “-”

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

(

a b

/

(

-

c

+ d ) ) * *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.

Not high order than “-”

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

(

a b

/

(

-c

+

d

)

) * *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

(

a b

/

-c d

) * *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

+

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

a b

/

-c d

* *( )- ae c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

+

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

a b /-c d

*

*

(

)

-

ae

c \0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

+

Not high order than “*”

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

a b /-c d *

*

-ae c

\0

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

+

“\0” is lowest order operator,so pop all the operators in stack.

Evaluation of Expressions Infix to PosfixUse stack to change infix into posfix

First, we prepare a stack and an array.

a b /-c d * *-ae c

Put operators to stack and numbers to array.If the operator is high order than the top term of stack,push it to stack, otherwise pop the top term to array. However, the ( ) operators without following the rules,push “(“ on any operators and pop when push “)” to stack.If push “)” to the stack, pop the operators between ( )to array and pop the ( ) without being stored.

+

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

a b /-c d * *-ae c+

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

5 10 /-7 2 * *-515 7+

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

5

10

/-

7

2 * *-515 7+

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

= 3

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

5

/2 * *-515 7+

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

3

=5

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

5

/ * *-515 7

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

5

=1

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

* *-515 7

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

1

=10

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

* *7

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

1

10

=10

Evaluation of Expressions Infix to Posfix Use posfix to calculate value

*

7

Push the item of array to stackIf the item is operator, operating the two top of stackwith this token and push to stack.

10

=70answer of the expressions