-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharith.cpp
285 lines (262 loc) · 5.79 KB
/
arith.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "bits/stdc++.h"
#include "stdint.h"
using namespace std;
#define TEN3 1000
#define TEN4 10000
#define TEN5 100000
#define TEN6 1000000
#define TEN7 10000000
#define TEN8 100000000
#define TEN9 1000000000
#define MOD7 1000000007
#define MOD9 1000000009
#define pb push_back
#define mp make_pair
#define MM(a,x) memset(a, x, sizeof(a))
#define P(x) cout<<x<<endl;
#define P2(x,y) cout<<#x<<" = "<<x<<", "<<#y<<" = "<<y<<endl;
#define PV(a,n) for(long i=0;i<n;i++) cout<<#a<<"[" << i <<"] = "<<a[i]<<endl;
#define TM(a,b) cout<<#a<<"->"<<#b<<": "<<1.*(b-a)/CLOCKS_PER_SEC<<"s\n";
#define all(X) (X).begin(), (X).end ()
//#define for_each(it, X) for (__typeof((X).begin()) it = (X).begin(); it != (X).end(); it++)
#define as(i,be,en) assert(i>=be&&i<=en);
#define fore(i, l, r) for(int i = (l); i < (r); ++i)
#define forn(i, n) fore(i, 0, n)
#define fori(i, l, r) fore(i, l, (r) + 1)
#define sz(v) long((v).size())
#define X first
#define Y second
typedef long long li;
typedef long double ld;
typedef unsigned long long LL;
typedef string st;
typedef pair<long, long> pt;
template<typename T> T abs(T a) { return a < 0 ? -a : a; }
template<typename T> T sqr(T a) { return a*a; }
const long INF = (long)1e9;
const ld EPS = 1e-9;
const ld PI = 3.1415926535897932384626433832795;
// end of constants and definitions........................................................
string add(st st_1,st st_2,bool mul)
{
st final="";
int st_1_len = st_1.length(),st_2_len = st_2.length();
int st_len = max(st_1_len,st_2_len);
if(st_1_len == st_len)
{
st_2 = string(st_len - st_2_len,'0')+st_2;
}
else if(st_2_len == st_len)
{
st_1 = string(st_len - st_1_len,'0')+st_1;
}
for(int i=0;i<st_len;i++)
{
int sum = (int)st_1[i]-48+(int)st_2[i]-48;
final.push_back((char)((sum%10)+48));
int it = final.size()-1;
while(it>=0 && sum >9)
{
if(it == 0 && sum > 9)
{
final = string(1,(char)(sum/10+48)) + final;
}
else
{
sum = (int)final[it-1]-48 + (sum/10);
final[it-1] = (char)((sum%10)+48);
}
it--;
}
}
st_1.erase(0,st_1.find_first_not_of('0'));
st_2 = "+"+st_2.substr(st_len-st_2_len,st_len);
st_2_len++;
if (!mul)
{
int space= final.length();
space = max(st_1_len,max(st_2_len,space));
st_1 = string(space-st_1_len,' ')+st_1;
st_2 = string(space-st_2_len,' ')+st_2;
final = string(space-final.length(),' ')+final;
st dash = st(space,'-');
P(st_1)
P(st_2)
P(dash)
P(final)
}
else
{
return final;
}
return "";
}
string sub(st st_1,st st_2)
{
st final="";
int st_1_len = st_1.length(),st_2_len = st_2.length();
st_2 = string(st_1_len - st_2_len,'0')+st_2;
final+=st_1.substr(0,st_1_len-st_2_len);
for (int i = st_1_len - st_2_len; i < st_1_len ; ++i)
{
int diff = (int)st_1[i] - (int)st_2[i];
if(diff < 0)
{
final.push_back((char)(diff+10)+48);
int it = i-1;
while(diff<0 && it>=0)
{
diff = (int)final[it]-48-1;
if(diff>=0)
{
final[it] = ((char)diff+48);
}
else
{
final[it] = ((char)(diff+10)+48);
}
it--;
}
}
else
{
final.push_back((char)diff+48);
}
}
st_2 = "-"+st_2.substr(st_1_len-st_2_len,st_1_len);
st_2_len++;
final.erase(0,final.find_first_not_of('0'));
if(final.length() == 0)
{
final = "0";
}
int space= final.length();
space = max(st_1_len,max(st_2_len,space));
int temp = final.length();
temp = max(st_2_len,temp);
st_1 = string(space-st_1_len,' ')+st_1;
st_2 = string(space-st_2_len,' ')+st_2;
final = string(space-final.length(),' ')+final;
st dash = string(space-temp,' ')+string(temp,'-');
P(st_1)
P(st_2)
P(dash)
P(final)
return "";
}
string mul(st st_1,st st_2)
{
st final = "0";
std::vector<st> multiplied;
int st_1_len = st_1.length(),st_2_len = st_2.length();
for(int i =st_2_len-1;i>=0;i--)
{
st current="";
for(int j=0;j<st_1_len;j++)
{
int pro = ((int)st_1[j]-48)*((int)st_2[i]-48);
current.push_back((char)(pro%10)+48);
int sum = pro;
int it = current.size()-1;
while(it>=0 && sum >9)
{
if(it == 0 && sum > 9)
{
current = string(1,(char)(sum/10+48)) + current;
}
else
{
sum = (int)current[it-1]-48 + (sum/10);
current[it-1] = (char)((sum%10)+48);
}
it--;
}
}
current.erase(0,current.find_first_not_of('0'));
if(current.length() == 0)
{
current = "0";
}
multiplied.push_back(current);
if(current[0] != '0')
{
current = current+string(st_2_len-1-i,'0');
}
final = add(final,current,true);
}
final.erase(0,final.find_first_not_of('0'));
if(final.length() == 0)
{
final = "0";
}
st_2 = "*"+st_2;
st_2_len++;
int space= final.length();
space = max(st_1_len,max(st_2_len,space));
st_1 = string(space-st_1_len,' ')+st_1;
st_2 = string(space-st_2_len,' ')+st_2;
st dash_2 = string(space-final.length(),' ')+string(final.length(),'-');
final = string(space-final.length(),' ')+final;
int temp_len = multiplied[0].length();
st dash_1 = string(max(temp_len,st_2_len),'-');
if (st_2_len == 2)
{
P(st_1)
P(st_2)
P(dash_1)
P(final)
}
else
{
P(st_1)
P(st_2)
dash_1 = string(space-dash_1.length(),' ')+dash_1;
P(dash_1)
forn(j,multiplied.size())
{
cout << string(space-multiplied[j].length()-j,' ');
P(multiplied[j])
}
P(dash_2)
P(final)
}
return "";
}
//main
int main(int argc, char const *argv[])
{
int t;cin>>t;
forn(i,t)
{
st input;cin>>input;
int pos = -1;
if(input.find("+") != -1)
{
pos = input.find("+");
}
else if(input.find("-") != -1)
{
pos = input.find("-");
}
else if(input.find("*") != -1)
{
pos = input.find("*");
}
st opr_1 = input.substr(0,pos);
st opr_2 = input.substr(pos+1,input.length());
if (input[pos] == '+')
{
add(opr_1,opr_2,false);
}
else if (input[pos] == '-')
{
sub(opr_1,opr_2);
}
else if (input[pos] == '*')
{
mul(opr_1,opr_2);
}
cout << endl;
}
return 0;
}