大数相乘(1)

本文介绍了一种处理大数相乘的算法实现方法,通过使用C++编程语言中的数组来模拟乘法运算过程。文章提供了两种不同的实现方式:一种是逐位相乘并累加;另一种则利用二维数组来存储中间结果再进行累加。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目:两个大数相乘。

大数相乘(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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值