Large number addition multiplication:
1 /*2 2015.43 large number addition, multiplication4 5 */6#include <iostream>7#include <string>8#include <vector>9 Ten using namespacestd; One #defineMAX 99 A #defineMAXM 200 - - voidBignumadd () the { - CharStra[max], Strb[max]; -cout <<"Enter large number A, B:"<<Endl; -CIN >>Stra; +CIN >>StrB; - + intNum1[max], Num2[max], K; A intI, J, Len1, Len2; at for(k =0; k<max; k++) - { -NUM1[K] =0; -NUM2[K] =0; - } -Len1 =strlen (stra); inLen2 =strlen (StrB); - for(i = len1-1, j =0; I >=0; i--)//Num[0] kept low. toNum1[j++] = stra[i]-'0'; + for(i = len2-1, j =0; I >=0; i--) -Num2[j++] = strb[i]-'0'; the for(i =0; i<max; i++) * { $Num1[i] + =Num2[i];Panax Notoginseng if(num1[i]>9) - { theNum1[i]-=Ten; +Num1[i +1]++; A } the } + - for(i = (MAX-1); (I >=0) && Num1[i] = =0; i--);//make the first digit not to be 0 $ if(I >=0) $ for(; I >=0; i--) -cout <<Num1[i]; - Else thecout <<"0"<<Endl; -cout <<Endl;Wuyi } the - voidbignummultiply () Wu { - inti; About strings; $vector<int>A, B; -cout <<"Enter large number A, B:"<<Endl; -CIN >>s; - A //capacity is not enough to expand + A.reserve (S.size ()); the for(i =0; I < (int) S.size (); ++i) - { $ //add in the tail theA.push_back (S[i]-'0'); the } theCIN >>s; the B.reserve (S.size ()); - for(i =0; I < (int) S.size (); ++i) in { theB.push_back (S[i]-'0'); the } About the //The container that holds the result, with an initial value of 0 thevector<int> C (a.size () + b.size ()-1,0); the intJ, K, temp; + - for(i =0; I < (int) A.size (); ++i) the{//the i,j loop is multiplied from the high, and the result is added in the same position.BayiK =i; the for(j =0; J < (int) B.size (); ++j) the { -c[k++] + = a[i] *B[j]; - } the } the the for(k = c.size ()-1; K >=0; --k) the { - if(C[k] >9) the { the if(k! =0) the {94C[k-1] + = C[k]/Ten; theC[K]%=Ten; the } the Else98 { Abouttemp = C[k]/Ten; -C[K]%=Ten;101 //add to the front102 C.insert (C.begin (), temp);103 }104 } the }106 107 for(i =0; I < (int) C.size (); ++i)108{//Output109cout <<C[i]; the }111cout <<Endl; the }113 the intMain () the { thecout <<"large number addition, multiplication:"<<Endl;117cout <<"Select: Add 2--multiplication 3--exit"<<Endl;118 int Select=0;119 - while(1)121 {122cout <<"Input Selection:";123CIN >>Select;124 if(Select==1) the Bignumadd ();126 Else if(Select==2)127 bignummultiply (); - Else if(Select==3)129 Break; thecout <<Endl;131 } the 133 return 0;134}
Large number addition multiplication