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