c语言 大数据乘法

c语言 大数据乘法

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


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");
}
    原文作者:大整数乘法问题
    原文地址: https://blog.csdn.net/e891377/article/details/51592774
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞