/*
Test Instructions: Large number addition minus multiplication
The use of single-linked list
*/
#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;
I=s;
}
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;
I=s;
}
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;
I=s;
}
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;
}
The addition and subtraction multiplication of large data in linked list