Multiply by large number

Source: Internet
Author: User

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

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.