大数加减法

Posted starry

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了大数加减法相关的知识,希望对你有一定的参考价值。

  1 #include <cstdio>
  2 #include <cstring>
  3 using namespace std;
  4 #define MAX 10005
  5 
  6 struct bigint
  7 {
  8     char num[MAX];
  9     int flag;  // 1:positive -1:negative
 10     int dgts;
 11     bigint(void)
 12     {
 13         memset(num, 0, sizeof(num));
 14     }
 15 };
 16 
 17 void BigIntTrans(char str[], bigint* a)   //convert string to the structure of bigint
 18 {
 19     int i, k, len;
 20     len = strlen(str);
 21     a->flag = 1;
 22     a->dgts = len;
 23     k = 0;
 24     for(i = len - 1; i >= 1; --i)
 25         a->num[k++] = str[i];
 26     if(str[0] == -) {
 27         a->flag = -1;
 28         --a->dgts;
 29     }
 30     else {
 31         a->num[k++] = str[i];
 32     }
 33 }
 34 
 35 void BigIntPrint(bigint* a)
 36 {
 37     int i;
 38     if(a->flag == -1) printf("-");
 39     if(a->dgts == 1 && a->num[0] == 0) {
 40         printf("0\n");
 41         return;
 42     }
 43     i = a->dgts - 1;
 44     while(a->num[i] == 0)
 45         --i;
 46     for( ; i >= 0; --i) {
 47         printf("%c", a->num[i]);
 48     }
 49     printf("\n");
 50 }
 51 
 52 void add(bigint* a, bigint* b, bigint* ans)
 53 {
 54     int i, c=0, len, tmp;
 55     len = (a->dgts >= b->dgts? a->dgts:b->dgts);
 56     for(i = 0; i < len; ++i) {
 57         tmp = (a->num[i]-0) + (b->num[i]-0) + c;
 58         if(tmp >= 10){
 59             c = 1;
 60             ans->num[i] = (tmp % 10) + 0;
 61         }
 62         else {
 63             c = 0;
 64             ans->num[i] = tmp + 0;
 65         }
 66     }
 67     if(c) {
 68         ans->num[i] = 1;
 69         ans->dgts = len + 1;
 70     }
 71     else ans->dgts = len;
 72     if(a->flag + b->flag == 2) ans->flag = 1;
 73     else ans->flag = -1;
 74 }
 75 
 76 void sub(bigint* a, bigint* b, bigint* ans)
 77 {
 78     int i, c=0, tmp;
 79     for(i = 0; i < a->dgts; ++i) {
 80         tmp = (a->num[i]-0) - (b->num[i]-0) - c;
 81         if(tmp >= 0) {
 82             ans->num[i] = tmp + 0;
 83             c = 0;
 84         }
 85         else {
 86             ans->num[i] = tmp + 10 + 0;
 87             c = 1;
 88         }
 89     }
 90     ans->dgts = a->dgts;
 91     ans->flag = a->flag;
 92 }
 93 
 94 int cmpabs(bigint* a, bigint* b)
 95 {
 96     if(a->dgts > b->dgts)
 97         return 1;
 98     else if(a->dgts < b->dgts)
 99         return 2;
100     else {
101         for(int i = a->dgts - 1; i >= 0; --i) {
102             if(a->num[i] > b->num[i]) return 1;
103             else if(a->num[i] < b->num[i]) return 2;
104             else continue;
105         }
106         return 0;
107     }
108 }
109 
110 void BigIntAdd(bigint* a, bigint* b, bigint* ans)
111 {
112     int flag;
113     if(a->flag * b->flag == 1)
114         add(a, b, ans);
115     else if(a->flag * b->flag == -1) {
116         flag = cmpabs(a, b);
117         if(flag == 1) sub(a, b, ans);
118         else if(flag == 2) sub(b, a, ans);
119         else {
120             ans->dgts = 1;
121             ans->num[0] = 0;
122             ans->flag = 1;
123         }
124     }
125 
126 }
127 
128 char str1[MAX], str2[MAX];
129 
130 int main()
131 {
132     bigint a, b, ans;
133     scanf("%s%s", str1, str2);
134     BigIntTrans(str1, &a);
135     BigIntTrans(str2, &b);
136     sub(&a, &b, &ans);
137     BigIntPrint(&ans);
138     BigIntAdd(&a, &b, &ans);
139     BigIntPrint(&ans);
140     return 0;
141 }

 

以上是关于大数加减法的主要内容,如果未能解决你的问题,请参考以下文章

大数加减法

POJ 2756 Autumn is a Genius 大数加减法

大数加减法模板

算法大数加减

大数减法

大数(高精度)加减乘除