First, the algorithm brief description
Given two numbers, multiply how to get high-precision results, given the two number, not sure is not a floating-point number, that is, may be a few with multiple decimal places, the other with a decimal, or two numbers with a number of decimals, or all without decimals, for these cases, the program should be considered, The so-called high-precision is actually to see two number of decimal place, then the scale of the result should be the sum of two decimal digits.
Second, the algorithm thinking
In view of the above description, in fact, most of the thought of the first thought is to use a string to represent the two numbers, with a decimal point and without a decimal point can eventually be converted into a similar to two large integer multiplication, in the final consideration of the decimal place to the result of the appropriate position
Third, the algorithm code
/*b float multipy, design a high-precision algorithm*/#include<iostream>#include<string>#include<cstring>#include<algorithm>using namespacestd;/** Function: To multiply the two numbers and insert the decimal point in the appropriate position, Posdot is the sum of two decimal places*/voidBig_multi (Const Char*STR1,Const Char*STR2,intPosdot) { inti,j; intlen1=strlen (STR1); intLen2=strlen (STR2); intrlen=len1+Len2; int*presult =New int[Rlen];//allocates an array to store the computed results, and the sum of the digits of size two digitsmemset (PResult,0, rlen*sizeof(int)); //multiply by phase and place in the corresponding position, Presult[0] holds the final carry, so the calculation is presult[i+j+1] for(i=0; i<len1;++i) { for(j=0; j<len2;++j) Presult[i+j+1]+= (str1[i]-'0') * (str2[j]-'0'); } //determines whether the result of a phase multiply is greater than 10 if it is greater than 10 to forward the bit from the back for(i=rlen-1;i>0;--i) {intt=Presult[i]; if(t>9) {Presult[i-1] + = t/Ten; Presult[i]=t%Ten; } } //determine whether the beginning of the result starts with 0, if you want to removeI=0; while(presult[i]==0) I++; //The decimal point should be posdot from the back to the forward positionj=i; while(j<Rlen) { if(j==rlen-Posdot) cout<<"."; cout<<Presult[j]; J++; } cout<<Endl; }intMain () {strings1,s2; intPos1,pos2,posdot; while(cin>>s1>>S2) { if(s1=="quit") Break; POS1=s1.find ("."); Pos2=s2.find ("."); //calculate the number of decimal digits for the result of the final multiplication of two numbers, with 3 cases as follows//two numbers have a decimal point. if(pos1!=string:: NPOs && pos2!=string:: NPOs) Posdot= S1.length () +s2.length ()-pos1-pos2-2; //One of them has a decimal point. Else if(pos2!=string:: NPOs) Posdot= S2.length ()-pos2-1; //One of them has a decimal point. Else if(pos1!=string:: NPOs) Posdot= S1.length ()-pos1-1; //before multiplying, remove the decimal point from the two number and use the STL's REMOVE_IF function string:: Iterator Newend1=remove_if (S1.begin (), S1.end (), bind2nd (equal_to<Char> (),'.')); string:: Iterator Newend2=remove_if (S2.begin (), S2.end (), bind2nd (equal_to<Char> (),'.')); //after the decimal point last year, remove the extra characters between the end of the new string and the end of the original string.s1.erase (Newend1,s1.end ()); S2.erase (Newend2,s2.end ()); Const Char*str1=S1.c_str (); Const Char*str2=S2.c_str (); //cout<<str1<< "" <<str2<< "" <<posDot<<endl;Big_multi (STR1,STR2,POSDOT); } return 0;}
Multiply by large number-high-precision multiplication