1 /* 2 一般将算数和关系操作符定义为非成员函数,而将赋值操作符定义为成员: 3 4 */ 5 6 7 #include<iostream> 8 #include<string> 9 #include<sstream> 10 #include<cmath> //pow() 11 #include<cstdlib> //system("PAUSE"); 12 #include<cassert> 13 using namespace std; 14 15 class BigNum 16 { 17 public: 18 string s; 19 string str() const; 20 BigNum (); 21 BigNum (const string& n); 22 BigNum (const int& n); 23 BigNum (const char* n); 24 25 friend std::istream& operator >> (std::istream&, BigNum& ); 26 friend std::ostream& operator << (std::ostream&, BigNum& ); 27 28 BigNum& operator = (const string& n); 29 BigNum& operator = (const int& n); 30 BigNum& operator = (const char* n); 31 32 friend BigNum operator + (const BigNum& , const BigNum& ); 33 friend BigNum operator - (const BigNum& , const BigNum& ); 34 friend BigNum operator * (const BigNum& , const BigNum& ); 35 friend BigNum operator / (const BigNum& , const BigNum& ); 36 friend BigNum operator % (const BigNum& , const BigNum& ); 37 38 BigNum& operator += (const BigNum& ); 39 BigNum& operator -= (const BigNum& ); 40 BigNum& operator *= (const BigNum& ); 41 BigNum& operator /= (const BigNum& ); 42 BigNum& operator %= (const BigNum& ); 43 }; 44 //******************************************************* 45 46 string BigNum::str() const 47 { 48 string res(s.rbegin(), s.rend()); 49 if(res == "") 50 res = "0"; 51 return res; 52 } 53 BigNum& BigNum::operator = (const string& n) 54 { 55 s = string(n.rbegin(), n.rend()); 56 return *this; 57 } 58 BigNum& BigNum::operator = (const int& n) 59 { 60 stringstream ss; 61 ss << n; 62 ss >> s; 63 s = string(s.rbegin(), s.rend()); 64 return *this; 65 } 66 BigNum& BigNum::operator = (const char* n) 67 { 68 s = string(n); 69 s = string(s.rbegin(), s.rend());; 70 return *this; 71 } 72 //******************************************************* 73 BigNum::BigNum() 74 { 75 s = ""; 76 } 77 BigNum::BigNum (const string& n) 78 { 79 *this = n; 80 } 81 BigNum::BigNum (const int& n) 82 { 83 *this = n; 84 } 85 BigNum::BigNum(const char* n) 86 { 87 *this = n; 88 } 89 90 //******************************************************* 91 std::istream& operator >> (std::istream& in, BigNum& x) 92 { 93 string s; 94 in >> s; 95 x = s; 96 return in; 97 } 98 string del_pre_zero(const string& a) 99 { 100 int i = 0; 101 for(; a[i] == '0' && i != a.size(); ++i); 102 if(i == a.size()) 103 return "0"; 104 string t(a.begin() + i, a.end()); 105 return t; 106 } 107 std::ostream& operator << (std::ostream& out, BigNum& x) 108 { 109 out << del_pre_zero(x.str()); 110 return out; 111 } 112 //******************************************************* 113 114 bool is_not_small(const string& a, const string& b) 115 { 116 if(a.size() != b.size()) 117 return a.size() > b.size(); 118 return a >= b; 119 } 120 121 122 //******************************************************* 123 124 BigNum operator + (const BigNum& x, const BigNum& y) 125 { 126 BigNum res; 127 string& result = res.s; 128 const string& a = x.s; 129 const string& b = y.s; 130 int i, c; 131 i = c = 0; 132 for(; i < a.size() && i < b.size(); ++i) 133 { 134 c += (a[i] - '0') + (b[i] - '0'); 135 result += (c % 10 + '0'); 136 c /= 10; 137 } 138 while(i < a.size()) 139 { 140 c += (a[i] - '0'); 141 result += (c % 10 + '0'); 142 c /= 10; 143 ++i; 144 } 145 while(i < b.size()) 146 { 147 c += (b[i] - '0'); 148 result += (c % 10 + '0'); 149 c /= 10; 150 ++i; 151 } 152 if(c) 153 result += (c + '0'); 154 return res; 155 } 156 BigNum operator - (const BigNum& x, const BigNum& y) 157 { 158 const string& a = x.s; 159 const string& b = y.s; 160 string x1(a), y1(b); 161 bool flag = true; 162 if(!is_not_small(x1, y1)) 163 { 164 flag = false; 165 string t(a); 166 x1 = y1; 167 y1 = t; 168 } 169 int i, cur, c; 170 i = cur = c = 0; 171 BigNum res; 172 string& result = res.s; 173 for(; i != y1.size(); ++i) 174 { 175 cur = x1[i] - y1[i] + c; 176 (cur < 0) ? (cur += 10, c = -1) : (c = 0); 177 result += (cur + '0'); 178 } 179 while(i < x1.size()) 180 { 181 cur = x1[i] - '0' + c; 182 (cur < 0) ? (cur += 10, c = -1) : (c = 0); 183 result += (cur + '0'); 184 ++i; 185 } 186 if(!flag) 187 result = result + "-"; 188 return res; 189 } 190 191 BigNum operator * (const BigNum& x, const BigNum& y) 192 { 193 if(x.s == "0" || y.s == "0") 194 return "0"; 195 string a(x.s), b(y.s); 196 if(a.size() > b.size()) 197 { 198 string t(a); 199 a = b; 200 b = t; 201 } 202 BigNum res; 203 string& result = res.s; 204 result = "0"; 205 int c; 206 for(int i = 0; i != a.size(); ++i) 207 { 208 string sum(i, '0'); 209 c = 0; 210 for(int j = 0; j != b.size(); ++j) 211 { 212 c += (a[i] - '0') * (b[j] - '0'); 213 sum += (c % 10 + '0'); 214 c /= 10; 215 } 216 while(c) 217 { 218 sum += (c%10 + '0'); 219 c /= 10; 220 } 221 result = (BigNum(string(result.rbegin(), result.rend())) + BigNum(string(sum.rbegin(), sum.rend()))).s; 222 } 223 return res; 224 } 225 226 BigNum operator / (const BigNum& x, const BigNum& y) 227 { 228 assert(y.s != "0"); 229 const string& a = x.s; 230 const string& b = y.s; 231 if(!is_not_small(a, b)) 232 return "0"; 233 if(a == b) 234 return "1"; 235 string x_s(a), y_s; 236 int n, tot; 237 n = tot = 0; 238 while(is_not_small(x_s, b)) 239 { 240 n = x_s.size() - b.size(); 241 if(*(x_s.rbegin()) < *(b.rbegin())) 242 --n; 243 if(n < 0) 244 n == 0; 245 string zero(n, '0'); 246 y_s = zero + b; 247 x_s = (BigNum(string(x_s.rbegin(), x_s.rend())) - BigNum(string(y_s.rbegin(), y_s.rend()))).s; 248 int i = x_s.size() - 1; 249 for(; i >= 0 && x_s[i] == '0'; --i); 250 if(i < 0) 251 x_s = "0"; 252 else 253 x_s = string(x_s.begin(), x_s.begin() + i + 1); 254 tot += pow(10, n); 255 //system("PAUSE"); 256 } 257 stringstream ss; 258 ss << tot; 259 BigNum res; 260 string& result = res.s; 261 ss >> result; 262 result = string(result.rbegin(), result.rend()); 263 return res; 264 } 265 BigNum operator % (const BigNum& x, const BigNum& y) 266 { 267 assert(y.s != "0"); 268 const string& a = x.s; 269 const string& b = y.s; 270 if(!is_not_small(a, b)) 271 return x; 272 if(a == b) 273 return "0"; 274 BigNum res; 275 string& x_s = res.s; 276 x_s = a; 277 string y_s; 278 int n = 0; 279 while(is_not_small(x_s, b)) 280 { 281 n = x_s.size() - b.size(); 282 if(*(x_s.rbegin()) < *(b.rbegin())) 283 --n; 284 string zero(n, '0'); 285 y_s = zero + b; 286 x_s = (BigNum(string(x_s.rbegin(), x_s.rend())) - BigNum(string(y_s.rbegin(), y_s.rend()))).s; 287 int i = x_s.size() - 1; 288 for(; i >= 0 && x_s[i] == '0'; --i); 289 if(i < 0) 290 x_s = "0"; 291 else 292 x_s = string(x_s.begin(), x_s.begin() + i + 1); 293 //system("PAUSE"); 294 } 295 return res; 296 } 297 //******************************************************* 298 299 BigNum& BigNum::operator += (const BigNum& x) 300 { 301 *this = *this + x; 302 return *this; 303 } 304 BigNum& BigNum::operator -= (const BigNum& x) 305 { 306 *this = *this - x; 307 return *this; 308 } 309 BigNum& BigNum::operator *= (const BigNum& x) 310 { 311 *this = *this * x; 312 return *this; 313 } 314 315 BigNum& BigNum::operator /= (const BigNum& x) 316 { 317 *this = *this / x; 318 return *this; 319 } 320 BigNum& BigNum::operator %= (const BigNum& x) 321 { 322 *this = *this % x; 323 return *this; 324 } 325 //******************************************************* 326 int main() 327 { 328 BigNum a, b, result; 329 while(cin >> a >> b) 330 { 331 result = a + b ; 332 cout << result << endl; 333 result = a - b ; 334 cout << result << endl; 335 result = a * b ; 336 cout << result << endl; 337 result = a / b ; 338 cout << result << endl; 339 result = a % b; 340 cout << result << endl; 341 } 342 return 0; 343 }
大数就先告一段落了,其他的就在