大整数类(模板)
Posted lfri
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大整数类(模板)相关的知识,希望对你有一定的参考价值。
1 const int maxn = 5000 + 10; //最大位数 2 3 struct BigInteger 4 { 5 int len, s[maxn]; 6 BigInteger() 7 { 8 memset(s, 0, sizeof(s)); 9 len = 1; 10 } 11 BigInteger(int num) { *this = num; } 12 BigInteger(const char *num) { *this = num; } 13 void clean() { while (len > 1 && !s[len - 1])len--; } 14 BigInteger operator = (const int num) 15 { 16 char s[maxn]; 17 sprintf(s, "%d", num); 18 *this = s; 19 return *this; 20 } 21 BigInteger operator = (const char *num) 22 { 23 len = strlen(num); 24 for (int i = 0; i < len; i++)s[i] = num[len - i - 1] - ‘0‘; 25 return *this; 26 } 27 BigInteger operator + (const BigInteger& b) 28 { 29 BigInteger c; 30 c.len = 0; 31 for (int i = 0, g = 0; g || i < max(len, b.len); i++) 32 { 33 int x = g; 34 if (i < b.len)x += b.s[i]; 35 if (i < len)x += s[i]; 36 c.s[c.len++] = x % 10; 37 g = x / 10; 38 } 39 return c; 40 } 41 BigInteger operator - (const BigInteger& b) 42 { 43 BigInteger c; 44 c.len = 0; 45 for (int i = 0, g = 0; i < len; i++) 46 { 47 int x = s[i] - g; 48 if (i < b.len)x -= b.s[i]; 49 if (x >= 0)g = 0; 50 else { g = 1; x += 10; } 51 c.s[c.len++] = x; 52 } 53 c.clean(); 54 return c; 55 } 56 BigInteger operator * (const BigInteger& b) 57 { 58 BigInteger c; 59 c.len = len + b.len; 60 for (int i = 0; i < len; i++) 61 { 62 for (int j = 0; j < b.len; j++) 63 { 64 c.s[i + j] += s[i] * b.s[j]; 65 } 66 } 67 for (int i = 0; i < c.len; i++) 68 { 69 c.s[i + 1] += c.s[i] / 10; 70 c.s[i] %= 10; 71 } 72 c.clean(); 73 return c; 74 } 75 BigInteger operator * (const int& b) 76 { 77 BigInteger c; 78 c.len = 0; 79 for (int i = 0, g = 0; g || i < len; i++) 80 { 81 int x; 82 if (i < len)x = s[i] * b + g; 83 else x = g; 84 c.s[c.len++] = x % 10; 85 g = x / 10; 86 } 87 return c; 88 } 89 BigInteger operator / (const BigInteger& b) 90 { 91 BigInteger c, f = 0; 92 for (int i = len - 1; i >= 0; i--) 93 { 94 f = f * 10; 95 f.s[0] = s[i]; 96 while (f >= b) 97 { 98 f = f - b; 99 c.s[i]++; 100 } 101 } 102 c.len = len; 103 c.clean(); 104 return c; 105 } 106 BigInteger operator / (const int& b) 107 { 108 BigInteger c, d = *this; 109 c.len = len; 110 for (int i = len - 1, g = 0; i >= 0; i--) 111 { 112 d.s[i] += g * 10; 113 c.s[i] = d.s[i] / b; 114 g = d.s[i] % b; 115 } 116 c.clean(); 117 return c; 118 } 119 BigInteger operator % (const BigInteger& b) 120 { 121 BigInteger c = *this / b; 122 c = *this - c * b; 123 return c; 124 } 125 BigInteger operator += (const BigInteger& b) 126 { 127 *this = *this + b; return *this; 128 } 129 BigInteger operator -= (const BigInteger& b) 130 { 131 *this = *this - b; return *this; 132 } 133 BigInteger operator *= (const BigInteger& b) 134 { 135 *this = *this*b; return *this; 136 } 137 BigInteger operator /= (const BigInteger& b) 138 { 139 *this = *this / b; return *this; 140 } 141 BigInteger operator *= (const int& b) 142 { 143 *this = *this*b; return *this; 144 } 145 BigInteger operator /= (const int& b) 146 { 147 *this = *this / b; return *this; 148 } 149 BigInteger operator %= (const BigInteger& b) 150 { 151 *this = *this%b; return *this; 152 } 153 bool operator < (const BigInteger& b) 154 { 155 if (b.len != len)return len < b.len; 156 for (int i = len - 1; i >= 0; i--) 157 { 158 if (s[i] != b.s[i])return s[i] < b.s[i]; 159 } 160 return 0; 161 } 162 bool operator > (const BigInteger& b) 163 { 164 if (b.len != len)return len > b.len; 165 for (int i = len - 1; i >= 0; i--) 166 { 167 if (s[i] != b.s[i])return s[i] > b.s[i]; 168 } 169 return 0; 170 } 171 bool operator == (const BigInteger& b) 172 { 173 return !(*this > b) && !(*this < b); 174 } 175 bool operator <= (const BigInteger& b) 176 { 177 return !(*this > b); 178 } 179 bool operator >= (const BigInteger& b) 180 { 181 return !(*this < b); 182 } 183 bool operator != (const BigInteger& b) 184 { 185 return !(*this == b); 186 } 187 string str() const 188 { 189 string res; 190 for (int i = 0; i < len; i++) 191 res = char(s[i] + ‘0‘) + res; 192 return res; 193 } 194 }; 195 istream& operator >> (istream&in, BigInteger &x) 196 { 197 string s; 198 in >> s; 199 x = s.c_str(); 200 return in; 201 } 202 ostream& operator << (ostream&out, BigInteger x) 203 { 204 out << x.str(); 205 return out; 206 }
以上是关于大整数类(模板)的主要内容,如果未能解决你的问题,请参考以下文章