1
+ #include < iostream>
2
+ #include < string>
3
+ #include < cstdlib>
4
+ using namespace std ;
5
+
6
+ class BigInt
7
+ {
8
+ private:
9
+ string num;
10
+ char sign;
11
+ public:
12
+ BigInt (string n = " 0" );
13
+ BigInt operator +(const BigInt &bi);
14
+ BigInt operator -(const BigInt &bi);
15
+ BigInt operator *(const BigInt &bi);
16
+ BigInt operator /(const BigInt &bi);
17
+ friend ostream& operator <<(ostream &o, const BigInt &b);
18
+ };
19
+
20
+ BigInt::BigInt (string n)
21
+ {
22
+ if (n[0 ] == ' -' )
23
+ {
24
+ sign = ' -' ;
25
+ num = n.substr (1 , n.length () - 1 );
26
+ }
27
+ else
28
+ {
29
+ sign = ' +' ;
30
+ num = n;
31
+ }
32
+ }
33
+
34
+ BigInt BigInt::operator +(const BigInt &bi)
35
+ {
36
+ string operand1 = num;
37
+ string operand2 = bi.num ;
38
+
39
+ int maxLength = operand1.length () > operand2.length () ? operand1.length () : operand2.length ();
40
+ while (operand2.length () < operand1.length ())
41
+ {
42
+ operand2.insert (0 , " 0" );
43
+ }
44
+ while (operand1.length () < operand2.length ())
45
+ {
46
+ operand1.insert (0 , " 0" );
47
+ }
48
+
49
+ char *result = new char [maxLength + 1 ];
50
+ result[maxLength] = ' \0 ' ;
51
+ int carry = 0 ;
52
+ for (int i = maxLength - 1 ; i >= 0 ; i--)
53
+ {
54
+ int add1 = operand1[i] - ' 0' ;
55
+ int add2 = operand2[i] - ' 0' ;
56
+ int add = (add1 + add2 + carry) % 10 ;
57
+ result[i] = ' 0' + add;
58
+ carry = (add1 + add2 + carry) / 10 ;
59
+ }
60
+
61
+ BigInt tmp (result);
62
+ if (carry)
63
+ {
64
+ tmp.num = " 1" + tmp.num ;
65
+ }
66
+ tmp.sign = sign;
67
+ return tmp;;
68
+ }
69
+
70
+ BigInt BigInt::operator -(const BigInt &bi)
71
+ {
72
+ string operand1 = num;
73
+ string operand2 = bi.num ;
74
+ BigInt tmp;
75
+ if (operand1 == operand2)
76
+ {
77
+ tmp.num = " 0" ;
78
+ tmp.sign = ' +' ;
79
+ return tmp;
80
+ }
81
+
82
+ int maxLength = 0 ;
83
+ char tmpSign = ' +' ;
84
+ if (operand1.length () < operand2.length () || (operand1.length () == operand2.length () && operand1 < operand2))
85
+ {
86
+ tmpSign = ' -' ;
87
+ swap (operand1, operand2);
88
+ }
89
+ maxLength = operand1.length ();
90
+ while (operand2.length () < operand1.length ())
91
+ {
92
+ operand2.insert (0 , " 0" );
93
+ }
94
+
95
+ char *result = new char [maxLength + 1 ];
96
+ result[maxLength] = ' \0 ' ;
97
+ int borrow = 0 ;
98
+ for (int i = maxLength - 1 ; i >= 0 ; i--)
99
+ {
100
+ int minus1 = operand1[i] - ' 0' ;
101
+ int minus2 = operand2[i] - ' 0' ;
102
+ int minus = minus1 - minus2 - borrow;
103
+ if (minus < 0 )
104
+ {
105
+ minus += 10 ;
106
+ borrow = 1 ;
107
+ }
108
+ else
109
+ {
110
+ borrow = 0 ;
111
+ }
112
+ result[i] = ' 0' + minus;
113
+ }
114
+
115
+ tmp.num = result;
116
+ while (tmp.num [0 ] == ' 0' )
117
+ {
118
+ tmp.num .erase (tmp.num .begin ());
119
+ }
120
+ tmp.sign = tmpSign;
121
+ return tmp;
122
+ }
123
+
124
+ BigInt BigInt::operator *(const BigInt &bi)
125
+ {
126
+ string operand1 = num;
127
+ string operand2 = bi.num ;
128
+ BigInt tmp;
129
+ BigInt result;
130
+ result.sign = ' +' ;
131
+
132
+ for (int i = 0 ; i < operand2.length (); i++)
133
+ {
134
+ tmp.num = " 0" ;
135
+ for (int j = 0 ; j < operand2[i] - ' 0' ; j++)
136
+ {
137
+ tmp = tmp + *this ;
138
+ }
139
+ tmp.num .resize (tmp.num .length () + i, ' 0' );
140
+ result = result + tmp;
141
+ }
142
+ return result;
143
+ }
144
+
145
+ BigInt BigInt::operator /(const BigInt &bi)
146
+ {
147
+ string operand1 = num;
148
+ string operand2 = bi.num ;
149
+ BigInt quotient;
150
+ quotient.sign = ' +' ;
151
+ if (operand1.length () < operand2.length ())
152
+ {
153
+ quotient.num = " 0" ;
154
+ return quotient;
155
+ }
156
+
157
+ int m = operand1.length () - operand2.length ();
158
+ operand2.resize (operand1.length (), ' 0' );
159
+ BigInt dividend (operand1);
160
+ BigInt divisor (operand2);
161
+ for (int i = 0 ; i <= m; i++)
162
+ {
163
+ int tmp = 0 ;
164
+ while ((dividend - divisor).sign != ' -' )
165
+ {
166
+ tmp++;
167
+ dividend = dividend - divisor;
168
+ }
169
+ quotient.num += (' 0' + tmp);
170
+ divisor.num .resize (divisor.num .length () - 1 );
171
+ }
172
+ while (quotient.num [0 ] == ' 0' )
173
+ {
174
+ quotient.num .erase (quotient.num .begin ());
175
+ }
176
+ return quotient;
177
+ }
178
+
179
+
180
+
181
+ ostream& operator <<(ostream &o, const BigInt &b)
182
+ {
183
+ if (b.sign == ' +' )
184
+ {
185
+ o << b.num ;
186
+ }
187
+ else if (b.sign == ' -' )
188
+ {
189
+ o << " -" << b.num ;
190
+ }
191
+ return o;
192
+ }
193
+
194
+ int main ()
195
+ {
196
+ string s1, s2;
197
+ char Operator;
198
+ cin >> s1 >> Operator >> s2;
199
+ BigInt a (s1);
200
+ BigInt b (s2);
201
+ BigInt result;
202
+
203
+ switch (Operator)
204
+ {
205
+ case ' +' :
206
+ result = a + b;
207
+ break ;
208
+ case ' -' :
209
+ result = a - b;
210
+ break ;
211
+ case ' *' :
212
+ result = a * b;
213
+ break ;
214
+ case ' /' :
215
+ result = a / b;
216
+ break ;
217
+ }
218
+ cout << result << endl;
219
+ return 0 ;
220
+ }
0 commit comments