Note: For Addition of large numbers, we need to use strings to solve the problem. The reason for not using an integer array is that the length of an array is hard to be defined if one of the large numbers is 100000. The Code is as follows:
Code:
1 # include <stdio. h> 2 # include <string. h> 3 # define MAX 1000 4 # define zerochar-48 // The asc ii value of 0 characters is 48 5 6/* take the small one in lenga and lengb */7 int maxleng (int, int B) 8 {9 return A> B? A: B; 10} 11 12/* convert the character to a number */13 int transnumber (char c) 14 {15 return C-'0 '; 16} 17 18 int main () 19 {20 char a [Max], B [Max]; 21 memset (A, 0, sizeof (a); 22 memset (B, 0, sizeof (B); 23 while (scanf ("% S % s", a, B )! = EOF) 24 {25 int lenga = strlen (a); 26 int lengb = strlen (B); 27 printf ("lenga = % d \ n", lenga ); 28 printf ("lengb = % d \ n", lengb); 29 30/* sort string a and string B in reverse order */31 int I; 32 For (I = 0; I <(lenga + 1)/2; ++ I) 33 {34 if (I = lengA-1-i) break; // exclude the influence of a common odd number of Large Numbers 35 A [I] = A [I] + A [lengA-1-i]; 36 A [lengA-1-i] = A [I]-A [lengA-1-i]; 37 A [I] = A [I]-A [lengA-1-i]; 38} 39 for (I = 0; I <(lengb + 1)/2; ++ I) 40 {41 if (I = Len GB-1-i) break; // exclude the effect of a total of odd digits of a Large Number 42 B [I] = B [I] + B [lengB-1-i]; 43 B [lengB-1-i] = B [I]-B [lengB-1-i]; 44 B [I] = B [I]-B [lengB-1-i]; 45} 46 47/* addition operation */48 int maxleng = maxleng (lenga, lengb); 49 int C; // indicates carry 50 int psum; 51 int sum [Max]; 52 int an, Bn; 53 for (I = 0, c = 0, psum = 0; I <= maxleng; ++ I) 54 {55 An = transnumber (A [I]); bn = transnumber (B [I]); 56 If (an = zerochar) An = 0; // exclude the influence of 0 characters in initialization; 57 if (BN = zerochar) bn = 0; // exclude the influence of 0 characters in initialization; 58 psum = An + BN + C; 59 sum [I] = psum % 10; 60 C = psum/10; 61} 62 int sumleng = (sum [maxleng]> 0? Maxleng + 1: maxleng); // if the last digit is greater than 0, the total number of digits + 163 64/* print sum [] */65 for (I = sumleng-1; i> = 0; -- I) 66 {67 printf ("% d", sum [I]); 68} 69 printf ("\ n "); 70} 71 return 0; 72}