//large number of additions/*1, input, char to int, and then reverse the array 2, find the length of the length of the array after the sum of 3, add the data can be placed in a new array, can also be placed in the length of the array (this time to pay attention to the length) 4, I foolishly, in fact, directly add, regardless of a[i]+b[j] The resulting number is greater than 9, because an array element holds an int that size 5, the sum of the number of processing, carry operation 6, the output because it is from the high output, so pay attention to the output of excess 0, such as 0013241;*//*addition of large numbers: Beyond the range of data types can also be added to the analysis: with a one-dimensional array to solve: 1. Data element input with GetChar infinite input characters, from subscript 0 to save the element save order is high in front, low after, enter 123 save A1[0]=1 .... Here also to convert the character to shaping, the relationship integer 1+ ' 0 ' = ' 1 ' 2. One but encountered rounding problem, the above will overflow, the data can not be fully saved, all the above storage order is low in front, high in the post as input 123 a[0]=3,a[1]=2 3. Add operation : Each one is added here the added results are saved with the existing numbers, and the number of inputs may vary in length. All array lengths take up to 4 of the maximum input. Add carry problem: Because each bit is added, the result may be greater than 9, more than 9 of the carry operation, the other unchanged 5. Display the output problem: there may be fewer or more digits, here is a variable ncount to solve*///From Net#include<iostream>using namespacestd;intMain () {inta1[ -]; intb1[ -]; inta[ -]={0}; intb[ -]={0}; intNa, NB;//Save the length of an array element intNcount =0;//How many elements are there in the added array intI=0, j=0; ////////////////////input data//////////////////////////////cout<<"Please enter Addend:"; while((A1[i]=getchar ())! ='\ n')//Subscript Small Save high{A1[i]= a1[i]-'0';//converts a character into a shape and stores it back in the A1 array.i++; } na= i;//save how many valid elements are stored in the array na-1 ///////////////////Convert Storage order/////////////////// for(intk=0, i=na-1; i>=0; i--)// //Subscript Small Save low{A[i]=A1[k]; K++; } cout<<"input Summand:"; while((B1[j]=getchar ())! ='\ n')//Put the summand inside the B1 .{B1[j]= b1[j]-'0'; J++; } NB= J;//save how many valid elements are stored in the array nb-1 ///////////////////Convert Storage order for(intk=0, j=nb-1; j>=0; j--)// //Subscript Small Save low{B[j]=B1[k]; K++; } ////////////gets the length of the array that saved the addition result//////////////////// //If you enter 123+23 we are going to take a large number of digits as a subscript to save the result array, or we will lose the data intlengh=na>nb?na:nb;//takes the longest array element as the result of saving the addition //////////////////////////start to perform addition operations//////////////// for(intm=0; m<lengh; m++) {A[m]= A[m] + b[m];//two number to add each bit } ///////////////////////////////////////Rounding/////////////////////////////////////////// /*such as 98+7 will carry data to save as follows: A[0]=8, a[1]=9; B[0]=7 performs the above add operation result as follows: A[0]=a[0]+b[0]=15, a[1]=a[1]+b[1] = 9+0=9 The following is the carry process Lengh =2 a[0]=15 a[1]=9 1. I=0 a[0]>9 required Carry temp=15/10=1 a[1]=a[1]+temp =9+1=10 a[0]=15%10=5 2.i=1 a[1]=10>9 Continue temp = 10/10=1 A[2]=0+1=1 a[1]=10%10=0 3.i=2 equals lengh So the result of the addition is: a[0]=5 a[1]=0 a[2]=1 output is the*/ for(intn=0; n<lengh; n++)//Rounding { if(A[n] >9) { inttemp = a[n]/Ten; A[n+1] = a[n+1] +temp; A[n]= a[n]%Ten; Ncount= Lengh;//One more of the array of rounding. } Else{ncount= lengh-1;//the length of the array that does not carry the saved result is the long number of digits in the input number Continue;//go down without carrying it. } } //////////////////////Output Results///////////////////////////// cout<<"The addition results are:"; for(intI=ncount; i>=0; i--)//Ncout solves the problem of displaying output results: There may be fewer or more digits{cout<<A[i]; } cout<<Endl; System ("Pause"); return 0;}
Add large numbers