Addition of the number of characters in the super-long digits:
Test Cases: 112233445566778899 + 998877665544332211 = 1111111111111111110
Program code:
1 # include <stdio. h> 2 # include <string. h> 3 # include <malloc. h> 4 # define maxlen 100 5 6 void add (char * a, char * B, char * C) 7 {8 int I, J; 9 int SA = strlen (a); 10 int sb = strlen (B); 11 int max = Sa> Sb? Sa: Sb; 12 INT * s = (int *) malloc (sizeof (INT) * (MAX + 1); // to ensure that the operation and, it should be the number of digits of the longest operand + 1, range: [0, Max]; 13 int * A = (int *) malloc (sizeof (INT) * max ); 14 int * B = (int *) malloc (sizeof (INT) * max); 15 16 for (I = 0; I <Max; I ++) 17 A [I] = B [I] = s [I] = 0; // initialize to 0 first, prevents problems caused by the absence of corresponding bits when adding high positions 18 s [Max] = 0; 19 20 for (I = 0; I <SA; I ++) // put a upside down to add 21 A [I] = A [Sa-I-1]-'0'; 22 23 for (I = 0; I <Sb; I ++) 24 B [I] = B [SB-I-1]-'0 '; 25 26 for (I = 0; I <Max; I ++) 27 s [I] = A [I] + B [I]; 28 29 for (I = 0; I <Max; I ++) // centralized handling of carry problem 30 {31 if (s [I]> = 10) // If I = max-1 has carry, s [Max]! = 032 {33 s [I + 1] + = s [I]/10; 34 S [I] % = 10; 35} 36} 37 38 If (s [Max]! = 0) // The maximum bit-wise data range is [0, Max] 39 {40 for (j = 0; j <= max; j ++) 41 {42 C [J] = s [Max-J] + '0'; 43} 44 C [Max + 1] = '\ 0 '; 45} 46 else // The highest bit without carry, data range: [0, max-1] 47 {48 for (j = 0; j <Max; j ++) 49 {50 C [J] = s [Max-1-J] + '0'; 51} 52 c [Max] = '\ 0 '; 53} 54} 55 56 57 int main () 58 {59 char a [maxlen]; 60 char B [maxlen]; 61 char C [2 * maxlen]; 62 while (scanf ("% S + % s", a, B )! = EOF) 63 {64 add (A, B, C); 65 printf ("% S + % s =", a, B); 66 puts (C ); 67} 68 return 0; 69}
Multiplication of the number of characters in a long digit:
Test Case: 112233445566778899*998877665544332211 = 112107482103740987777903741240815689
Program code:
1 # include <stdio. h> 2 # include <string. h> 3 # include <malloc. h> 4 # define maxlen 100 5 6 void multiply (char * a, char * B, char * C) 7 {8 int I, j, CA, CB, * s; 9 CA = strlen (a); // The number of digits of the operand 10 cb = strlen (B); // The number of digits of the B operand 11 S = (int *) malloc (sizeof (INT) * (Ca + CB); // s points to the space that can store A and B 12 for (I = 0; I <Ca + CB; I ++) 13 s [I] = 0; // the elements in the initialized s array are all 014 for (I = 0; I <CA; I ++) 15 For (j = 0; j <CB; j ++) 16 s [I + J + 1] + = (a [I]-'0 ') * (B [J]-'0'); 1 7 18 for (I = Ca + CB-1; I> = 0; I --) 19 if (s [I]> = 10) 20 {21 s [I-1] + = s [I]/10; // high plus low carry 22 s [I] % = 10; 23} 24 I = 0; 25 while (s [I] = 0) 26 I ++; 27 for (j = 0; I <Ca + CB; I ++, J ++) 28 C [J] = s [I] + '0'; 29 C [J] = '\ 0'; 30 free (s ); 31} 32 33 34 int main () 35 {36 char a [maxlen]; 37 char B [maxlen]; 38 char C [2 * maxlen]; 39 while (scanf ("% S * % s", a, B )! = EOF) 40 {41 multiply (A, B, C); 42 printf ("% S * % s =", a, B); 43 puts (C ); 44} 45 return 0; 46}
Addition and multiplication of the number of characters in a very long Digit