-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path727-Equation.cpp
167 lines (148 loc) · 4.6 KB
/
727-Equation.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
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
162
163
164
165
166
/*
* FILE: Equation-727.cpp
*
* @author: Md. Arafat Hasan Jenin <arafathasanjenin[at]gmail[dot]com>
*
* LINK:
*
* DATE CREATED: 12-05-17 14:05:23 (+06)
* LAST MODIFIED: 19-05-17 15:07:10 (+06)
*
* DESCRIPTION:
*
* DEVELOPMENT HISTORY:
* Date Version Description
* --------------------------------------------------------------
* 12-05-17 1.0 File Created, Accepted
*
*/
/*
// ___ ___ ___ ___
// / /\ / /\ /__/\ ___ /__/\
// / /:/ / /:/_ \ \:\ / /\ \ \:\
// /__/::\ / /:/ /\ \ \:\ / /:/ \ \:\
// \__\/\:\ / /:/ /:/_ _____\__\:\ /__/::\ _____\__\:\
// \ \:\ /__/:/ /:/ /\ /__/::::::::\ \__\/\:\__ /__/::::::::\
// \__\:\ \ \:\/:/ /:/ \ \:\~~\~~\/ \ \:\/\ \ \:\~~\~~\/
// / /:/ \ \::/ /:/ \ \:\ ~~~ \__\::/ \ \:\ ~~~
// /__/:/ \ \:\/:/ \ \:\ /__/:/ \ \:\
// \__\/ \ \::/ \ \:\ \__\/ \ \:\
// \__\/ \__\/ \__\/
*/
#include <iostream>
#include <climits>
#include <cmath>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iomanip>
#include <utility>
#include <sstream>
#include <algorithm>
#include <stack>
#include <set>
#include <list>
#include <map>
#include <queue>
#include <vector>
typedef long long ll;
typedef double lf;
#define ull unsigned long long
#define FileIn(file) freopen("input.txt", "r", stdin)
#define FileOut(file) freopen("output.txt", "w", stdout)
#define __FastIO ios_base::sync_with_stdio(false); cin.tie(0)
#define FOR(i, a, b) for (int i=a; i<=b; i++)
#define ROF(i, b, a) for (int i=b; i>=a; i--)
#define REP(i, n) for (int i=0; i<n; i++)
#define ALL(ar) ar.begin(), ar.end()
#define Fill(ar, val) memset(ar, val, sizeof(ar))
#define clr(a) memset(a, 0, sizeof a)
#define sfll(a) scanf("%lld", &a)
#define pfll(a) printf("%lld", a)
#define sflf(a) scanf("%lf", &a)
#define pflf(a) printf("%lf", a)
#define sff(a) scanf("%f", &a)
#define pff(a) printf("%f", a)
#define sf(a) scanf("%d", &a)
#define pf(a) printf("%d", a)
#define NL cout << '\n';
#define SP cout << ' ';
#define CHK cout << "##########\n"
#define DEBUG1(x) cout << #x << ": " << x << endl
#define DEBUG2(x, y) cout << #x << ": " << x << "\t" << #y << ": " << y << endl
#define DEBUG3(x, y, z) cout << #x << ": " << x << "\t" << #y << ": " << y << "\t" << #z << ": " << z << endl
#define PB push_back
#define max(a, b) (a < b ? b : a)
#define min(a, b) (a > b ? b : a)
#define sq(a) a * a
#define INF 2147483646
#define MOD 1000000007
#define PI acos(-1.0)
#define MAX 100000000
using namespace std;
/********************* Code starts here ************************/
vector<string> infix_to_postfix(deque<string> v) {
vector<string> P;
stack<string> Stk;
v.push_front("(");
v.push_back(")");
for (int i = 0 ; i < (int)v.size() ; i++) {
//DEBUG1(v[i]);
string tmp = v[i];
/// Case 1 : number
if (tmp[0] >= '0' && tmp[0] <= '9' ) {
P.push_back(tmp);
}
/// Operator
else if (tmp == "+" || tmp == "-" || tmp == "*" || tmp == "/") {
if (tmp == "+" || tmp == "-" ) {
while ( !Stk.empty() && ( Stk.top() == "+" || Stk.top() == "-"
|| Stk.top() == "*" || Stk.top() == "/" ) ) {
P.push_back(Stk.top() );
Stk.pop();
}
} else if (tmp == "*" || tmp == "/" ) {
while ( !Stk.empty() && ( Stk.top() == "*" || Stk.top() == "/" ) ) {
P.push_back(Stk.top() );
Stk.pop();
}
}
Stk.push(tmp);
} else {
/// Opening
if (tmp == "(") Stk.push(tmp);
else { /// )
while (Stk.top() != "(") {
P.push_back(Stk.top() );
Stk.pop();
}
if (!Stk.empty()) Stk.pop();
}
}
/// Par
}
return P;
}
int main() {
int T;
string str;
deque<string> v;
vector<string> P;
cin >> T;
cin.ignore();
cin.ignore();
while (T--) {
while (getline(cin, str) && (str != "\n" && str != ""))
v.push_back(str);
P = infix_to_postfix(v);
for (int i = 0; i < (int)P.size(); i++) {
cout << P[i];
}
cout << '\n';
if (T) cout << '\n';
v.clear();
P.clear();
}
return 0;
}