Big integer addition, subtraction, multiplication, division
# Include <stdio. h> # define maxint 1000int compare (int A [], int B []); int bigplus (int A [], int B [], int C []); int bigsub (int A [], int B [], int C []); int bigmult (int A [], unsigned int B, int C []); int bigmult2 (int A [], int B [], int C []); int bigdiv (int A [], unsigned int B, int C [], int * D); int bigdiv2 (int A [], int B [], int C [], int d []); int main (INT argc, char * argv []) {// A [0], B [0] indicates the number of digits, and a [1], B [1] indicates the bit, similarly, int A [maxint] = {, 1}; // multiplier or divisor int B [maxint] = {, 7 }; // multiplier or divisor int C [maxint], d [maxint]; // C [] storage provider, d [] storage remainder int DIV = 1234; // small multiplier or small divisor int K = 0; int * res = & K; // small remainder integer pointer int I = 0; Int J; for (j = A [0]; j> = 1; j --) printf ("% d", a [J]); // number a printf ("\ n"); For (j = B [0]; j> = 1; j --) // number B printf ("% d ", B [J]); printf ("\ n"); bigplus (A, B, C); bigsub (A, B, C); bigmult (A, Div, c); bigmult2 (A, B, C); bigdiv (A, Div, C, Res); bigdiv 2 (a, B, c, d); getchar (); Return 0;} int compare (int A [], int B []) // compare the size of a large INTEGER {int I; if (a [0]> B [0]) return 1; // compare, the number of digits of B determines the returned value else if (a [0] <B [0]) Return-1; comparison of else // when the number of digits is equal {I = A [0]; while (A [I] = B [I]) // compare I by bit --; if (I = 0) return 0; else if (a [I]> B [I]) return 1; else return-1 ;}} int bigplus (int A [], int B [], int C []) // large integer addition {int I, Len; Len = (a [0]> B [0]? A [0]: B [0]); // A [0] B [0] saves the array length. Len is a longer for (I = 0; I <maxint; I ++) // clear the array 0 C [I] = 0; for (I = 1; I <= Len; I ++) // calculate the value of each bit {C [I] + = (a [I] + B [I]); // printf ("% d, % d, % d \ n ", a [I], B [I], C [I]); If (C [I]> = 10) {c [I]-= 10; // for a single position greater than 10, c [I + 1] ++; // Add 1} If (C [I + 1]> 0) Len ++; C [0] = Len; // C [0] Save the actual length of the result array printf ("Big integers add:"); for (I = Len; I> = 1; I --) printf ("% d", C [I]); // print the result printf ("\ n"); Return 0;} int bigsub (int A [], int B [], Int C []) // subtraction of large integers {int I, Len; Len = (a [0]> B [0]? A [0]: B [0]); // A [0] saves the length of the number. Len is a longer for (I = 0; I <maxint; I ++) // clear the array 0 C [I] = 0; If (compare (a, B) = 0) // compare, B size {printf ("Result: 0"); Return 0;} else if (compare (a, B)> 0) for (I = 1; I <= Len; I ++) // calculate the value of each bit {C [I] + = (a [I]-B [I]); If (C [I] <0) {c [I] + = 10; // Add 10 C [I + 1] In the original position smaller than 0 --; // minus 1} else for (I = 1; I <= Len; I ++) // calculate the value of each bit {C [I] + = (B [I]-A [I]); if (C [I] <0) {C [I] + = 10; // Add 10 C [I + 1] less than 0 in situ --; // high minus 1} while (LEN> 1 & C [Len] = 0) // remove the high zero Len --; C [0] = Len; printf ("Big integers sub ="); if (a [0] <B [0]) printf ("-"); for (I = Len; I> = 1; I --) // print the result printf ("% d", C [I]); printf ("\ n"); Return 0;} int bigmult (int A [], unsigned int B, int C []) // High Precision multiplied by low precision {int Len, I; for (I = 0; I <maxint; I ++) // array clear 0 C [I] = 0; Len = A [0]; for (I = 1; I <= Len; I ++) // calculate every bit {C [I] + = A [I] * B; C [I + 1] + = C [I]/10; c [I] % = 10;} while (C [++ Len]> = 10) // handle high {C [Len + 1] = C [Len]/10; c [Len] % = 10;} If (C [Len] = 0) Len --; // process printf ("Big integrs multi small integer: "); for (I = Len; I> = 1; I --) printf (" % d ", C [I]); printf (" \ n ");} int bigmult2 (int A [], int B [], int C []) // High Precision multiplied by High Precision {int I, j, Len; for (I = 0; I <maxint; I ++) // clear array 0 C [I] = 0; for (I = 1; I <= A [0]; I ++) // multiplier loop for (j = 1; j <= B [0]; j ++) // multiplier loop {C [I + J-1] + = A [I] * B [J]; // accumulate every bit of computation C [I + J] + = C [I + J-1]/10; // accumulate each result to a high C [I + J-1] % = 10; // calculate each bit} Len = A [0] + B [0]; // get the maximum length while (LEN> 1 & C [Len] = 0) // remove the high value 0 Len --; C [0] = Len; printf ("Big integers multi:"); for (I = Len; I> = 1; I --) // print the result printf ("% d ", c [I]); printf ("\ n");} int bigdiv (int A [], unsigned int B, int C [], int * D) // High Precision divided by low precision {// A [] is the multiplier, B is the divisor, C [] is the result, D is the remainder int I, Len; Len = A [0]; // Len is the array length of a [0] for (I = Len; I> = 1; I --) {(* d) = 10 * (* D) + A [I]; // calculate the remainder of each step C [I] = (* D)/B; // calculate the result of each step (* d) = (* D) % B; // modulus remainder} while (LEN> 1 & C [Len] = 0) Len --; // go to the high position 0 printf ("Big integer Div small integer:"); for (I = Len; I> = 1; I --) // print the result printf ("% d", C [I]); printf ("\ tarithmetic compliment: % d", * D ); printf ("\ n");} int bigdiv2 (int A [], int B [], int C [], int d []) // High Precision divided by High Precision {int I, j, Len; If (compare (a, B) <0) // directly print the result {printf ("result: 0 "); printf (" arithmetic compliment: "); for (I = A [0]; I> = 1; I --) printf (" % d ", A [I]); printf ("\ n"); Return-1 ;}for (I = 0; I <maxint; I ++) // clear the quotient and remainder 0 {C [I] = 0; d [I] = 0;} Len = A [0]; d [0] = 0; for (I = Len; I> = 1; I --) // division by phase {for (j = d [0]; j> = 1; j --) d [J + 1] = d [J]; d [1] = A [I]; // high * 10 + ladies and gentlemen d [0] ++; // increase the length of array d by 1 while (compare (D, B)> = 0) // compare D, B size {for (j = 1; j <= d [0]; j ++) // do the subtraction D-B {d [J]-= B [J]; If (d [J] <0) {d [J] + = 10; d [J + 1] -- ;}} while (j> 0 & D [J] = 0) // remove the high value 0 J --; d [0] = J; C [I] ++; // Add 1} J = B [0] to the bid of the business. while (C [J] = 0 & J> 0) j --; // evaluate the length of the quotient array C [0] = J; printf ("Big integers Div result: "); for (I = C [0]; I> = 1; I --) // print vendor printf (" % d ", C [I]); printf ("\ tarithmetic compliment:"); // print the remainder for (I = d [0]; I> = 1; I --) printf ("% d ", d [I]); printf ("\ n ");}