加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大数相乘简单代码

发布时间:2020-12-14 04:13:44 所属栏目:大数据 来源:网络整理
导读:只是简单实现,还没有优化,先放上来再说。看上去还有点乱,应该还会有更简单的方法 #include stdio.h#include string.h#include malloc.h#include stdlib.h#define NUMLENTH 32char *multibig(char *a,char *b,char *c){if(NULL == a || NULL == b || NULL

只是简单实现,还没有优化,先放上来再说。看上去还有点乱,应该还会有更简单的方法


#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define NUMLENTH   32
char *multibig(char *a,char *b,char *c)
{
	if(NULL == a || NULL == b || NULL == c){
		exit(0);
	}

	int len1,len2;
	len1 = strlen(a);
	len2 = strlen(b);

	int i,j;
	int len = len1 + len2 -2;
	for(i=len1-1;i>=0;i--){ 	/*因为是倒序的*/
		for(j=len2-1;j>=0;j--){
			int len3 = len - i - j;
			c[len3] += (a[i] - '0')*(b[j] - '0');
			if(c[len3] >= 10){
				c[len3+1] += (c[len3]/10);
				c[len3] %= 10;
				if(c[len3+1] >= 10){
					int k=2;
					c[len3+k]++;
					c[len3+1] %= 10;
					while(c[len3+k] >= 10){
						c[len3+k+1]++;
						c[len3+k] %= 10;
						k++;
					}
				}
			}
		}
	}

	return ;
}

int main(void)
{
	char *a = NULL;
	char *b = NULL;
	char *c = NULL;

	a =(char *) malloc(NUMLENTH);
	if(NULL == a){
		exit(0);
	}
	memset(a,NUMLENTH);

	b = (char *)malloc(NUMLENTH);
	if(NULL == b){
		exit(0);
	}
	memset(b,NUMLENTH);

	c = (char *)malloc(NUMLENTH*2+1);
	if(NULL == c){

		exit(0);
	}
	memset(c,NUMLENTH*2+1);

	printf("please input num 1:");
	scanf("%s",a);

	
	printf("please input num 2:");
	scanf("%s",b);

	multibig(a,b,c);	
	int flag = 0;
	int i=0;
	printf("the result:");
	for(i=NUMLENTH*2+1;i>=0;i--){
		if(*(c+i) != 0 || flag !=0){
			printf("%d",*(c+i));
			flag =1;
		}
	}
	printf("n");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读