The implementation of the multiplication of large numbers, in this case, uses the most direct implementation method: Similar to manual calculation, phase multiplication.
1#include <iostream>2 3 using namespacestd;4 5 //invert characters so that they conform to the array lows to the digital low6 voidReversenum (Char*S1)7 {8 intI=0;9 intJ=strlen (S1)-1;Ten while(i<j) One { A Chartemp=S1[i]; -s1[i]=S1[j]; -s1[j]=temp; the++i; ---J; - } - } + - //enter two numeric strings, multiply them, and return the result string + Char* Bignummultiple (Char* S1,Char*S2) A { at //Invert numbers - Reversenum (S1); - reversenum (S2); - - intTemp1,temp2,addflag,multiflag; - intlen1=strlen (S1); in intLen2=strlen (S2); -Temp1=temp2=0; to + Char* result=New Char[len1+len2+1]; -memset (Result, -, len1+len2); theresult[len1+len2]=' /'; * $ //MultiplyPanax Notoginseng for(intI=0; i<=len1-1; i++) - { themultiflag=0; +addflag=0; A for(intj=0; j<=len2-1; j + +) the { +Temp1= (s1[i]-'0') * (s2[j]-'0')+Multiflag; -multiflag=temp1/Ten; $temp1=temp1%Ten; $Temp2= (result[i+j]-'0') +temp1+Addflag; -result[i+j]=temp2%Ten+'0'; -addflag=temp2/Ten; the } -result[i+len2]+=multiflag+Addflag;Wuyi } the - //remove high-level excess 0 Wu intN=strlen (Result)-1; - while(result[n]=='0') About { $ //Make sure the result is 0 without emptying the last 0. - if(n==0) - Break; -result[n]=' /'; An--; + } the - //string display of recovered numbers $ Reversenum (S1); the reversenum (S2); the reversenum (result); the the returnresult; - } in the //Simple Test the intMain () About { the while(true) the { the Char* left=New Char[ -]; + Char* right=New Char[ -]; - Char*str; theCin.getline (left, -);BayiCin.getline (right, -); theStr=bignummultiple (left,right); thecout<<left<<"*"<<right<<"=\n"<<str<<Endl; -System"Pause"); - } the}
Multiply by large number