File tree Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Expand file tree Collapse file tree 1 file changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ //Give an input Postfix Expression, write the program to evaluate the given postfix expression using Stack Operations.
3+
4+ //Input : Postfix Expression and List of values of the variables
5+
6+ //Output : Result of the expression
7+
8+ #include <stdio.h>
9+ #include <stdlib.h>
10+ #include <string.h>
11+ int s [20 ],top = -1 ;
12+ void push (int e )
13+ {
14+ top ++ ;
15+ s [top ]= e ;
16+ }
17+ int pop ()
18+ {
19+ int temp = top ;
20+ top -- ;
21+ return s [temp ];
22+ }
23+ int main ()
24+ {
25+ char str [20 ];
26+ int a ,n ,b ,c ;
27+ scanf ("%s" ,str );
28+ for (int i = 0 ;i < strlen (str );i ++ )
29+ {
30+ if (str [i ]>='a' && str [i ]<='z' )
31+ {
32+ scanf ("%d" ,& n );
33+ push (n );
34+ }
35+ else
36+ {
37+ switch (str [i ])
38+ {
39+ case '+' : a = pop ()+ pop ();
40+ push (a );
41+ break ;
42+ case '-' : b = pop ();
43+ c = pop ();
44+ a = c - b ;
45+ push (a );
46+ break ;
47+ case '*' : a = pop ()* pop ();
48+ push (a );
49+ break ;
50+ case '/' : b = pop ();
51+ c = pop ();
52+ a = c /b ;
53+ push (a );
54+ break ;
55+ }
56+ }
57+ }
58+ printf ("%d" ,pop ());
59+ return 0 ;
60+ }
You can’t perform that action at this time.
0 commit comments