-
Notifications
You must be signed in to change notification settings - Fork 1
/
ARHN04.cpp
111 lines (92 loc) · 1.99 KB
/
ARHN04.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
#include <bits/stdc++.h>
#define ll int
#define get(a) cin >> a;
#define rep(n) for( ll i = 1; i <= n; i++ )
#define repVector(v) for( vector<ll>::iterator it = v.begin(); it != v.end(); it++ )
#define repSet(s) for( set<string>::iterator it = s.begin(); it != s.end(); it++ )
#define all(c) c.begin(), c.end()
#define pb push_back
#define absol(a) ( ( a >= 0 ) ? a : -a )
#define FOR(i,a,b) for( ll i = a; i <= b; i++ )
#define matrix vector< vector<ll> >
#define MOD 1000000007
#define F first
#define S second
#define mp make_pair
#define L 2*r
#define R 2*r+1
#define INPFILE freopen("input.in","r",stdin)
#define INF LONG_MAX
using namespace std;
bool compare( ll a[], ll b [] ) {
for( ll i = 200; i >= 0; i-- )
if( a[i] > b[i] )
return 1;
else if( b[i] > a[i] )
return 0;
return 1;
}
int c[300] = {0};
void add( ll a[], ll b[] ) {
int carry = 0;
FOR(i,0,205) {
c[i] = ( a[i] + b[i] + carry )%10;
carry = ( a[i] + b[i] + carry )/10;
}
}
void sub( ll a[], ll b[] ) {
FOR(i,0,205)
if( a[i] >= b[i] )
{ c[i] = a[i] - b[i]; }
else
{ a[i] += 10; a[i+1]--; c[i] = a[i] - b[i]; }
}
void display() {
bool flag = 0;
for( ll i = 205; i >= 0; i-- ) {
if( c[i] != 0 )
flag = 1;
if( flag )
printf("%d", c[i]);
}
if(!flag)
printf("0");
}
int main() {
// INPFILE;
ll t; get(t);
while(t--) {
string s;
cin >> s;
vector<int> a, b;
int aa[300] = {0}, bb[300] = {0};
char op;
ll i = 0;
for( ; i < s.size(); i++ )
if( s[i] == '-' )
{ op = '-'; i++; break; }
else if( s[i] == '+' )
{ op = '+'; i++; break; }
else if( isdigit(s[i]) )
a.pb(s[i]-'0');
for( ; i < s.size(); i++ )
b.pb(s[i]-'0');
reverse( all(a) );
reverse( all(b) );
FOR(i,0,a.size()-1)
aa[i] = a[i];
FOR(i,0,b.size()-1)
bb[i] = b[i];
memset( c, 0, sizeof c );
if( op == '-' ) {
if( compare( aa, bb ) )
sub( aa, bb );
else
{ sub( bb, aa ); printf("-"); }
}
else
add(aa,bb);
display();
printf("\n");
}
}