大数类
1 #include<cstdio> 2 #include<cstring> 3 #include<vector> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 8 struct BigInteger 9 { 10 vector<int> s; 11 int flag;//1 represent +,-1 represent - 12 13 BigInteger(long long num = 0) 14 { 15 *this = num; // 构造函数 16 if(num>=0) 17 flag = 1; 18 else 19 flag = -1; 20 } 21 BigInteger operator = (long long num) // 赋值运算符 long long 22 { 23 s.clear(); 24 if(num>=0) 25 flag = 1; 26 else 27 flag = -1; 28 num = abs(num); 29 do 30 { 31 s.push_back(num % 10); 32 num /= 10; 33 } 34 while(num > 0); 35 return *this; 36 } 37 BigInteger operator = (const string& str) // 赋值运算符 string 38 { 39 s.clear(); 40 int x, len = str.size(); 41 if(str[0]=='-') 42 { 43 flag = -1; 44 len --; 45 } 46 else 47 flag = 1; 48 49 for(int i = 0; i < len; i++) 50 { 51 int end = str.size() - i; 52 sscanf(str.substr(end-1, 1).c_str(), "%d", &x); 53 s.push_back(x); 54 } 55 return *this; 56 } 57 BigInteger operator + (const BigInteger& b) const 58 { 59 BigInteger c; 60 c.flag = 1; 61 c.s.clear(); 62 if(b.flag==-1&&flag==-1) 63 c.flag = -1; 64 else if(flag==-1&&b.flag==1) 65 { 66 BigInteger tmp = *this; 67 tmp.flag = 1; 68 if(tmp>=b) 69 { 70 c = tmp - b; 71 c.flag = -1; 72 } 73 else 74 { 75 c = b - tmp; 76 } 77 return c; 78 } 79 else if(flag==1&&b.flag==-1) 80 { 81 BigInteger tmp = b; 82 tmp.flag = 1; 83 if(tmp>=*this) 84 { 85 c = tmp - *this; 86 c.flag = -1; 87 } 88 else 89 { 90 c = *this - tmp; 91 } 92 return c; 93 } 94 for(int i = 0, g = 0; ; i++) 95 { 96 if(g == 0 && i >= s.size() && i >= b.s.size()) break; 97 int x = g; 98 if(i < s.size()) x += s[i]; 99 if(i < b.s.size()) x += b.s[i]; 100 c.s.push_back(x % 10); 101 g = x / 10; 102 } 103 104 return c; 105 } 106 BigInteger operator - (const BigInteger& b) const 107 { 108 BigInteger c; 109 c.flag = 1; 110 c.s.clear(); 111 if(flag==1 && b.flag==-1) 112 { 113 BigInteger tmp = b; 114 tmp.flag = 1; 115 c = tmp+*this; 116 } 117 else if(flag==-1&&b.flag==1) 118 { 119 BigInteger tmp = *this; 120 tmp.flag = 1; 121 c = tmp+b; 122 c.flag= -1; 123 } 124 else if(flag==1&&b.flag==1) 125 { 126 if(*this<b) 127 { 128 BigInteger tmp1,tmp2; 129 tmp1 = *this; 130 tmp2 = b; 131 c = b-*this; 132 c.flag = -1; 133 } 134 else 135 { 136 for(int i = 0; i < s.size(); i ++) 137 { 138 if(i<b.s.size()) 139 c.s.push_back(s[i] - b.s[i]); 140 else 141 c.s.push_back(s[i]); 142 } 143 for(int i = 0; i < c.s.size(); i ++) 144 { 145 if(c.s[i]<0) 146 { 147 c.s[i] += 10; 148 c.s[i+1] --; 149 } 150 if(i==c.s.size()-1) 151 { 152 int end = i; 153 while(c.s[end--]==0) 154 { 155 c.s.pop_back(); 156 } 157 } 158 } 159 } 160 } 161 else 162 { 163 if(*this<b) 164 { 165 BigInteger tmp1,tmp2; 166 tmp1 = *this; 167 tmp1.flag = 1; 168 tmp2 = b; 169 tmp2.flag = 1; 170 c = tmp1-tmp2; 171 c.flag = -1; 172 } 173 else 174 { 175 BigInteger tmp1,tmp2; 176 tmp1 = *this; 177 tmp1.flag = 1; 178 tmp2 = b; 179 tmp2.flag = 1; 180 c = tmp2-tmp1; 181 } 182 } 183 int zero_test = 1; 184 for(int i = 0; i < c.s.size(); i ++) 185 { 186 if(c.s[i]!=0) 187 { 188 zero_test = 0; 189 break; 190 } 191 } 192 if(zero_test) 193 { 194 c.s.clear(); 195 c.s.push_back(0); 196 } 197 return c; 198 } 199 BigInteger operator * (const BigInteger& b) const 200 { 201 vector<BigInteger> plusList; 202 BigInteger c; 203 c.s.clear(); 204 205 206 if(s.size()>=b.s.size()) 207 { 208 int cnt = 0; 209 210 for(int i = 0; i < b.s.size(); i ++) 211 { 212 BigInteger tmpbigInt; 213 tmpbigInt.flag = 1; 214 tmpbigInt.s.clear(); 215 int digit = 0; 216 for(int k = 0; k < cnt; k ++) 217 { 218 tmpbigInt.s.push_back(0); 219 } 220 cnt ++; 221 for(int j = 0; j < s.size(); j ++) 222 { 223 int res = digit+s[j]*b.s[i]; 224 225 tmpbigInt.s.push_back(res % 10); 226 digit = res/10; 227 if(j==s.size()-1 && digit) 228 tmpbigInt.s.push_back(digit); 229 } 230 plusList.push_back(tmpbigInt); 231 } 232 233 for(auto d:plusList) 234 c += d; 235 } 236 else 237 { 238 239 return b **this; 240 } 241 if(flag==-1 && b.flag||b.flag==-1 && flag) 242 c.flag = -1; 243 return c; 244 } 245 BigInteger operator / (const BigInteger& b) const 246 { 247 BigInteger c; 248 c.s.clear(); 249 c.flag = 1; 250 251 BigInteger input1 = *this; 252 input1.flag = 1; 253 BigInteger input2 = b; 254 input2.flag = 1; 255 BigInteger tmpB = input2; 256 BigInteger zero_num; 257 258 259 if(input1.s.size() < input2.s.size() || (input1.s.size() == input2.s.size() && input1<input2)) 260 { 261 c.s.push_back(0); 262 return c; 263 } 264 else 265 { 266 reverse(input2.s.begin(),input2.s.end()); 267 while(input2.s.size() < input1.s.size()-1) 268 { 269 input2.s.push_back(0); 270 zero_num += 1; 271 } 272 reverse(input2.s.begin(),input2.s.end()); 273 } 274 275 string ttmp = "1"; 276 for(BigInteger i = 0; i < zero_num; i += 1) 277 ttmp += '0'; 278 BigInteger TEN; 279 TEN = ttmp; 280 281 while(input1>=tmpB) 282 { 283 while(input1>=input2) 284 { 285 input1 -= input2; 286 c += TEN; 287 // cout << "OK" << endl; 288 // cout << input1.s[5] << input1.s[4] << input1.s[3] << input1.s[2] << input1.s[1] << input1.s[0] << endl; 289 } 290 // cout << input1.s[5] << endl; 291 reverse(input2.s.begin(),input2.s.end()); 292 input2.s.pop_back(); 293 reverse(input2.s.begin(),input2.s.end()); 294 reverse(TEN.s.begin(),TEN.s.end()); 295 TEN.s.pop_back(); 296 reverse(TEN.s.begin(),TEN.s.end()); 297 } 298 299 if(flag==-1 && b.flag||b.flag==-1 && flag) 300 c.flag = -1; 301 return c; 302 } 303 BigInteger operator % (const BigInteger& b) const 304 { 305 BigInteger c; 306 BigInteger tmpA = *this; 307 BigInteger tmpB = b; 308 309 if(tmpA.flag==-1) 310 c.flag = -1; 311 tmpA.flag = 1; 312 tmpB.flag = 1; 313 314 BigInteger tmp1 = tmpA/tmpB; 315 BigInteger tmp2 = tmp1*tmpB; 316 c = tmpA - tmp2; 317 return c; 318 } 319 BigInteger operator += (const BigInteger& b) 320 { 321 *this = *this + b; 322 return *this; 323 } 324 BigInteger operator -= (const BigInteger& b) 325 { 326 *this = *this - b; 327 return *this; 328 } 329 BigInteger operator *= (const BigInteger& b) 330 { 331 *this = *this * b; 332 return *this; 333 } 334 BigInteger operator /= (const BigInteger& b) 335 { 336 *this = *this / b; 337 return *this; 338 } 339 bool operator < (const BigInteger& b) const 340 { 341 if(flag==-1 && b.flag==1) 342 return true; 343 else if(flag==1 && b.flag==-1) 344 return false; 345 else if(flag==-1 && b.flag==-1) 346 { 347 if(s.size() != b.s.size()) 348 return s.size() > b.s.size(); 349 for(int i = s.size()-1; i >= 0; i --) 350 { 351 if(s[i] != b.s[i]) 352 return s[i] > b.s[i]; 353 } 354 } 355 else 356 { 357 if(s.size() != b.s.size()) 358 return s.size() < b.s.size(); 359 for(int i = s.size()-1; i >= 0; i --) 360 { 361 if(s[i] != b.s[i]) 362 return s[i] < b.s[i]; 363 } 364 } 365 return false; 366 } 367 bool operator > (const BigInteger& b) const 368 { 369 return b < *this; 370 } 371 bool operator <= (const BigInteger& b) const 372 { 373 return !(b < *this); 374 } 375 bool operator >= (const BigInteger& b) const 376 { 377 return !(b > *this); 378 } 379 bool operator != (const BigInteger& b) const 380 { 381 return b < *this || *this < b; 382 } 383 bool operator == (const BigInteger& b) const 384 { 385 return !(b < *this) && !(*this < b); 386 } 387 }; 388 389 ostream& operator << (ostream &out, const BigInteger& x) 390 { 391 if(x.flag==-1) 392 out << '-'; 393 out << x.s.back(); 394 for(int i = x.s.size()-2; i >= 0; i--) 395 { 396 char buf[10]; 397 sprintf(buf, "%d", x.s[i]); 398 for(int j = 0; j < strlen(buf); j++) out << buf[j]; 399 } 400 return out; 401 } 402 403 istream& operator >> (istream &in, BigInteger& x) 404 { 405 string s; 406 if(!(in >> s)) return in; 407 x = s; 408 return in; 409 } 410 411 #include <set> 412 #include <map> 413 set<BigInteger> s; 414 map<BigInteger, int> m; 415 416 int main() 417 { 418 BigInteger a,b,c; 419 420 string testInput = "-888"; 421 a = "6666666666666666666666666666"; 422 b = -777; 423 c = testInput; 424 425 if(a>b) 426 cout << "a>b" << endl; 427 else 428 cout << "a<=b" << endl; 429 cout << "a = " << a << endl; 430 cout << "b = " << b << endl; 431 cout << "c = " << c << endl; 432 433 cout << "a-b = " << a-b << endl; 434 cout << "a-c = " << a-c << endl; 435 cout << "c*b = " << c*b << endl; 436 cout << "b/a = " << b/a << endl; 437 cout << "a%b = " << a%b << endl; 438 439 return 0; 440 }
支持'+','-','*','\'四则运算,支持'>''<'">="'<='==''!='运算符比较,支持'%'取余操作,支持'<<''>>'标准输入输出流,支持'+=','-=','*=','\='运算符,不支持自增自减操作(因为懒),四则运算实现都是模拟十进制手算,未使用任何优化!