Two large numbers are added by a string array
1#include <stdio.h>2#include <stdlib.h>3#include <string.h>4 5 //maximum number of digits in large numbers6 #defineMAX 1007 8 //and the maximum number of digits9 #defineN 101Ten One intMainintargcChar*argv[]) A { - intI//Working pointer - intNUM1, num2, num3;//records the number of digits of a, B, two, and the number of digits after the sum of two numbers the intc =0;//Record Rounding - intSum//record the sum of a bit - - CharTemp[n];//the sum of the records added + //allocating memory dynamically for two numbers - Char*a = (Char*)malloc(max*sizeof(Char)); + Char*b = (Char*)malloc(max*sizeof(Char)); A atprintf"Enter the first number:\n"); -scanf"%s", a); -printf"Enter the second number:\n"); -scanf"%s", b); - -NUM1 =strlen (a); innum2 =strlen (b); - to //a higher number of record digits + if(Num1 <num2) - { the Char*p; * inttemp; $p =A;Panax NotoginsengA =b; -b =p; the +temp =NUM1; ANUM1 =num2; thenum2 =temp; + } -num3 = NUM1;//record a larger number of digits $ $ //Add - for(i =0; i < NUM1; i++) - { the //The working pointer is less than the smaller number of digits - if(I <num2)Wuyi { the //the sum of the records added -sum = a[num1-i-1]-'0'+ b[num2-i-1]-'0'+C; Wu if(Sum >=Ten)//and greater than ten - { Aboutc =1; $Temp[i] = (Char)((int)('0') + Sum-Ten); - } - Else//and less than ten - { Ac =0; +Temp[i] = (Char)((int)('0') +sum); the } - } $ Else{//I >= num2 thesum = a[num1-i-1] -'0'+C; the if(Sum >=Ten) the { the if(i = = (NUM1-1)) -num3++; inc =1; theTemp[i] = (Char)((int)('0') + Sum-Ten); the}Else{ Aboutc =0; theTemp[i] = (Char)((int)('0') +sum); the } the } + } - the //Reverse print two number of andBayiprintf"The sum is:\n"); the for(i = num3-1; I >=0; i--) theprintf"%c", Temp[i]); -printf"\ n"); - the return 0; the}
Operation Result:
Add two large numbers