#include #include #include #include struct cirlink { int x; struct cirlink *pre,*next; } *ps1, *ps2, *ps3; int count(struct cirlink *ps) { int co; struct cirlink *p, *pp; pp=p=ps->next; co=1; p=p->next; while (p != pp) { co++; p=p->next; } return co; } void printp(struct cirlink *ps,int maxl, int plen) { /* circle link list */ struct cirlink *p, *pp; int i; for (i=0; i<(maxl-plen); i++) printf(" "); pp=p=ps->pre; printf("%4d",p->x); p=p->pre; while (p != pp) { printf("%04d",p->x); p=p->pre; } printf("\n"); } struct cirlink * create() { char t[20]; int x,n,cc,s,ff; struct cirlink *pp,*p,*ps; printf("give me the data --> "); gets(t); x=strlen(t); ff=0; while (x > 4) { s=0; cc=4; while (cc >= 1) { s=s*10+(t[x-cc]-'0'); cc--; } if (s != 0) { if (ff == 0) { ff=1; ps=pp=p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=s; p->next=p->pre=p; } else { p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=s; p->pre=pp; p->next=ps; pp->next=p; pp=p; ps->pre=p; } } x-=4; } if (x > 0) { s=0; cc=x; while (cc >= 1) { s=s*10+(t[x-cc]-'0'); cc--; } if (s != 0) { p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=s; p->pre=pp; p->next=ps; pp->next=p; pp=p; ps->pre=p; } } return ps; } void main() { struct cirlink *p1, *p2, *pn1, *pn2, *p3, *p, *pp; int ff,ca,fa,fb,c1,c2,c3,max; p1=ps1=create(); p2=ps2=create(); pn1=p1->next; pn2=p2->next; fa=fb=ff=0; while ((!fa) && (!fb)) { /* long integer add */ if (ff == 0) { pp=p=ps3=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=p1->x+p2->x; p->pre=p->next=p; ff=1; ca=p->x/10000; p->x=p->x%10000; } else { p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=p1->x+p2->x+ca; ca=p->x/10000; p->x=p->x%10000; p->pre=pp; p->next=ps3; pp->next=p; ps3->pre=p; pp=p; } p1=pn1; pn1=pn1->next; p2=pn2; pn2=pn2->next; if (p1 == ps1) fa=1; if (p2 == ps2) fb=1; } /* while */ if (fb) { while (p1 != ps1) { /* long integer add */ p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=p1->x+ca; ca=p->x/10000; p->x=p->x%10000; p->pre=pp; p->next=ps3; pp->next=p; ps3->pre=p; pp=p; p1=pn1; pn1=pn1->next; } } else { while (p2 != ps2) { /* long integer add */ p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=p2->x+ca; ca=p->x/10000; p->x=p->x%10000; p->pre=pp; p->next=ps3; pp->next=p; ps3->pre=p; pp=p; p2=pn2; pn2=pn2->next; } } if (ca != 0) { p=(struct cirlink *)malloc(sizeof(struct cirlink)); p->x=ca; p->pre=pp; p->next=ps3; pp->next=p; ps3->pre=p; pp=p; } c1=count(ps1); c2=count(ps2); c3=count(ps3); printf("\n\n"); if (((c1-c2) >= 0) && ((c1-c3) >= 0)) max=c1; else if (((c2-c1) >= 0) && ((c2-c3) >= 0)) max=c2; else if (((c3-c1) >= 0) && ((c3-c2) >= 0)) max=c3; printf(" "); printp(ps1,max,c1); printf("+)"); printp(ps2,max,c2); printf("--"); for (ca=0; ca