以下是自己写的两个大整数乘法的程序:
//the progrom is used to calculate the product of two large integer
//It is need a input file named abc.txt which is formated as follows:
// m //the integer m means that the first large integer have m median
// p //the integer p is the first large integer
// n //the integer n means that the second large integer have n median
// q //the integer q is the second large integer
//for instance,we have a input file as follows:
// 3
// 1 2 7
// 3
// 175
#include
#include
#include
using namespace std;
void input(vector
void MulOfBigInt(const vector
void print(const vector
int main()
{
int i,j,m=0,n=0;
vector
vector
input(a,b,m,n);//从文件读入数据
vector
vector
vector
for(i=0;i
for(j=0;j
MulOfBigInt(a1,b1,c);//调用大整数乘法函数
print(c); //输出结果
return 0;
}
//input function
void input(vector
{
ifstream in("abc.txt");
int i,j;
in>>m;
a.resize(m);
for(i=0;i
in>>n;
b.resize(n);
for(j=0;j
}
//the function of product of two large integer
void MulOfBigInt(const vector
{
int temp1,temp2;
int m=a.size(),n=b.size();
for(int i=0;i
temp1=b[i]*a[j];
if(temp1>=10)
{
c[i+j]+=temp1%10;
c[i+j+1]+=temp1/10;
if(c[i+j]>=10||c[i+j+1]>=10)
goto L;
}
else
{
c[i+j]+=temp1;
if(c[i+j]>=10)
{
L:temp2=c[i+j];
c[i+j]=temp2%10;
c[i+j+1]+=temp2/10;
}
}
}
}
//output function
void print(const vector
{
int n=c.size();
for(int i=1;;)
{
if(c[n-i]==0)
i++;
else
{
for(int j=n-i;j>=0;j--)
cout<
}
}
}
以下是一个测试数据:
abc.txt
60
1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7
62
4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 4 3 7 1 4 3 5 3 8 6 1 3
结果: