朴素高精度四则运算模板
不考虑负数情况。还包含了高精度和 int 类型的乘除运算。
1 #include <stdio.h> 2 #include <string.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 struct bign { 8 int v[1218]; 9 int len; 10 11 bign() { memset(v, 0, sizeof v), len = 0; } 12 13 void _load(char *num, int beg, int end) 14 { 15 int i, cnt = 0; 16 len = end-beg+1; 17 for (i = end; i >= beg; --i) v[++cnt] = num[i]-'0'; 18 return ; 19 } 20 21 void _print() 22 { 23 for (int i = len; i >= 1; --i) putchar(v[i]+'0'); 24 return ; 25 } 26 27 }; 28 29 bool operator < (bign a, bign b) 30 { 31 if (a.len != b.len) return a.len < b.len; 32 for (int i = a.len; i >= 1; --i) 33 if (a.v[i] != b.v[i]) return a.v[i] < b.v[i]; 34 return false; 35 } 36 37 bool operator == (bign a, bign b) 38 { 39 if (a.len != b.len) return false; 40 for (int i = 1; i <= a.len; ++i) 41 if (a.v[i] != b.v[i]) return false; 42 return true; 43 } 44 45 bool operator <= (bign a, bign b) { return a < b || a == b; } 46 47 bign operator + (bign a, bign b) 48 { 49 bign c; int i; 50 c.len = max(a.len, b.len)+1; 51 for (i = 1; i <= c.len; ++i) 52 c.v[i] = a.v[i]+b.v[i]; 53 for (i = 1; i <= c.len; ++i) 54 if (c.v[i] >= 10) c.v[i] %= 10, ++c.v[i+1]; 55 while (!c.v[c.len] && c.len > 1) --c.len; 56 return c; 57 } 58 59 bign operator - (bign a, bign b) 60 { 61 bign c; int i; 62 c.len = a.len; 63 for (i = 1; i <= c.len; ++i) c.v[i] = a.v[i]-b.v[i]; 64 for (i = 1; i <= c.len; ++i) 65 if (c.v[i] < 0) c.v[i] += 10, --c.v[i+1]; 66 while (!c.v[c.len] && c.len > 1) --c.len; 67 return c; 68 } 69 70 bign operator * (bign a, bign b) 71 { 72 bign c; 73 int i, j; 74 c.len = a.len + b.len; 75 for (i = 1; i <= a.len; ++i) 76 for (j = 1; j <= b.len; ++j) 77 c.v[i+j-1] += a.v[i]*b.v[j]; 78 for (i = 1; i <= c.len; ++i) 79 if (c.v[i] >= 10) c.v[i+1] += c.v[i]/10, c.v[i] %= 10; 80 while (!c.v[c.len] && c.len > 1) --c.len; 81 return c; 82 } 83 84 bign operator / (bign a, bign b) 85 { 86 if (a < b) return a; 87 int t = a.len-b.len, i; bign tmp, c; 88 for (i = 1; i <= b.len; ++i) tmp.v[t+i] = b.v[i]; 89 tmp.len = t+b.len, c.len = t+1; 90 for (i = 0; i < c.len; ++i) { 91 while (tmp < a) a = a-tmp, ++c.v[c.len-i]; 92 for (int j = 1; j < tmp.len; ++j) tmp.v[j] = tmp.v[j+1]; 93 tmp.v[tmp.len--] = 0; 94 } 95 while (!c.v[c.len] && c.len > 1) --c.len; 96 //余数为a 97 return c; 98 } 99 100 bign _mul(bign a, int b) 101 { 102 bign c; int i, d = 0; 103 c.len = a.len; 104 for (i = 1; i <= c.len; ++i) 105 c.v[i] = a.v[i]*b+d, d = c.v[i]/10, c.v[i] %= 10; 106 while (d) c.v[++c.len] = d%10, d /= 10; 107 while (!c.v[c.len] && c.len > 1) --c.len; 108 return c; 109 } 110 111 bign _div(bign a, int b) 112 { 113 bign c; int i, d = 0; 114 c.len = a.len; 115 for (i = c.len; i >= 1; --i) 116 c.v[i] = (d*10+a.v[i])/b, d = (d*10+a.v[i])%b; 117 while (!c.v[c.len] && c.len > 1) --c.len; 118 //余数为d 119 return c; 120 } 121 122 int main() 123 { 124 bign a, b; char input[9000]; 125 scanf("%s", input), a._load(input, 0, strlen(input)-1); 126 scanf("%s", input), b._load(input, 0, strlen(input)-1); 127 (a+b)._print(), putchar('\n'); 128 (a-b)._print(), putchar('\n'); 129 (a*b)._print(), putchar('\n'); 130 (a/b)._print(), putchar('\n'); 131 return 0; 132 }