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;
}