#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct CR
{
int m;
struct CR *next;
};
typedef struct CR CR;
struct DATEFOR
{
int x;
int flag;
struct DATEFOR *next;
};
typedef struct DATEFOR DF;
CR *createCR(char *str)
{
CR *head,*p,*s;
int i;
head=(CR *)malloc(sizeof(CR));
p=head;
i=strlen(str)-1;
while(p!=NULL&&i>=0)
{
s=(CR *)malloc(sizeof(CR));
s->m=str[i]-'0';
s->next=NULL;
p->next=s;
p=s;
i--;
}
p->next=NULL;
return head;
}
int CRlen(CR *head)
{
CR *p=head->next;
int n;
n=0;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
int rtCRvl(CR *head,int key)
{
CR *p=head->next;
int i=0;
while(p!=NULL&&i!=key)
{
p=p->next;
i++;
}
if(p!=NULL)
{
return p->m;
}
return -1;
}
DF *createDF(CR *str1,CR *str2)
{
DF *head;
DF *p,*s,*q;
int n,m;
int i=0,j=0;
int v1,v2;
n=CRlen(str1);
m=CRlen(str2);
head=(DF *)malloc(sizeof(DF));
head->flag=0;
head->next=NULL;
p=head;
while(p!=NULL&&i<n&&j<m)
{
q=p;
s=(DF *)malloc(sizeof(DF));
v1=rtCRvl(str1,i);
v2=rtCRvl(str2,j);
if(q==head)
{
if(v1+v2>=10)
{
s->x=v1+v2-10;
s->flag=1;
}else
{
s->x=v1+v2;
s->flag=0;
}
}else
{
if(v1+v2>=10)
{
s->x=v1+v2+q->flag-10;
s->flag=1;
}else if(v1+v2+q->flag>=10)
{
s->x=v1+v2+q->flag-10;
s->flag=1;
}else
{
s->x=v1+v2+q->flag;
s->flag=0;
}
}
p->next=s;
p=s;
i++;
j++;
}
while(i<n)
{
s=(DF *)malloc(sizeof(DF));
v1=rtCRvl(str1,i);
if(v1+p->flag>=10)
{
s->x=v1+p->flag-10;
s->flag=1;
}else
{
s->x=v1+p->flag;
s->flag=0;
}
p->next=s;
p=s;
i++;
}
while(j<m)
{
s=(DF *)malloc(sizeof(DF));
v2=rtCRvl(str1,j);
if(v2+p->flag>=10)
{
s->x=v2+p->flag-10;
s->flag=1;
}else
{
s->x=v2+p->flag;
s->flag=0;
}
p->next=s;
p=s;
j++;
}
p->next=NULL;
return head;
}
DF *rev(DF *head)
{
DF *p,*q,*t;
p=head->next;
t=NULL;
while(p)
{
q=p;
p=p->next;
q->next=t;
t=q;
}
head->next=t;
return head;
}
void display(DF *head)
{
DF *p=head->next;
while(p!=NULL)
{
printf("%d",p->x);
p=p->next;
}
printf("\n");
}
int main()
{
char str1[]="111111123234545454554";
char str2[]="1888888845454554";
CR *head1,*head2;
DF *df1,*df2;
head1=createCR(str1);
head2=createCR(str2);
df1=createDF(head1,head2);
display(df1);
df2=rev(df1);
display(df2);
return 0;
}
#include<stdlib.h>
#include<string.h>
struct CR
{
int m;
struct CR *next;
};
typedef struct CR CR;
struct DATEFOR
{
int x;
int flag;
struct DATEFOR *next;
};
typedef struct DATEFOR DF;
CR *createCR(char *str)
{
CR *head,*p,*s;
int i;
head=(CR *)malloc(sizeof(CR));
p=head;
i=strlen(str)-1;
while(p!=NULL&&i>=0)
{
s=(CR *)malloc(sizeof(CR));
s->m=str[i]-'0';
s->next=NULL;
p->next=s;
p=s;
i--;
}
p->next=NULL;
return head;
}
int CRlen(CR *head)
{
CR *p=head->next;
int n;
n=0;
while(p!=NULL)
{
n++;
p=p->next;
}
return n;
}
int rtCRvl(CR *head,int key)
{
CR *p=head->next;
int i=0;
while(p!=NULL&&i!=key)
{
p=p->next;
i++;
}
if(p!=NULL)
{
return p->m;
}
return -1;
}
DF *createDF(CR *str1,CR *str2)
{
DF *head;
DF *p,*s,*q;
int n,m;
int i=0,j=0;
int v1,v2;
n=CRlen(str1);
m=CRlen(str2);
head=(DF *)malloc(sizeof(DF));
head->flag=0;
head->next=NULL;
p=head;
while(p!=NULL&&i<n&&j<m)
{
q=p;
s=(DF *)malloc(sizeof(DF));
v1=rtCRvl(str1,i);
v2=rtCRvl(str2,j);
if(q==head)
{
if(v1+v2>=10)
{
s->x=v1+v2-10;
s->flag=1;
}else
{
s->x=v1+v2;
s->flag=0;
}
}else
{
if(v1+v2>=10)
{
s->x=v1+v2+q->flag-10;
s->flag=1;
}else if(v1+v2+q->flag>=10)
{
s->x=v1+v2+q->flag-10;
s->flag=1;
}else
{
s->x=v1+v2+q->flag;
s->flag=0;
}
}
p->next=s;
p=s;
i++;
j++;
}
while(i<n)
{
s=(DF *)malloc(sizeof(DF));
v1=rtCRvl(str1,i);
if(v1+p->flag>=10)
{
s->x=v1+p->flag-10;
s->flag=1;
}else
{
s->x=v1+p->flag;
s->flag=0;
}
p->next=s;
p=s;
i++;
}
while(j<m)
{
s=(DF *)malloc(sizeof(DF));
v2=rtCRvl(str1,j);
if(v2+p->flag>=10)
{
s->x=v2+p->flag-10;
s->flag=1;
}else
{
s->x=v2+p->flag;
s->flag=0;
}
p->next=s;
p=s;
j++;
}
p->next=NULL;
return head;
}
DF *rev(DF *head)
{
DF *p,*q,*t;
p=head->next;
t=NULL;
while(p)
{
q=p;
p=p->next;
q->next=t;
t=q;
}
head->next=t;
return head;
}
void display(DF *head)
{
DF *p=head->next;
while(p!=NULL)
{
printf("%d",p->x);
p=p->next;
}
printf("\n");
}
int main()
{
char str1[]="111111123234545454554";
char str2[]="1888888845454554";
CR *head1,*head2;
DF *df1,*df2;
head1=createCR(str1);
head2=createCR(str2);
df1=createDF(head1,head2);
display(df1);
df2=rev(df1);
display(df2);
return 0;
}