Unit I - Evaluation of expression

2,211 views 22 slides Jul 24, 2020
Slide 1
Slide 1 of 22
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22

About This Presentation

Evalauation of Expression, Infix, Postfix Expression


Slide Content

Evaluation of Expression Dr. R. Khanchana Assistant Professor Department of Computer Science Sri Ramakrishna College of Arts and Science for Women

Evaluation of Expression An expression is made up of operands, operators and delimiters.  Operands - A,B,C,D,E ( these are all one letter variables) Operators **,+,*,-, /, = A  = 4,  B  =  C  = 2,  D  =  E  = 3 Output ?

Evaluation of Expression

Basic Operator Types Basic arithmetic operators: Plus, minus, times, divide, and exponentiation (+,-,*,/,**) Other arithmetic operators Unary plus, unary minus and  mod, ceil , and  floor . Relational operators Relational operators is one of the two constants:  true  or  false ( Boolean)

Priority of arithmetic, Boolean and relational operators

Precedence of Operator Operator Associativity (),{},[] ^ - Right to Left * / - Left to Right + - - Left to Right

Order of Evaluation Scenario 1 A  = 4,  B  =  C  = 2,  D  =  E  = 3, 4/(2 ** 2) + (3 * 3) - (4 * 2) = (4/4) + 9 - 8 = 2. Scenario 2 (4/2) ** (2 + 3) * (3 - 4) * 2 = (4/2) ** 5* -1 * 2 = (2**5)* -2 = 32* -2 = -64.

Infix & Postfix Notation If  e  is an expression with operators and operands, the conventional way of writing  e  is called  infix , because the operators come  in- between the operands. The  postfix  form of an expression calls for each operator to appear  after  its operands. Example Infix:  A * B/C   Postfix:  AB  *  C /

Infix to Postfix Conversion  It is simple to describe an algorithm for producing postfix from infix: 1) Fully parenthesize the expression 2) Move all operators so that they replace their corresponding right parentheses 3) Delete all parentheses. Example : A/B  **  C  +  D  *  E  -  A  *  C   when fully parenthesized yields The arrows point from an operator to its corresponding right parenthesis. Performing steps 2 and 3 gives ABC  ** /  DE  * +  AC  * -

Infix & Postfix Expression Infix:  A / B  **  C  +  D  *  E  -  A  *  C Postfix:  ABC  ** / DE  * +  AC  * - Operation Postfix --------- ------- T 1 = B **C AT 1 /DE * + AC * - T 2 = A/T 1 T 2 DE * + AC * - T 3 = D * E T 2 T 3 + AC * - T 4 = T 2 + T 3 T 4 AC * - T 5 = A * C T 4 T 5 - T 6 = T 4 - T 5 T 6

Steps for Postfix Expression Using Stack Start reading from left to right and push the operands into the stack If an operator occurs pop the last two operands from stack and then perform the repeated operation (Operand 2) Operator (Operand 1) Example 23*51/+ Pop 3 and 2 Push (2*3) Pop 5 and 1 Push (5/1) Push (6 +5)

Procedure -Expression Evaluation

STACK Implementation A  +  B  *  C  to yield  ABC  * + our algorithm should perform the following sequence of stacking

STACK Implementation Animated Video https://www.youtube.com/watch?v=TeM6DdvSxvQ

Assignment Another example,  A  * ( B  +  C ) *  D  has the postfix form  ABC  + *  D  *

Assignment Results A  * ( B  +  C ) * 

Priorities of Operators for Producing Postfix

Postfix conversion with Stack EXAMPLE 1

Postfix conversion with Stack EXAMPLE 2 - - -

Postfix conversion with Stack ( A+B)*C – (D*E) Ch Stack Postfix Expression ( ( A A + + A ( B + AB ( ) ) AB+ + ( * * AB+ C * AB+ - - AB+* Ch Stack Postfix Expression ( ( AB+* - D ( AB+*D _ * * AB+*D ( - E * AB+* DE ( - ) ) AB+*DE*- * ( _ EXAMPLE Vs Assignment

Procedure Postfix

Quiz https://quizizz.com/admin/quiz/5f14103a219aa5001b718cf1
Tags