C++ custom API function implements large number multiplication algorithm

  • 2020-06-19 11:18:12
  • OfStack

Preface:

Is the front of the subject is divided into custom API function update (), because the author wanted to form a set of algorithm is good, clear interface, convenient programming algorithm, but also for future better call algorithm, similar problems can be called directly, everyone and easy to use, develop higher efficiency of programs. Among them the efficiency dare not say best, return hope everybody exchanges, common progress! Let's get down to business.

Ordinary multiplication calculation can be solved by int, long and double, but sometimes the number to be processed is too large, resulting in overflow. The following is the algorithm to realize the positive integer A*B of arbitrary length, namely the multiplication of large Numbers. This algorithm is easy to understand and the train of thought is as follows:

1. Use char array a and b to save the input Numbers A and B respectively in the main function;

2. Each bit of string array a and b is multiplied by each other, like the vertical multiplication learned in primary school. The result is saved in the string array s, but no carry;
3. Carry each 1 bit of string array s;

4. Assign the string array s to the char array c, and return the result.

char version:


// Multiply large Numbers, and realize large Numbers A*B
char* getMultiplyValue(char a[],char b[]) // parameter a : char Type of the array a The name of the array; parameter b : char Type of the array b The name of the array; 
{
 int i,j,ca,cb,*s,cs;
 char *c;
 
 ca=strlen(a); // Find an array of strings a The length of the 
 cb=strlen(b); // Find an array of strings b The length of the 
 cs=ca+cb;  
 s=new int[cs];
 c=new char[cs];
 
 // Initialize the 
 for(i=0;i<cs;i++)
 s[i]=0;
 
 // Direct vertical summation, no carry 
 for(i=0;i<ca;i++)
 for(j=0;j<cb;j++)
 s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); // because s[0] Used to save the highest bit carry after, so from s[1] start 
 
 // Handle the carry of the sum 
 for(i=cs-1;i>=0;i--)
 if(s[i]>=10)
 {
 s[i-1]+=s[i]/10;
 s[i]%=10;
 }
 
 // If the highest bit does not carry, the beginning is discarded 0
 i=0;
 while(s[i]==0)
 i++;
 
 // Save the result to a string 
 for(j=0;i<cs;i++,j++)
 c[j]=s[i]+'0';
 c[j]='\0'; 
 
 return c;
 
}

In addition, attached is the version of string,


#include <string>
using namespace std;
 
// Multiply large Numbers, and realize large Numbers A*B
string getMultiplyValue(string a,string b) // parameter a : string type a Is the variable name; parameter b : string type b Is the variable name; 
{
 int i,j,ca,cb,cs,*s;
 string sum;
 
 ca=a.length(); // Strives for the string a The length of the 
 cb=b.length(); // Strives for the string b The length of the 
 s=new int[cs=ca+cb];
 
 
 // Initialize the 
 for(i=0;i<cs;i++)
 s[i]=0;
 
 // Direct vertical summation, no carry 
 for(i=0;i<ca;i++)
 for(j=0;j<cb;j++)
 s[i+j+1]+=(a[i]-'0')*(b[j]-'0'); // because s[0] Used to save the highest bit carry after, so from s[1] start 
 
 // Handle the carry of the sum 
 for(i=cs-1;i>=0;i--)
 if(s[i]>=10)
 {
 s[i-1]+=s[i]/10;
 s[i]%=10;
 }
 
 // If the highest bit does not carry, the beginning is discarded 0
 i=0;
 while(s[i]==0)
 i++;
 
 // Save the result to a string 
 for(j=0;i<cs;i++,j++)
 sum+=s[i]+'0';
 
 return sum;
 
}

Here's an example of how char works (same as string), to teach you how to use it more thoroughly,


#include <iostream>
using namespace std;
int main()
{
 char a[255],b[255];
 
 cin>>a>>b;     // Enter the Numbers a,b
 
 cout<<getMultiplyValue(a,b)<<endl; // The output a*b
 
 return 0;
}

I hope it helps.


Related articles: