2. İf we look at the arithmetic expressions, we encounter operand, operator
and parenthesis.
2+3 , (p*q), 2+5*6
For evaluting this expressions, we should parse and them calculate.
<operand><operator><operand> (infix)
Operand: Any object that operation is performed.
Operator: In mathematics and sometimes in computer programming, an
operator is a character that represents an action, as for example x is an
arithmetic operator that represents multiplication.
2
Evaluation of Expressions
3. For example 2+3*5-1*4
To evaluate this expressions you should know precedence of
operator.
Order of operations
1. Parentheses {()}
2. Exponents 2^3^2 (right to left)
3. Multiplication and division (left to right)
4. Addition and subtraction (left to right)
2*6/3+5-2+8/4*2
3
Evaluation of Expressions
6. a*b+c*d-2
First step we should add parentheses
{(a*b)+(c*d)}-2
Next step is that operator will place to end of expression
{(ab*)+(cd*)}-2
{ab*cd*+}-2
ab*cd*+2-
6
Evaluation of prefix and postfix
7. 2*3+4*5-6=>23*45*+6- (postfix)
evaluatePostfix(exp){
create a Stack S
for i<-0 to length(exp)-1
{
if (exp[i] is operand)
push(exp[i])
else if (exp[i] is operator){
op2<-pop()
op1<-pop()
temp<perform(op1,op2,exp[i])
push(temp)
}
} return S[top]
}
7
Evaluation of Postfix with Stack
8. 2*3+4*5-6=>-+*23*456 (prefix)
This algorithm is same with postfix algorithm. But this
algorithm starts from end of expression.
8
Evaluation of Prefix with Stack
9. A+B*C-D*E
InfixToPostfix(exp){
create a Stack S
String temp<-empty String
for i<-0 to length(exp)-1{
if exp[i] is operand
temp <-temp+exp[i]
else if exp[i] is operator
while(!S.empty()&&HasHigherPrecedence(s.top(),exp[i])){
temp <-temp + s.pop();
}
s.push(exp[i])
}
while(!s.empty()) temp<-temp+s.pop();
Return 1;
}
9
Infix to postfix