#include<cstdio>
#include<cstdlib>
typedef int SElemType;
#define MAXSIZE 100000
typedef struct StackNode{
SElemType data;
struct StackNode *next;
};
typedef struct LinkStack{
StackNode *top;
int count;
};
void initLinkStack(LinkStack *S){
S->top=NULL;
S->count=0;
}
void Push(LinkStack *S,SElemType e){
StackNode *p=(StackNode *)malloc(sizeof(StackNode));
p->data=e;
p->next=S->top;
S->top=p;
S->count++;
}
void Pop(LinkStack *S,SElemType *e){
StackNode *p;
p=S->top;
*e=S->top->data;
S->top=S->top->next;
S->count--;
free(p);
}
int main()
{
LinkStack a,b,c;
char a_t[MAXSIZE];char b_t[MAXSIZE];
int i=0;
int e,d;
int t=0;
initLinkStack(&a);initLinkStack(&b);initLinkStack(&c);
printf("请输入两个大整数,两数用空格隔开:\n");
scanf("%s%s",a_t,b_t);
while(a_t[i]!='\0'){
Push(&a,a_t[i]-'0');
++i;
}
i=0;
while(b_t[i]!='\0'){
Push(&b,b_t[i]-'0');
++i;
}
while(a.count && b.count){
Pop(&a,&e);
Pop(&b,&d);
Push(&c,(e+d+t)%10);
t=(int)((e+d+t)/10);
}
while(a.count){
Pop(&a,&e);
Push(&c,(e+t)%10);
t=(int)((e+t)/10);
}
while(b.count){
Pop(&b,&e);
Push(&c,(e+t)%10);
t=(int)((e+t)/10);
}
printf("计算的结果为:\n");
if(t>0)printf("%d",t);
while(c.count){
Pop(&c,&e);
printf("%d",e);
}
printf("\n");
return 0;
}
/*
【个人原创】
以链栈的形式实现。。
欢迎大家互相学习*/
|