Common Library of functions (collected in Network)

Source: Internet
Author: User

Large number multiplication: void _mult (String num1, String num2, String &result) Parameters 1 and 2 are two large numbers, the result is saved in the 3rd string

1 void_mult (stringNUM1,stringNUM2,string&result)2 {3Reverse (Num1.begin (), Num1.end ());//reversal4 Reverse (Num2.begin (), Num2.end ());5result="";6     intI, J, re_int[ Max];//******** Here 150 is the number of digits that can be increased or decreased as needed *********7memset (Re_int,0,sizeof(Re_int));8      for(i=0; I<num1.length (); i++)//two string multiplication, the result is stored in the Re_int array, up to 150 bits! 9          for(j=0; J<num2.length (); J + +)TenRE_INT[I+J] + = ((num1[i]- -) * (num2[j]- -));  One     intJinwei=0, Zhi; A      for(i=0; I<num1.length () +num2.length (); i++)//handling the carry problem individually, the array in the previous step can have more than 10 of each element, so no rounding is handled -     { -Zhi = re_int[i]+Jinwei; theRe_int[i] = zhi%Ten; -Jinwei = zhi/Ten; -     } -      for(I=num1.length () +num2.length ()-1; i>=0; i--)//Mark I with a marker, the front part of the array re_int may be 0, to remove +         if(re_int[i]!=0) Break;  -      for(; i>=0; i--)//Convert an integer array to a string +result = result+ (Char) (re_int[i]+ -); A     if(result=="")//If the result is still empty, the result of multiplication is 0?  atresult="0";  -}
Multiply

Large number of additions: void _plus (String num1,string num2,string &result) Parameters 1 and 2 are two large numbers, the result is saved in the 3rd string

1 void_plus (stringNUM1,stringNUM2,string&result)2 {3result="";4 Reverse (Num1.begin (), Num1.end ());5 Reverse (Num2.begin (), Num2.end ());6     inti;7      for(i=0;i<int(Num1.length ()) &&i<int(Num2.length ()); i++)8     {9         CharC= (Char) (num1[i]+num2[i]- -);Tenresult=result+C; One     } A      while(i<int(Num1.length ())) -     { -result=result+Num1[i]; thei++; -     } -      while(i<int(Num2.length ())) -     { +result=result+Num2[i]; -i++; +     } A     intJinwei=0; at      for(i=0;i<int(Result.length ()); i++) -     { -         intzhi=result[i]- -+Jinwei; -Result[i]= (Char) (zhi%Ten+ -); -jinwei=zhi/Ten; -     } in     if(jinwei!=0) -result = result+ (Char) (jinwei+ -); to      for(I=result.length ()-1; i>=0; i--) +         if(result[i]!='0') Break; -result = Result.substr (0, i+1); the Reverse (Result.begin (), Result.end ()); *     if(result.length () = =0) $result="0";Panax Notoginseng}
Plus

Common Library of functions (collected in Network)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.