-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathykm.java
More file actions
161 lines (144 loc) · 5.11 KB
/
ykm.java
File metadata and controls
161 lines (144 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Stack;
public class Main{
static final int LIMIT = 1000000000;
public static void main(String[] args) throws IOException{
System.setIn(new FileInputStream("input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringBuilder sb = new StringBuilder();
while(true){
String line = br.readLine();
ArrayList<String> commands = new ArrayList<>();
if(line.equals("QUIT")){
break;
}
while(!line.equals("END")){
commands.add(line);
line = br.readLine();
}
int N = Integer.parseInt(br.readLine());
for(int i = 0 ; i<N ; i++){
int input = Integer.parseInt(br.readLine());
String result = execute(commands, input);
sb.append(result+"\n");
}
sb.append("\n");
br.readLine();
}
bw.write(sb.toString().strip());
bw.flush();
bw.close();
br.close();
}
private static String execute(ArrayList<String> commands, int input) {
Stack<Integer> s = new Stack<Integer>();
s.push(input);
for(int i = 0 ; i<commands.size(); i++){
String[] command = commands.get(i).split(" ");
if(command[0].equals("NUM")){
s.push(Integer.parseInt(command[1]));
}else if(command[0].equals("POP")){
if(s.size()>=1){
s.pop();
}else{
return "ERROR";
}
}else if(command[0].equals("INV")){
if(s.size()>=1){
int temp = s.pop();
s.push(-temp);
}else{
return "ERROR";
}
}else if(command[0].equals("DUP")){
s.push(s.peek());
}else if(command[0].equals("SWP")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
s.push(temp1);
s.push(temp2);
}else{
return "ERROR";
}
}else if(command[0].equals("ADD")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
int result = temp1+temp2;
if(result>LIMIT || result<-LIMIT){
return "ERROR";
}
s.push(result);
}else{
return "ERROR";
}
}else if(command[0].equals("SUB")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
int result = temp2-temp1;
if(result>LIMIT || result<-LIMIT){
return "ERROR";
}
s.push(result);
}else{
return "ERROR";
}
}else if(command[0].equals("MUL")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
long result = temp1*temp2;
if(result>LIMIT || result<-LIMIT){
return "ERROR";
}
s.push((int)result);
}else{
return "ERROR";
}
}else if(command[0].equals("DIV")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
if(temp1 == 0){
return "ERROR";
}
int minusCount = 0;
if(temp1<0) minusCount++;
if(temp2<0) minusCount++;
int result = Math.abs(temp2)/Math.abs(temp1);
if(minusCount==1) result *= -1;
s.push(result);
}else{
return "ERROR";
}
}else if(command[0].equals("MOD")){
if(s.size()>=2){
int temp1 = s.pop();
int temp2 = s.pop();
if(temp1 == 0){
return "ERROR";
}
int result = Math.abs(temp2)%Math.abs(temp1);
if(temp2<0) result *= -1;
s.push(result);
}else{
return "ERROR";
}
}else{
// 이곳으로 들어오면 안됨.
return "ERROR";
}
}
if(s.size()!=1) return "ERROR";
return Integer.toString(s.pop());
}
}