//交流方式sbkhyd@163.com
#include <stdio.h>
#define SIZE sizeof(Node)
typedef struct student
{
char x;
} Data;
typedef struct list_node
{
Data d;
struct list_node *next;
} Node;
void print_list(Node *head)
{
Node *p=head ;
if(head->next==NULL)
{
printf("The List is nothing");
exit (0);
}
if(head->d.x=='-') printf("%c",p->d.x);
p=p->next;
while(p&&p->d.x=='0')p=p->next;
if(p==NULL)
{ printf("0\n");
return ;}
while(p)
{
printf("%c",p->d.x);
p=p->next;
}
printf("\n");
}
Node *creatlink()
{
Node *head,*q,*p;
char c;
head = q =(Node*)malloc(SIZE);
q->d.x='+';
c=getchar();
if(c=='-'){q->d.x='-';c=getchar();}
else if(c=='+')c=getchar();
while(c>='0'&&c<='9')
{
p=(Node*)malloc(SIZE);
p->d.x=c;
q->next =p;
q=p;
c=getchar(); //数据输入
}
q->next =NULL;
q=head;
return head;
}
Node *creatNode() //创建结点
{
Node *p;
p=(Node*)malloc(SIZE);
p->d.x='0';
p->next =NULL;
return p;
}
Node *invert_list(Node *head) //逆链
{
Node *poi,*back,*next;
if((head->next==NULL)||(head->next->next==NULL))return head;
back=head->next;
poi=back->next;
back->next=NULL;
while (poi->next)
{
next=poi->next;
poi->next=back;
back=poi;
poi=next;
}
poi->next=back;
head->next=poi;
poi=head;
return head;
}
int cmplink(Node *head1,Node *head2)//比较两个数的大小
{
Node *p1=head1->next,*p2=head2->next;
int i=1;
for (;p1&&p2;p1=p1->next,p2=p2->next)
if(p1->d.x!=p2->d.x)
{
i=p1->d.x-p2->d.x;
break;
}
for (;p1&&p2;p1=p1->next,p2=p2->next);
if(p1==NULL&&p2==NULL)
{
if(i>0)return 1;
else return 2;
}
else if(p1&&p2==NULL)return 1;
else return 2;
}
Node *mulresult(Node *head1,Node *head2) //正数的乘法
{
Node *p1,*p2=head2,*head3,*p3,*p4;
int carry=0,res;
if(head1==NULL||head2==NULL)exit(0);
head3=p3=creatNode();
p4=p3=head3;
for(;p2;p2=p2->next,p4=p4->next)
{
p1=head1;
p3=p4;
if(p2->d.x=='0')
{
if(p3->next==NULL)
p3->next = creatNode();
}
else
for(;p1;p1=p1->next)
{
res=(p1->d.x-'0')*(p2->d.x-'0')+p3->d.x-'0';
p3->d.x=res%10+'0';
carry=res/10;
if(p3->next==NULL)
p3->next = creatNode();
p3=p3->next;
p3->d.x=p3->d.x+carry;
}
}
//计算结果
return head3;
}
Node *addresult(Node *head1,Node *head2) //正数的加法
{
Node *p1,*p2=head2,*head3,*p3;
int carry=0,res=0;
if(head1==NULL||head2==NULL)exit(0);
head3=p3=creatNode();
p1=head1;
for(;p1&&p2;p1=p1->next,p2=p2->next)
{
res=(p1->d.x-'0')+(p2->d.x-'0')+p3->d.x-'0';
p3->d.x=res%10+'0';
carry=res/10;
p3->next = creatNode();
p3=p3->next;
p3->d.x=carry+'0';
}
//计算结果
if(p1==NULL&&p2!=NULL)
for(;p2;p2=p2->next)
{
res=(p2->d.x-'0')+p3->d.x-'0';
p3->d.x=res%10+'0';
carry=res/10;
p3->next = creatNode();
p3=p3->next;
p3->d.x=carry+'0';
}
else if (p2==NULL&&p1!=NULL)
for(;p1;p1=p1->next)
{
res=(p1->d.x-'0')+p3->d.x-'0';
p3->d.x=res%10+'0';
carry=res/10;
p3->next = creatNode();
p3=p3->next;
p3->d.x=carry+'0';
}
return head3;
}
Node *bigsubresult(Node *head1,Node *head2) //大的数减小的数
{
Node *p1,*p2=head2,*head3,*p3;
int carry=0,res=0;
if(head1==NULL||head2==NULL)exit(0);
head3=p3=creatNode();
p1=head1;
for(;p1&&p2;p1=p1->next,p2=p2->next)
{
res=(p1->d.x-'0')-(p2->d.x-'0')+p3->d.x-'0';
if(res<0)
{
res=10+res;
carry=-1;
}
else carry=0;
p3->d.x=res+'0';
if(p3->next==NULL)
p3->next = creatNode();
p3=p3->next;
p3->d.x=carry+'0';
}
if(p1)
for(;p1;p1=p1->next)
{
res=(p1->d.x-'0')+p3->d.x-'0';
if(res<0)
{
res=10+res;
carry=-1;
}
else carry=0;
p3->d.x=res+'0';
p3->next = creatNode();
p3=p3->next;
p3->d.x=carry+'0';
}
return head3;
}
Node *sub(Node *head1,Node *head2,int i)//减法包括符号的运算
{
Node *head;
head =(Node*)malloc(SIZE);
if(head1->d.x!=head2->d.x)
{
head->d.x=head1->d.x;
head->next=addresult(head1->next,head2->next);
}
else
{
if(i==1)
{
head->d.x=head1->d.x;
head->next=bigsubresult(head1->next,head2->next);
}
else
{
if(head1->d.x=='+')head->d.x='-';
else head->d.x='+';
head->next=bigsubresult(head2->next,head1->next);
}
}
return head;
}
Node *add(Node *head1,Node *head2,int i) //加法包括符号的运算
{
Node *head;
head =(Node*)malloc(SIZE);
if(head1->d.x==head2->d.x)
{
head->d.x=head1->d.x;
head->next=addresult(head1->next,head2->next);
}
else
{
if(i==1)
{
head->d.x=head1->d.x;
head->next=bigsubresult(head1->next,head2->next);
}
else
{
head->d.x=head2->d.x;
head->next=bigsubresult(head2->next,head1->next);
}
}
return head;
}
Node *mul(Node *head1,Node *head2)//乘法包括符号的运算
{
Node *head;
head=(Node*)malloc(SIZE);
if(head1->d.x==head2->d.x)
head->d.x='+';
else head->d.x='-';
head->next=mulresult(head1->next,head2->next);
return head;
}
main()
{
Node *head1,*head2,*head3;
int i;
head1 = creatlink();
head2 = creatlink();
i=cmplink(head1,head2); // 比较两个数的大小
head1 = invert_list(head1);
head2 = invert_list(head2);
printf("加法的结果为:");
head3=add(head1,head2,i);
head3=invert_list(head3);
print_list(head3);
printf("减法的结果为:");
head3=sub(head1,head2,i);
head3=invert_list(head3);
print_list(head3);
printf("乘法的结果为:");
head3=mul(head1,head2);
head3=invert_list(head3);
print_list(head3);
}