#include<stdio.h>
#include<conio.h>
#define MAX 100
typedef struct LNOde
{
int data;
struct LNOde *pre,*next;
}*LNOde,List;
LNOde CreatList()
{
LNOde p,q,r;
char s[MAX];
int i=0;
printf("输入一个整数:");
gets(s);
p=(LNOde)malloc(sizeof(List)*MAX);
q=r=p;p->data=-1;
while(s[i]!='\0')
{
q=(LNOde)malloc(sizeof(List));
q->data=s[i++]-'0';
q->pre=r;
r->next=q;
r=q;
}r->next=NULL;
return p;
}
LNOde QiuHe(LNOde t1,LNOde t2)
/*两数相加*/
{
while(t1->data!=-1&&t2->data!=-1)
{
t1->data=t1->data+t2->data;
if(t1->data>9)
{
t1->data-=10;
t1->pre->data+=1;
}
t1=t1->pre;
t2=t2->pre;
}
if(t1->next->data>9)
t1->next->data-=10;
return t1;
}
LNOde Comper(LNOde p1,LNOde p2)
/*比较两数的位数*/
{
LNOde s1=p1->next,s2=p2->next,t1,t2;
int i=0,j=0;
while(s1!=NULL){t1=s1,s1=s1->next;i++;}
while(s2!=NULL){t2=s2;s2=s2->next;j++;}
if(i>=j)QiuHe(t1,t2);
/*第一个数的位数不小于第二个数的位数,加到第一个数上*/
else QiuHe(t2,t1);
return i>=j?p1:p2;
}
void print(LNOde L)
{
LNOde p;
printf("两整数的和为:");
if(L->data!=-1)printf("%d",1);
p=L->next;
do
{
printf("%d",p->data);
p=p->next;
} while(p!=NULL);
printf("\n");
}
int main()
{
LNOde p1,p2,p3;
p1=CreatList();
p2=CreatList();
p3=Comper(p1,p2);
print(p3);
getch();
return 0;
}