You are on page 1of 3

EVALUATION OF POSTFIX EXPRESSION USING ARRAY IMPLEMENTATION

#include<iostream.h> #include<conio.h> #include<string.h> #include<ctype.h> #include<math.h> #include<stdlib.h> #include"d:\turbocpp\post.h" void main() { int c,d,n,x,i; char p[20]; stk obj; i=0; clrscr(); cout<<"ENTER EXPRESSION"; cin>>p; while(p[i]!='\0') { x=toascii(p[i]); if((x>48) && (x<=57)) { n=x-48; obj.push(n); } else { c=obj.pop(); d=obj.pop(); switch(p[i]) { case '+': obj.push(d+c); break; case '-': obj.push(d-c); break; case '*': obj.push(d*c); break; case '/': obj.push(d/c); break; }

} i++; } cout<<"Result="<<obj.pop(); getch(); }

/*POST.H*/
#include<iostream.h> #include<conio.h> #include<process.h> #define max 20 class stk { public: struct stack { int top; int a[max]; }s; void push(int n) { if(s.top==max) { cout<<"\nStack Full"; } else { s.top++; s.a[s.top]=n; } } int pop() { int t; if(s.top<0) return 0; else t=s.a[s.top]; s.top--; return t; } };

OUTPUT
ENTER EXPRESSION 88+ Result=16 ENTER EXPRESSION 15+ Result=6 ENTER EXPRESSION 77* Result=49 ENTER EXPRESSION 11/ Result=1

You might also like