重载运算符加减乘除170312
1 #include <cstdio> 2 #include <cstring> 3 //2017.3.12 4 int max(int a,int b){return a >= b ? a : b;} 5 struct bignum{ 6 int data[1001],len; 7 bignum(){len = 1;memset(data,0,sizeof(data));} 8 }; 9 void operator==(bignum &a,int b){ 10 int t,i = 0; 11 int tmp[1001]; 12 while(b > 0){ 13 i++; 14 t = b % 10; 15 tmp[i] = t; 16 b = (b - t) / 10; 17 } 18 for(int j = 1;j <= i;j++){ 19 a.data[j] = tmp[j]; 20 } 21 a.len = i; 22 } 23 bignum operator-(bignum a,bignum b){ 24 bignum c; 25 c.len = max(a.len,b.len); 26 for(int i = 1;i <= c.len;i++){ 27 if(a.data[i] < b.data[i]) 28 a.data[i] += 10,a.data[i + 1]--; 29 c.data[i] = a.data[i] - b.data[i]; 30 } 31 if(c.data[c.len] == 0)c.len--; 32 return c; 33 } 34 void operator-=(bignum &a,bignum b){ 35 a = a - b; 36 } 37 bignum operator/(bignum a,int b){ 38 int d = 0; 39 for(int i = a.len;i >= 1;i--) 40 { 41 d = d * 10 + a.data[i]; 42 a.data[i] = d / b; 43 d = d % b; 44 } 45 while(a.data[a.len] == 0 && a.len > 1) a.len--; 46 return a; 47 } 48 void operator/=(bignum &a,int b){ 49 a = a / b; 50 } 51 bignum operator+(bignum a,bignum b){ 52 bignum c; 53 c.len = max(a.len,b.len); 54 for(int i = 1;i <= c.len;i++){ 55 c.data[i] += a.data[i] + b.data[i]; 56 if(c.data[i] >= 10){ 57 c.data[i] %= 10; 58 c.data[i + 1]++; 59 } 60 } 61 if(c.data[c.len + 1] > 0)c.len++; 62 return c; 63 } 64 void operator+=(bignum &a,bignum b){ 65 a = a + b; 66 } 67 bignum operator*(bignum a,bignum b){ 68 bignum c; 69 c.len = a.len + b.len; 70 for(int i = 1;i <= a.len;i++){ 71 for(int j = 1;j <= b.len;j++){ 72 c.data[i+j-1] += a.data[i] * b.data[j]; 73 if(c.data[i+j-1] >= 10){ 74 c.data[i+j] += c.data[i+j-1] / 10; 75 c.data[i+j-1] %= 10; 76 } 77 } 78 } 79 if(c.data[c.len] == 0)c.len--; 80 return c; 81 } 82 void operator*=(bignum &a,bignum b){ 83 a = a * b; 84 } 85 void get(bignum &a){ 86 char x[10001]; 87 scanf("%s",x + 1);a.len = strlen(x + 1); 88 for(int i = 1;i <= a.len;i++){ 89 a.data[a.len - i + 1] = x[i] - '0'; 90 } 91 } 92 void put(bignum a){ 93 for(int i = a.len;i >= 1;i--) 94 printf("%d",a.data[i]); 95 }