Add a large number to a typical C language question, and add a large number to a C language question
Adding a large number to a typical C language question
/* [Program 32] addition of large numbers * question: Calculate the sum of two very large numbers, for example, 123456789123456 + 123456789 * program analysis: and very large, int, long, long, and other data types cannot fit this number. What should I do? Returns an array. */# Include
# Include
// Strlen () memset () # define SIZE 1000 void add_oper (const char num1 [], const char num2 []); void output (int num [], int len ); int main (int argc, char * argv []) {char num1 [SIZE] ={}; char num2 [SIZE] ={}; puts ("Input number1 :"); scanf ("% s", num1); fputs ("Input number2: \ n", stdout); scanf ("% s", num2); add_ut (num1, num2 ); return 0;} // calculate void add_oper (const char num1 [], const char num2 []) {unsigned int len1 = strlen (num1 ); unsigned int len2 = strlen (num2); int result_add [len1 + len2]; memset (result_add, 0, sizeof (result_add )); // convert and add for (int I = len1-1; I> = 0; I --) {result_add [I + len2] + = num1 [I]-'0 ';} for (int j = len2-1; j> = 0; j --) {result_add [j + len1] + = num2 [j]-'0 ';} // carry for (int I = len1 + len2-1; I> = 0; I --) {result_add [I-1] + = result_add [I]/10; result_add [I] % = 10;} output (result_add, len1 + len2);} // output void output (int result [], int len) {printf ("Result ="); int k, n = 0; while (result [n] = 0) {n ++;} for (k = n; k
Result: