/*
题意:大数相加减相乘
单链表的运用
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
char c;
int flag;
typedef struct node
{
char data;
struct node *next;
} Node;
Node *a[10];
Node *create_link()
{
Node *head,*p;
head=p=NULL;
char m;
while(1)
{
scanf("%c",&m);
if(m==‘\n‘||m==‘+‘||m==‘-‘||m==‘*‘)
{
if(m==‘+‘)
c=‘+‘;
else if(m==‘-‘)
c=‘-‘;
else if(m==‘*‘)
c=‘*‘;
break;
}
p=(Node *)malloc(sizeof(Node));
p->data=m;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head=p;
}
}
return head;
}
Node *flash_back(Node *link)
{
Node *p=NULL,*head=NULL;
while(link)
{
p=(Node *)malloc(sizeof(Node *));
p->data=link->data;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head=p;
}
link=link->next;
}
return head;
}
void comp(Node *p1,Node *p2)
{
while(p1&&p2)
{
if(p1->data>p2->data)
flag=1;
else if(p1->data<p2->data)
flag=0;
p1=p1->next;
p2=p2->next;
}
if(p1)
flag=1;
else if(p2)
flag=0;
}
Node *add(Node *p1,Node *p2)
{
char s;
int k=0;
Node *p,*head;
p=head=NULL;
while(p1&&p2)
{
s=p1->data+p2->data-‘0‘+k;
k=0;
if(s>‘9‘)
{
s=s-10;
k=1;
}
p=(Node *)malloc(sizeof(Node));
p->data=s;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head=p;
}
p1=p1->next;
p2=p2->next;
}
while(p1)
{
p=(Node *)malloc(sizeof(Node));
s=k+p1->data;
k=0;
if(s>‘9‘)
{
s=s-10;
k=1;
}
p->data=s;
p->next=head;
head=p;
p1=p1->next;
}
while(p2)
{
p=(Node *)malloc(sizeof(Node));
s=k+p2->data;
k=0;
if(s>‘9‘)
{
s=s-10;
k=1;
}
p->data=s;
p->next=head;
head=p;
p2=p2->next;
}
if(k)
{
p=(Node *)malloc(sizeof(Node));
p->data=k+‘0‘;
p->next=head;
head=p;
}
return head;
}
Node *sub(Node *p1,Node *p2)
{
char s;
int k=0;
Node *p,*head;
p=head=NULL;
while(p1&&p2)
{
if(p1->data<p2->data)
{
s=p1->data+10-p2->data+‘0‘-k;
k=1;
}
else
{
s=p1->data-p2->data+‘0‘-k;
k=0;
}
p=(Node *)malloc(sizeof(Node));
p->data=s;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->next=head;
head=p;
}
p1=p1->next;
p2=p2->next;
}
while(p1)
{
p=(Node *)malloc(sizeof(Node));
s=p1->data-k;
k=0;
p->data=s;
p->next=head;
head=p;
p1=p1->next;
}
return head;
}
void print_link(Node *p);
Node *mul(Node *p1,Node *p2)
{
int i=0,j=0;
int s,k;
Node *head,*head1,*head2,*tail,*p;
head1=p1;
while(head1)
{
k=0;
head2=p2;
head=tail=NULL;
while(head2)
{
s=(head2->data-‘0‘)*(head1->data-‘0‘)+k;
k=0;
k=s/10;
s=s%10;
p=(Node *)malloc(sizeof(Node *));
p->data=s+‘0‘;
p->next=NULL;
if(head==NULL)
{
head=p;
tail=p;
}
else
{
tail->next=p;
tail=p;
}
head2=head2->next;
}
if(k)
{
p=(Node *)malloc(sizeof(Node *));
p->data=k+‘0‘;
p->next=NULL;
tail->next=p;
tail=p;
}
int i1=i;
while(i1>0)
{
p=(Node *)malloc(sizeof(Node *));
p->data=‘0‘;
p->next=head;
head=p;
i1--;
}
a[i]=head;
i++;
head1=head1->next;
}
Node *mu=NULL;
if(i==1)
{
Node *mk=(Node *)malloc(sizeof(Node));
mk->data=‘0‘;
mk->next=NULL;
return add(mk,a[0]);
}
else if(i==2)
return add(a[0],a[1]);
else
{
mu=add(a[0],a[1]);
for(j=2;j<i;j++)
{
mu=flash_back(mu);
mu=add(mu,a[j]);
}
return mu;
}
}
void print_link(Node *p)
{
Node *head;
head=p;
while(p)
{
if(head->data==‘0‘&&p->next)
{
head=p->next;
p=p->next;
continue;
}
printf("%c",p->data);
p=p->next;
}
/* while(p)
{
printf("%c",p->data);
p=p->next;
}*/
putchar(‘\n‘);
return ;
}
int main(void)
{
Node *p1,*p2,*p3;
int i;
flag=1;
for(i=0; i<10; i++)
a[i]=NULL;
p1=create_link();
p2=create_link();
if(c==‘+‘)
p3=add(p1,p2);
else if(c==‘-‘)
{
comp(p1,p2);
if(flag)
{
p3=sub(p1,p2);
}
else
{
putchar(‘-‘);
p3=sub(p2,p1);
}
}
else if(c==‘*‘)
{
p3=mul(p1,p2);
}
print_link(p3);
return 0;
}