File tree Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Expand file tree Collapse file tree 2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change
1
+ package stack ;
2
+
3
+ public class PostCalculator {
4
+
5
+ public int calculate (char [] input ) {
6
+ Stack <Integer > stack = new ListStack <>();
7
+
8
+ for (char tok : input ) {
9
+ if (Character .isDigit (tok )) {
10
+ stack .push (Character .getNumericValue (tok ));
11
+ } else {
12
+ int op2 = stack .pop ();
13
+ int op1 = stack .pop ();
14
+
15
+ switch (tok ) {
16
+ case '+' :
17
+ stack .push (op1 + op2 );
18
+ break ;
19
+ case '-' :
20
+ stack .push (op1 - op2 );
21
+ break ;
22
+ case '*' :
23
+ stack .push (op1 * op2 );
24
+ break ;
25
+ case '/' :
26
+ stack .push (op1 / op2 );
27
+ break ;
28
+ }
29
+ }
30
+ }
31
+
32
+ return stack .pop ();
33
+ }
34
+ }
Original file line number Diff line number Diff line change 1
1
package stack ;
2
2
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+
3
5
import org .junit .jupiter .api .BeforeEach ;
4
6
import org .junit .jupiter .api .DisplayName ;
5
7
import org .junit .jupiter .api .Test ;
You can’t perform that action at this time.
0 commit comments