C语言实现大数相加

C语言实现大数相加

//利用栈来实现大数相加
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char s1[100];
	char s2[100];
	printf("请分别输入你的s1,s2\n");
	gets(s1);
	gets(s2);
	char *s1_p;
	char *s2_p;
	int *s3_p;
	int i,temp;
	s1_p=(char *)malloc(sizeof(char)*strlen(s1));
	s2_p=(char*)malloc(sizeof(char)*strlen(s2));
	s3_p=(int*)malloc(sizeof(int)*(strlen(s2)+strlen(s1)));
	int s1_p_;
	int s2_p_;
	int s3_p_;
	s1_p_=s2_p_=s3_p_=-1;
	for(i=0;i<strlen(s1);i++){
		s1_p[++s1_p_]=s1[i];
	}
	for(i=0;i<strlen(s2);i++){
		s2_p[++s2_p_]=s2[i];
	}
	temp=0;
	while(s1_p_>-1&&s2_p_>-1){
		s3_p[++s3_p_]=(s1_p[s1_p_]+s2_p[s2_p_]-2*'0'+temp)%10;
		temp=(s1_p[s1_p_]+s2_p[s2_p_]+temp-2*'0')/10;
		s1_p_--;
		s2_p_--;
	}
	if(s1_p_==-1){
		while(s2_p_>-1){
		s3_p[++s3_p_]=(s2_p[s2_p_]-'0'+temp)%10;
		temp=(s2_p[s2_p_]-'0'+temp)/10;
		s2_p_--;
	}
	}
	else if(s2_p_==-1){
		while(s1_p_>-1){
		s3_p[++s3_p_]=(s1_p[s1_p_]-'0'+temp)%10;
		temp=(s1_p[s1_p_]-'0'+temp)/10;
		s1_p_--;
	}
	}
		s3_p[++s3_p_]=temp; 
	while(s3_p_>-1){
		printf("%d",s3_p[s3_p_--]);
	}
	printf("\n");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值