You are on page 1of 6

Aim:

To write an C++ program to evaluate a postfix expression.


Algorithm:
1. Start the program
2. Get the expression from the user.
3. Create stack for evaluating the expression.
4. read the expression from left to right.
5. If an operand is seen push it on to stack.
!. If an operator is seen pop of top t"o elements#operands$ from stack and
perform the corresponding operation push the resulted output onto stack.
%. &epeat steps 5 and ! till the expression is over.
'. Stop the program.
#include<iostream>
#include<cctype>
#include<stack>
using namespace std;
// returns the value when a specific operator
// operates on two operands
int eval(int op1, int op2, char operate) {
switch (operate) {
case '*': return op2 * op1;
case '/': return op2 / op1;
case '+': return op2 + op1;
case '-': return op2 - op1;
default : return 0;
}
}
// evaluates the postfix operation
// this module neither supports multiple digit integers
// nor looks for valid expression
// However it can be easily modified and some additional
// code can be added to overcome the above mentioned limitations
// it's a simple function which implements the basic logic to
// evaluate postfix operations using stack
int evalPostfix(char postfix[, int si!e) {
sta"#$int% s;
int i & 0;
char "';
int val;
while (i $ si!e) {
"' & postfix[i;
if (isdi(it("')) {
// we saw an operand
// push the digit onto stack
s)p*s'("'-'0');
}
else {
// we saw an operator
// pop off the top two operands from the
// stack and evalute them using the current
// operator
int op1 & s)top();
s)pop();
int op2 & s)top();
s)pop();
val & eval(op1, op2, "');
// push the value obtained after evaluating
// onto the stack
s)p*s'(val);
}
i++;
}
return val;
}
// main
int +ai,() {
char postfix[ & {'-','.','/','+','*','2','/'};
int si!e & sizeof(postfix);
int val & evalPostfix(postfix, si!e);
"o*t$$01,2xpressio, eval*ates to 0$$val;
"o*t$$e,dl;
return 0;
}
Algorithm for prefix to postfix expression
1.Start the program
2.Include the header files
3. Initialize the input string, the output string, and the operators stack.
4. Repeat steps 3! until there are no more input s"m#ols.
!. $et the next input s"m#ol.
%. If it is an operator, push it on the stack.
&. If it is an operand, append it to the output string and the operand must correspond
to the top operator on the stack.
'.(heck )hether the operand processed is the first or the second operand associated
)ith the top operator.If it is the second, it is time to append the top operator to the
output string.
*. +ispla" the output.
1,. stop the program.
3i,"l*de $iostrea+%
002 3i,"l*de $stri,(%
004 3i,"l*de $fstrea+%
005

00-
*si,( ,a+espa"e std;
00.

006
7ool is8perator("'ar s9+7ol) //defi,itio, of is8perator
00/ {
00:
if( (s9+7ol && '*') ;; (s9+7ol && '+') ;; (s9+7ol && '/') ;; (s9+7ol
&& '-' ) )
010
ret*r, tr*e;
011 else
012
ret*r, false;
014 }
015

01-
"lass <ta"# {
01.

016 p*7li":
01/
i,t =ax<ta"#;
01:
i,t 2+pt9<ta"#;
020 "'ar* ite+s;
021
i,t top;
022

024

025 <ta"#(i,t);
02- ><ta"#();
02.
i,t ?8P();
026
void p*s'("'ar);
02/
"'ar pop();
02:
i,t e+pt9();
040
i,t f*ll();
041

042 };
044
<ta"#::<ta"#(i,t si!e) {
045 =ax<ta"# & si!e;
04- 2+pt9<ta"# & -1;
04.
top & 2+pt9<ta"#;
046
ite+s & ,e@ "'ar[=ax<ta"#;
04/ }
04:

050 <ta"#::><ta"#() {delete[ ite+s;}
051
i,t <ta"#::?8P(){
052
ret*r, ite+s[top;
054 }
055
void <ta"#::p*s'("'ar ") {
05- ite+s[++top & ";
05. }
056

05/
"'ar <ta"#::pop() {
05:
ret*r, ite+s[top--;
0-0 }
0-1

0-2
i,t <ta"#::f*ll() {
0-4
ret*r, top + 1 && =ax<ta"#;
0-5 }
0--

0-.
i,t <ta"#::e+pt9() {
0-6
ret*r, top && 2+pt9<ta"#;
0-/ }
0-:

0.0

0.1

0.2
i,t +ai,()
0.4 {
0.5
"o*t $$0A2BC8=2 ?8 PD8E2C? 5 PD8FDG=0$$e,dl;
0.-

0.. // 7asi" "o+po,e,ts
0.6 stri,( li,e;
0./ stri,( o*tp*tBi,e;
0.:

060 //sta"#s
061

062 <ta"# sta"#(20);
064
"'ar ";
065

06-

06. ifstrea+ +9file (0Prefix)i,0);
066
if (+9file)isHope,())
06/ {
06:
@'ile ( +9file)(ood() )
0/0 {
0/1 (etli,e(+9file,li,e);
0/2
"o*t $$ li,e $$ e,dl;
0/4

0/5 }
0/- }
0/.

0/6
else "o*t $$ 0I,a7le to ope, file0;
0//

0/: //fla(s, operator +et'od
0:0
@'ile (li,e)le,(t'()J&0) //@'ile ,ot e,d of 7la,# li,e
0:1 {
0:2 <ta"# fla(s(20);
0:4 <ta"# operators(20);
0:5

0:-
for(i,t i&0; i $ li,e)si!e(); i ++) //reiterate t'ro*(' Kli,eL
0:. {
0:6
"'ar s9+7ol & li,e [i;
0:/
if( is8perator(s9+7ol)) //if t'e s9+7ol is a,
operator
0:: {
100
operators)p*s'(s9+7ol); //p*s' operator
o, sta"#
101
fla(s)p*s'(0); //p*s' asso"iated fla( o,
sta"#
102 }
104
if((s9+7ol J& ' ') MM J
is8perator(s9+7ol)) //t'e, itLs a opera,d
105 {
10-
o*tp*tBi,e +&
s9+7ol; //appe,d
10.
@'ile(fla(s)?8P()) //@'ile top fla( is
8N o, sta"#
106 {
10/
o*tp*tBi,e +& operators)?8P() ; //appe,d
t'e asso"iated op
10:
operators)pop(); //re+ove operator fro+
sta"#
110 fla(s)pop(); //re+ove fla( fro+ sta"#
111 }
112
fla(s)pop(); //set ,ext
fla( 8N
114 fla(s)p*s'(1);
115 }
11- }//e,d of for
11.

116 if(Joperators)e+pt9() ;; (Jfla(s)e+pt9()) )
11/ {
11:
"o*t $$ 0<8=2?OPNF A2N? AD8NF) Pro7
i,"orre"t i,p*t0$$ e,dl;
120 }
121 else
122 {
124
"o*t $$ li,e $$ e,dl;
125
"o*t $$ o*tp*tBi,e $$ e,dl;
12- }
12. } //e,d of @'ile eof
126

12/

12: ofstrea+ o*tfile (0Postfix)o*t0);
140
if (o*tfile)isHope,())
141 {
142 o*tfile $$ o*tp*tBi,e $$ e,dl;
144 o*tfile $$ li,e $$ e,dl;
145

14- +9file)"lose();
14. }
146
else "o*t $$ 0I,a7le to ope, file0;
14/ +9file)"lose();
14: //o*tQile)"lose();
150
ret*r, 0;
151 }