File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ * Author: Minho Kim (ISKU)
3
+ * Date: November 13, 2019
4
+ * E-mail: minho.kim093@gmail.com
5
+ *
6
+ * https://github.com/ISKU/Algorithm
7
+ * https://www.acmicpc.net/problem/1918
8
+ */
9
+
10
+ import java .util .*;
11
+ import java .io .*;
12
+
13
+ public class Main {
14
+ public static void main (String [] args ) throws Exception {
15
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
16
+ char [] line = br .readLine ().toCharArray ();
17
+
18
+ Stack <Character > stack = new Stack <>();
19
+ for (char c : line ) {
20
+ if (c == '(' ) {
21
+ stack .push (c );
22
+ } else if (c == ')' ) {
23
+ while (!stack .isEmpty () && stack .peek () != '(' )
24
+ System .out .print (stack .pop ());
25
+ stack .pop ();
26
+ } else if (c == '*' || c == '/' ) {
27
+ while (!stack .isEmpty () && getPriority (stack .peek ()) >= 3 )
28
+ System .out .print (stack .pop ());
29
+ stack .push (c );
30
+ } else if (c == '+' || c == '-' ) {
31
+ while (!stack .isEmpty () && getPriority (stack .peek ()) >= 2 )
32
+ System .out .print (stack .pop ());
33
+ stack .push (c );
34
+ } else {
35
+ System .out .print (c );
36
+ }
37
+ }
38
+ while (!stack .isEmpty ())
39
+ System .out .print (stack .pop ());
40
+ }
41
+
42
+ private static int getPriority (char c ) {
43
+ switch (c ) {
44
+ case '*' :
45
+ case '/' :
46
+ return 3 ;
47
+ case '+' :
48
+ case '-' :
49
+ return 2 ;
50
+ default :
51
+ return 1 ;
52
+ }
53
+ }
54
+ }
You can’t perform that action at this time.
0 commit comments