大数乘法

#include <iostream>

void multipy(char* a, char* b, char* res)
{
	int lenA = strlen(a);
	int lenB = strlen(b);

	int i, j;


	int* c = new int[lenA + lenB];
	memset(c, 0, sizeof(int) * (strlen(a) + strlen(b)));

	for (i = lenA - 1; i >= 0; i--)
		for (j = lenB - 1; j >= 0; j--)					
			c[i + j + 1] += (b[j] - '0') * (a[i] - '0');

	for (i = lenA + lenB - 1; i >= 0; i--)
	{
		if (c[i] >= 10)
		{
			c[i - 1] += c[i] / 10;
			c[i] %= 10;
		}
	}
		
	i = 0;
	while (c[i] == 0)
		i++;

	j = 0;
	while (i < lenA + lenB)
	{
		res[j] = c[i] + '0';
		i++; j++;
	}

	res[j] = 0;
	delete[] c;
};

int main()
{
	char* a = "999";
	char* b = "9999";
	char* res = new char[strlen(a) + strlen(b)];

	multipy(a, b, res);
	return 0;
}

你可能感兴趣的:(大数乘法)