c语言 大数据乘法

c语言 大数据乘法


#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include


void getbigdata(char* dataa, char* datab)
{

	int lengtha = strlen(dataa);
	int lengthb = strlen(datab);
	int *pres = (int *)malloc(sizeof(int)*(lengtha + lengthb));
	memset(pres, 0, sizeof(int)*(lengtha + lengthb));
	//累乘
	for (int i=0; i < lengtha; i++)
	{
		for (int j = 0; j < lengthb; j++)
		{
			pres[i+j+1]+=(dataa[i] - '0')*(datab[j] - '0');
		}
	}
	
	for (int i = lengtha + lengthb - 1; i >= 0;i--)
	{
		if (pres[i] >= 10)
		{
			pres[i - 1] += pres[i] / 10;
			pres[i] %= 10;
		}
	}
	int i = 0;
	while (pres[i] == 0)
	{
		i++;
	}
	char *lastres = malloc(sizeof(char)*(lengtha + lengthb));
	int j = 0;
	for (j = 0; j < lengtha + lengthb; j++)
	{
		lastres[j] = pres[i] + '0';
	}
	lastres[j] = '\0';
	printf("\nlast结果=%s", lastres);



}

void main()
{
	char str1[100] = { 0 };
	char str2[100] = { 0 };
	scanf("%s%s", str1, str2);
	printf("str1=%s,str2=%s", str1, str2);
	getbigdata(str1, str2);

	system("pause");
}


你可能感兴趣的:(c语言 大数据乘法)