Skip to content

Commit 40dc60d

Browse files
committed
Add day29
1 parent c11f092 commit 40dc60d

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed

Jongeun/Day29/3425_고스택.cpp

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
4+
5+
#define MaxV 1000000000
6+
7+
int main(int argc, char **argv)
8+
{
9+
ios::sync_with_stdio(false);
10+
cin.tie(0);
11+
12+
string temp;
13+
cin >> temp;
14+
while (temp != "QUIT")
15+
{
16+
17+
vector<pair<string, int>> program;
18+
while (temp != "END")
19+
{
20+
if (temp == "NUM")
21+
{
22+
int num;
23+
cin >> num;
24+
program.push_back({temp, num});
25+
}
26+
else
27+
{
28+
program.push_back({temp, 0});
29+
}
30+
cin >> temp;
31+
}
32+
33+
int N;
34+
cin >> N;
35+
36+
for (int i = 0; i < N; i++) // Run each program
37+
{
38+
int num;
39+
cin >> num;
40+
vector<int> stack;
41+
stack.push_back(num);
42+
string result{};
43+
bool error = false;
44+
for (auto it : program)
45+
{
46+
if (it.first == "NUM")
47+
{
48+
stack.push_back(it.second);
49+
}
50+
else if (it.first == "POP")
51+
{
52+
if (stack.empty())
53+
{
54+
error = true;
55+
break;
56+
}
57+
stack.pop_back();
58+
}
59+
else if (it.first == "INV")
60+
{
61+
if (stack.empty())
62+
{
63+
error = true;
64+
break;
65+
}
66+
int temp = stack.back();
67+
temp = -temp;
68+
stack.pop_back();
69+
stack.push_back(temp);
70+
}
71+
else if (it.first == "DUP")
72+
{
73+
if (stack.empty())
74+
{
75+
error = true;
76+
break;
77+
}
78+
int temp = stack.back();
79+
stack.push_back(temp);
80+
}
81+
else if (it.first == "SWP")
82+
{
83+
if (stack.size() < 2)
84+
{
85+
error = true;
86+
break;
87+
}
88+
int first = stack.back();
89+
stack.pop_back();
90+
int second = stack.back();
91+
stack.pop_back();
92+
93+
stack.push_back(first);
94+
stack.push_back(second);
95+
}
96+
else
97+
{
98+
if (stack.size() < 2)
99+
{
100+
error = true;
101+
break;
102+
}
103+
long long first = stack.back();
104+
stack.pop_back();
105+
long long second = stack.back();
106+
stack.pop_back();
107+
long long temp;
108+
109+
if (it.first == "ADD")
110+
{
111+
temp = (long long)first + second;
112+
}
113+
else if (it.first == "SUB")
114+
{
115+
temp = second - first;
116+
}
117+
else if (it.first == "MUL")
118+
{
119+
temp = first * second;
120+
}
121+
else if (it.first == "DIV")
122+
{
123+
if (first == 0)
124+
{
125+
error = true;
126+
break;
127+
}
128+
129+
int minusCnt = (first < 0) + (second < 0);
130+
temp = abs(second) / abs(first) * (minusCnt == 1 ? -1 : 1);
131+
}
132+
else if (it.first == "MOD")
133+
{
134+
if (first == 0)
135+
{
136+
error = true;
137+
break;
138+
}
139+
int minusCnt = (second < 0);
140+
temp = abs(second) % abs(first) * (minusCnt == 1 ? -1 : 1);
141+
}
142+
143+
if (abs(temp) > MaxV)
144+
{
145+
error = true;
146+
break;
147+
}
148+
else
149+
{
150+
stack.push_back(temp);
151+
}
152+
}
153+
}
154+
if (error || stack.size() != 1)
155+
{
156+
cout << "ERROR" << '\n';
157+
}
158+
else
159+
{
160+
cout << stack.back() << '\n';
161+
}
162+
}
163+
164+
cout << '\n';
165+
cin >> temp;
166+
}
167+
168+
return 0;
169+
}

0 commit comments

Comments
 (0)