-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[BOJ]1918번.cpp
77 lines (69 loc) · 1.21 KB
/
[BOJ]1918번.cpp
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
/*
Problem: [BOJ]1918
Solver: Jinwon
Reference: X
*/
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <stack>
using namespace std;
int priority(char _operator);
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
stack<char> _operator;
string exp, postexp;
int i;
cin >> exp;
for (i = 0; i < exp.size(); i++)
{
if ('A' <= exp[i] && exp[i] <= 'Z')
postexp.push_back(exp[i]);
else if (exp[i] == ')')
{
while (_operator.top() != '(')
{
postexp.push_back(_operator.top());
_operator.pop();
}
_operator.pop();
}
else if (_operator.empty() || exp[i] == '(')
_operator.push(exp[i]);
else if (priority(exp[i]) > priority(_operator.top()))
_operator.push(exp[i]);
else
{
while (!(_operator.empty()) && priority(exp[i]) <= priority(_operator.top()))
{
postexp.push_back(_operator.top());
_operator.pop();
}
_operator.push(exp[i]);
}
}
while (_operator.empty() == false)
{
postexp.push_back(_operator.top());
_operator.pop();
}
cout << postexp;
}
int priority(char _operator)
{
switch (_operator)
{
case '(':
return 0;
case '-':
case '+':
return 1;
case '/':
case '*':
return 2;
}
}