题目:两个大数相乘。
大数相乘(2)见此。
方法1:将两个大数分别用数组存储,模拟乘法运算,将其中一个数组的每一位分别与令一个大数相乘,结果相加。
代码见后面代码部分的string multiply1(bignumber &bignumber2)。
方法2:同样是上面的思路,不同的是存储过程直接用一个二维数组来存取每一位相乘的结果,然后处理相加的过程。
代码见后面代码部分的string multiply2(bignumber &bignumber2)。
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class bignumber
{
private:
string bigno;
bool checkout;
bignumber();
bool checknumber()
{
for(int count = 0; count < bigno.length(); ++count)
{
if(!isdigit(bigno[count]))
{
return false;
}
}
return true;
}
public:
explicit bignumber(const char tocopy[])
{
bigno = tocopy;
checkout = checknumber();
}
explicit bignumber(const bignumber &tocopy)
{
bigno = tocopy.bigno;
checkout = checknumber();
}
string showvalue()
{
return bigno;
}
bool showcheck()
{
return checkout;
}
string multiply1(bignumber &bignumber2)
{
if(!(checkout && bignumber2.showcheck()))
{
cout<<"Values are not all positive numbers."<<endl;
return "";
}
int len1 = bigno.length();
int len2 = bignumber2.showvalue().length();
int *no1 = new int[len1];
memset(no1, 0, len1 * sizeof(int));
int *no2 = new int[len2];
memset(no2, 0, len2 * sizeof(int));
int count = 0;
for(count = len1; count > 0; --count)
{
no1[count - 1] = (int)(bigno[count - 1]) - 48;
}
for(count = len2; count > 0; --count)
{
no2[count - 1] = (int)(bignumber2.showvalue()[count - 1]) - 48;
}
int *convertpointer = new int[len1 + len2];
memset(convertpointer, 0, (len1 + len2) * sizeof(int));
for(count = len2; count > 0; --count)
{
for(int countrow = len1; countrow > 0; --countrow)
{
convertpointer[countrow + count - 1] += no1[countrow - 1] * no2[count - 1];
}
}
for(count = len1 + len2; count > 0; --count)
{
if(convertpointer[count - 1] > 9)
{
convertpointer[count - 2] += convertpointer[count - 1] / 10;
convertpointer[count - 1] = convertpointer[count - 1] % 10;
}
}
char *resultchar = new char[len1 + len2];
bool checkresultzero = false;
int countresultzero = 0;
for(count = 0; count < len1 + len2; ++count)
{
if(!checkresultzero)
{
if(convertpointer[count] != 0)
{
checkresultzero = true;
resultchar[countresultzero++] = convertpointer[count] + '0';
}
}
else
{
resultchar[countresultzero++] = convertpointer[count] + '0';
}
}
resultchar[countresultzero] = '\0';
string result = resultchar;
delete[] no1;
delete[] no2;
delete[] convertpointer;
delete[] resultchar;
return result;
}
string multiply2(bignumber &bignumber2)
{
if(!(checkout && bignumber2.showcheck()))
{
cout<<"Values are not all positive numbers."<<endl;
return "";
}
int len1 = bigno.length();
int len2 = bignumber2.showvalue().length();
int **convertcolumn = new int*[len2 + 1];
int countcolumn = 0;
int countrow = 0;
for(countcolumn = 0; countcolumn < len1 + 1; ++countcolumn)
{
convertcolumn[countcolumn] = new int[len1 + len2 + 1];
memset(convertcolumn[countcolumn], 0, sizeof(int) * (len1 + len2 + 1));
}
for(countcolumn = len2 + 1; countcolumn < len1 + len2 + 1; ++countcolumn)
{
convertcolumn[0][countcolumn] = (int)(bigno[countcolumn - len2 - 1]) - 48;
}
for(countrow = 1; countrow < len2 + 1; ++countrow)
{
convertcolumn[countrow][0] = (int)(bignumber2.showvalue()[countrow - 1]) - 48;
}
for(countrow = len2; countrow > 0; --countrow)
{
for(countcolumn = len1; countcolumn > 0; --countcolumn)
{
convertcolumn[countrow][len2 + 1 - countrow + countcolumn] = convertcolumn[0][countcolumn + len2] * convertcolumn[len2 + 1 - countrow][0];
}
}
int *convertpointer = new int[len1 + len2];
memset(convertpointer, 0, sizeof(int) * (len1 + len2));
int count = 0;
for(count = len1 + len2; count > 0; --count)
{
for(countrow = 1; countrow < len2 + 1; ++countrow)
{
convertpointer[count - 1] += convertcolumn[countrow][count];
}
if(convertpointer[count - 1] > 9)
{
convertpointer[count - 2] += convertpointer[count - 1] / 10;
convertpointer[count - 1] = convertpointer[count - 1] % 10;
}
}
char *resultchar = new char[len1 + len2];
bool checkresultzero = false;
int countresultzero = 0;
for(count = 0; count < len1 + len2; ++count)
{
if(!checkresultzero)
{
if(convertpointer[count] != 0)
{
checkresultzero = true;
resultchar[countresultzero++] = convertpointer[count] + '0';
}
}
else
{
resultchar[countresultzero++] = convertpointer[count] + '0';
}
}
resultchar[countresultzero] = '\0';
string result = resultchar;
for(countcolumn = 0; countcolumn < len1 + 1; ++countcolumn)
{
delete[] convertcolumn[countcolumn];
}
delete[] convertcolumn;
delete[] convertpointer;
delete[] resultchar;
return result;
}
};
int main()
{
bignumber aaa("11234567890");
bignumber bbb("00123456788");
cout<<aaa.showvalue()<<endl<<bbb.showvalue()<<endl;
cout<<aaa.multiply1(bbb)<<endl;
cout<<aaa.multiply2(bbb)<<endl;
return 0;
}