最终版本的带符号的和无符号的高精度类:(将来应该不会再更新)
支持高精度加、减、乘、除、模。
2019-10-23 更新:更正错误
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 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | class bigInt { /* This class is capable of calculating big non-negative numbers of any length. You must include header files **iostream**,**algorithm**,**cstdio** and **vector** before using this class! */ typedef vector<int> Container; protected: Container v; //Every Digit is stored in this vector in the reverse order static Container& bIsort(Container& v) { for (int i = 0; i < (int)v.size(); i++) { if (v[i] > 9) { if (i == (int)v.size() - 1)v.push_back(v[i] / 10); else v[i + 1] += v[i] / 10; v[i] %= 10; } } return v; } static Container lng2vec(unsigned long long v) { Container c; while (v) { c.push_back(v % 10); v /= 10; } return c; } static Container& trimPre0(Container& v) { int cnt = 0; int i = v.size() - 1; while (i >= 0 && v[i] == 0)i--, cnt++; v.resize(v.size() - cnt); if (v.size() == 0)v.push_back(0); return v; } static Container easyDiv(bigInt& bI1, const bigInt& bI2) { unsigned long long cnt = 0; while (bI1 >= bI2) { bI1 -= bI2; cnt++; } return lng2vec(cnt); } public: bigInt() { v.clear(); v.push_back(0); } bigInt(long long n) { setV(n); } bigInt(const Container& c) { setV(c); } bigInt(const string& s) { setV(s); } void setV(long long n) { v = lng2vec(n); trimPre0(v); } void setV(const Container& c) { v = c; trimPre0(v); } void setV(const string& s) { v.clear(); //2019-10-23更正错误 for (int i = s.size() - 1; i >= 0; i--)v.push_back(s[i] - '0'); trimPre0(v); } bigInt& operator = (const bigInt& bI) { this->v = bI.v; return *this; } bool operator < (const bigInt& bI2) const { if (v.size() != bI2.v.size())return v.size() < bI2.v.size(); for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != bI2.v[i])return v[i] < bI2.v[i]; } return false; } bool operator > (const bigInt& bI2) const { if (v.size() != bI2.v.size())return v.size() > bI2.v.size(); for (int i = v.size() - 1; i >= 0; i--) { if (v[i] != bI2.v[i])return v[i] > bI2.v[i]; } return false; } bool operator <= (const bigInt& bI2) const { return (*this) < bI2 || (*this) == bI2; } bool operator >= (const bigInt& bI2) const { return (*this) > bI2 || (*this) == bI2; } bool operator == (const bigInt& bI2) const { return v == bI2.v; } bigInt& operator += (const bigInt& bI2) { Container& s1 = v; const Container& s2 = bI2.v; if (s1.size() < s2.size())s1.resize(s2.size()); int s1Len = s1.size(), s2Len = s2.size(); for (int i = 0; i < s2Len; i++)s1[i] += s2[i]; for (int i = 0; i < s1Len - 1; i++)if (s1[i] > 9) { s1[i + 1] += s1[i] / 10; s1[i] %= 10; } if (s1[s1Len - 1] > 9) { s1.push_back(s1[s1Len - 1] / 10); s1[s1Len - 1] %= 10; } return *this; } bigInt operator + (const bigInt& bI2) const { bigInt bI1 = *this; bI1 += bI2; return bI1; } bigInt& operator -= (const bigInt& bI2) { Container& s1 = v; const Container& s2 = bI2.v; bool flag = false; if ((*this) < bI2)flag = true, s1.resize(s2.size()); int s1Len = s1.size(), s2Len = s2.size(); for (int i = 0; i < s2Len; i++)s1[i] -= s2[i]; if (flag)for (int i = 0; i < s2Len; i++)s1[i] = -s1[i]; for (int i = 0; i < s1Len - 1; i++)if (s1[i] < 0) { s1[i + 1] -= 1; s1[i] += 10; } trimPre0(s1); return *this; } bigInt operator - (const bigInt& bI2) const { bigInt bI1 = *this; bI1 -= bI2; return bI1; } bigInt operator * (const bigInt& bI2) const { const Container& v1 = v; const Container& v2 = bI2.v; Container v3; v3.resize(v1.size() + v2.size(), 0); for (int i = 0; i < (int)v2.size(); i++) { for (int j = 0; j < (int)v1.size(); j++) { v3[i + j] += v2[i] * v1[j]; } } bIsort(v3); return bigInt(trimPre0(v3)); } bigInt& operator *= (const bigInt& bI2) { *this = (*this) * bI2; return *this; } bigInt operator / (const unsigned long long& bI2) const { const Container& v1 = v; long long v2 = bI2; Container v3; long long div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2) { v3.push_back(0); continue; } Container tmp = lng2vec(div / v2); v3.insert(v3.end(), tmp.begin(), tmp.end()); div %= v2; } reverse(v3.begin(), v3.end()); return bigInt(trimPre0(v3)); } unsigned long long operator % (const unsigned long long& bI2) const { const Container& v1 = v; unsigned long long v2 = bI2; unsigned long long div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2)continue; div %= v2; } return div % v2; } bigInt operator / (const bigInt& bI2) const { const Container& v1 = v; const Container& v2 = bI2.v; Container v3; bigInt div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2) { v3.push_back(0); continue; } Container tmp = easyDiv(div, v2); v3.insert(v3.end(), tmp.begin(), tmp.end()); } reverse(v3.begin(), v3.end()); return bigInt(trimPre0(v3)); } bigInt& operator /= (const bigInt& bI2) { *this = (*this) / bI2; return *this; } bigInt operator % (const bigInt& bI2) const { const Container& v1 = v; const Container& v2 = bI2.v; bigInt div = 0; for (int i = v1.size() - 1; i >= 0; i--) { div *= 10; div += v1[i]; if (div < v2)continue; easyDiv(div, v2); } return div; } bigInt& operator %= (const bigInt& bI2) { *this = (*this) % bI2; return *this; } friend istream& operator >> (istream& in, bigInt& bI) { string s; in >> s; bI.v.clear(); for (int i = s.size() - 1; i >= 0; i--) bI.v.push_back(s[i] - '0'); trimPre0(bI.v); return in; } friend ostream& operator << (ostream& out, bigInt& bI) { for (int i = bI.v.size() - 1; i >= 0; i--) out << (char)(bI.v[i] + '0'); return out; } void scan() { v.clear(); scanf(" "); char c; do { c = getchar(); if (c <= '9' && c >= '0')v.push_back(c - '0'); else { ungetc(c, stdin); break; } } while (1); reverse(v.begin(), v.end()); trimPre0(v); } void print() { for (int i = v.size() - 1; i >= 0; i--) { printf("%d", v[i]); } } }; class sBigInt { /* This class is capable of calculating big signed numbers of any length. The "sBigInt" class is dependent on the "bigInt" class. */ protected: bigInt bI; bool sign; //T:Negetive,F:Positive public: sBigInt() { bI.setV(0); sign = false; } sBigInt(long long n) { if (n < 0) { sign = true; n = -n; } else sign = false; bI.setV(n); } sBigInt(string s) { if (s[0] == '+' || s[0] == '-') { sign = (s[0] == '-'); s.erase(s.begin()); } bI.setV(s); } sBigInt& operator = (const sBigInt& bI) { this->bI = bI.bI; this->sign = bI.sign; return *this; } bool operator < (const sBigInt& bI2) const { if (this->sign != bI2.sign)return this->sign; if (this->sign)return this->bI > bI2.bI; else return this->bI < bI2.bI; } bool operator > (const sBigInt& bI2) const { if (this->sign != bI2.sign)return !(this->sign); if (this->sign)return this->bI < bI2.bI; else return this->bI > bI2.bI; } bool operator == (const sBigInt& bI2) const { return this->sign == bI2.sign && this->bI == bI2.bI; } bool operator <= (const sBigInt& bI2) const { return (*this) < bI2 || (*this) == bI2; } bool operator >= (const sBigInt& bI2) const { return (*this) > bI2 || (*this) == bI2; } sBigInt operator + (const sBigInt& bI2) const { sBigInt result(0); if (this->sign == bI2.sign) { result.sign = this->sign; result.bI = this->bI + bI2.bI; return result; } result.bI = this->bI - bI2.bI; result.sign = ((this->bI) > (bI2.bI)) ? this->sign : bI2.sign; return result; } sBigInt operator - (const sBigInt& bI2) const { sBigInt result(0); if (this->sign != bI2.sign) { result.sign = this->sign; result.bI = this->bI + bI2.bI; return result; } result.bI = this->bI - bI2.bI; bool flag1 = this->sign, flag2 = ((this->bI) >= (bI2.bI)); //2019-10-23更正错误 result.sign = (flag1 && flag2 || !flag1 && !flag2); return result; } sBigInt operator * (const sBigInt& bI2) const { sBigInt result(0); result.sign = (this->sign) ^ (bI2.sign); result.bI = (this->bI) * (bI2.bI); return result; } sBigInt operator / (const sBigInt& bI2) const { sBigInt result(0); result.sign = (this->sign) ^ (bI2.sign); result.bI = (this->bI) / (bI2.bI); return result; } sBigInt operator % (const sBigInt& bI2) const { sBigInt result=*this-bI2*(*this/bI2); //2019-10-23更正错误 return result; } sBigInt& operator += (const sBigInt& bI2) { *this = (*this) + bI2; return *this; } sBigInt& operator -= (const sBigInt& bI2) { *this = (*this) - bI2; return *this; } sBigInt& operator *= (const sBigInt& bI2) { *this = (*this) * bI2; return *this; } sBigInt& operator /= (const sBigInt& bI2) { *this = (*this) / bI2; return *this; } sBigInt& operator %= (const sBigInt& bI2) { *this = (*this) % bI2; return *this; } friend istream& operator >> (istream& in, sBigInt& bI) { bI.sign = false; do { char c = in.peek(); if (c <= '9' && c >= '0')break; else if (c == '-')bI.sign ^= true; in.get(); } while (1); in >> (bI.bI); if (bI.sign && bI.bI == bigInt(0))bI.sign = false; return in; } friend ostream& operator << (ostream& out, sBigInt& bI) { if (bI.sign)out << "-"; out << (bI.bI); return out; } void scan() { this->sign = false; do { char c = getchar(); if (c <= '9' && c >= '0') { ungetc(c, stdin); break; } else if (c == '-')this->sign ^= true; } while (1); bI.scan(); if (this->sign && this->bI == bigInt(0))this->sign = false; } void print() { if (this->sign)printf("-"); bI.print(); } }; |