File tree Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Expand file tree Collapse file tree 1 file changed +51
-0
lines changed Original file line number Diff line number Diff line change
1
+ int evalRPN (char * * tokens , int tokensSize ){
2
+ long int stk [tokensSize ];
3
+
4
+ int stkIndex = -1 ;
5
+
6
+ for (int i = 0 ; i < tokensSize ; i ++ )
7
+ {
8
+ if (strcmp (tokens [i ], "+" ) == 0 )
9
+ {
10
+ int first = stk [stkIndex ];
11
+ stkIndex -- ;
12
+ int second = stk [stkIndex ];
13
+
14
+ stk [stkIndex ] = first + second ;
15
+ }
16
+ else if (strcmp (tokens [i ], "-" ) == 0 )
17
+ {
18
+ int first = stk [stkIndex ];
19
+ stkIndex -- ;
20
+ int second = stk [stkIndex ];
21
+
22
+ stk [stkIndex ] = second - first ;
23
+ }
24
+ else if (strcmp (tokens [i ], "*" ) == 0 )
25
+ {
26
+ long first = stk [stkIndex ];
27
+ stkIndex -- ;
28
+ int second = stk [stkIndex ];
29
+
30
+ stk [stkIndex ] = first * second ;
31
+
32
+ }
33
+ else if (strcmp (tokens [i ], "/" ) == 0 )
34
+ {
35
+ int first = stk [stkIndex ];
36
+ stkIndex -- ;
37
+ int second = stk [stkIndex ];
38
+
39
+ stk [stkIndex ] = second / first ;
40
+ }
41
+ else
42
+ {
43
+ stkIndex ++ ;
44
+ stk [stkIndex ] = atoi (tokens [i ]);
45
+ }
46
+
47
+ }
48
+
49
+
50
+ return stk [stkIndex ];
51
+ }
You can’t perform that action at this time.
0 commit comments