You are on page 1of 7

#include"malloc.

h"
#include"stdio.h"
#define OK
#define ERROR
#define len

1
0
10

typedef struct DLNode


{
char data;
struct DLNode *prior;
struct DLNode *next;
}DLNode,*DLinkList;

//????

void CreateList(DLinkList &T)//??????????


{
T=(DLinkList)malloc(sizeof(DLNode));
if(!T)return;
T->data='+';
T->next=T;
T->prior=T;
}
void DataIn(DLinkList &T)
//?????????
{
char temp;
DLinkList p,s;
//s?????,p?T??????
p=T;
scanf("%c",&temp);
if(temp=='-')
{
T->data=temp;
scanf("%c",&temp);
//??????,?????T??????,????????
}
while(temp!=10)
//??????????
{
s=(DLinkList)malloc(sizeof(DLNode));
if(!s)return;
//????????????
s->data=temp;
s->prior=p;
p->next=s;
T->prior=s;
s->next=T;
p=s;
scanf("%c",&temp);
}
}
int Judge(DLinkList T)
{
int num=0;
int flag=0;
(??????4???)
DLinkList p;
p=T;
if(p->next==p)
{
return ERROR;
}

//???????????
//?????????
//?????????,num-flag????????????

//????

else
{
p=p->next;
//????
while(p!=T)
//??????,?????????
{
num++;
if(p->data==',')
//??????????
{
if(num==1)return ERROR;
else if(flag==0)
//?????????????4?,??????????4?
{
if((num-flag-1)>4)return ERROR; //?????
}
else
{
if((num-flag-1)!=4)return ERROR;//?????
}
flag=num;
}
else if(p->data <'0'||p->data>'9')
//??????
????
{
return ERROR;
}
p=p->next;
}
if(flag==0)
{if(num>4)return ERROR;}
else if(num-flag!=4)return ERROR;
return OK;
}
}
void FreeList(DLinkList T)
//???????????
{
DLinkList p,q;
p=T->next;
while(p!=T)
{
q=p->next;
p->prior->next=p->next;
p->next->prior=p->prior;
free(p);
p=q;
}
free(T);
}
int Compare(DLinkList first,DLinkList second)
d???OK,
{
int i=0,j=0,flag=0;
DLNode *p,*q;
p=first->next;
while(p!=first){i++;p=p->next;}
q=second->next;

//?????????,first>=secon

while(q!=second){j++;q=q->next;}
if(i>j)return OK;
//??????
???first????OK
else if(i==j)
//?????,
???????????????
{
p=first->next;
q=second->next;
while(p!=first&&q!=second&&flag==0)
{
if(p->data==','){p=p->next;q=q->next;}
else
{
if(p->data<q->data){flag=1;return ERROR;}
p=p->next;q=q->next;
}
}
return OK;
}
else return ERROR;
}
void AddTwoList(DLinkList &first,DLinkList &second,DLinkList &third)
//??????
{
int carry=0,flag=0;
DLinkList s,p,q;
DLinkList pfirst,psecond,pthird;
pfirst=first->prior;
psecond=second->prior;
pthird=third;
//---------------------------------------------------------------------------------------------------------//---------------------------------------------------------------------------------------------------------if((first->data=='+'&&second->data=='+')||(first->data=='-'&&second->dat
a=='-'))//???????????
{
third->data=first->data;
while(pfirst!=first&&psecond!=second)
{
flag++;
s=(DLinkList)malloc(sizeof(DLNode));
if(pfirst->data==',')
//?????,?????
{flag=0;s->data=pfirst->data;}
else
{
s->data=carry+pfirst->data+psecond->data
-2*'0'; //???????s->data?
carry=0;
if(s->data>=len)
//????10,???10,????carry?1
{
s->data=s->data-len;
carry=1;
}
}

third->next=s;
//????????third??
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;
pfirst=pfirst->prior;
psecond=psecond->prior;
}
while(pfirst!=first)//?first?????,?????????third??--------------------------------------------{
flag++;
s=(DLinkList)malloc(sizeof(DLNode));
if(pfirst->data==',')
//?????,?????
{
flag=0;s->data=pfirst->data;
}
else
{
s->data=carry+pfirst->data-'0';
//???????s->data?
carry=0;
if(s->data>=len)
//????10,???10,????carry?1
{
s->data=s->data-len;
carry=1;
}
}
third->next=s;
//????????third??
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;
pfirst=pfirst->prior;
}
if(carry==1)
{
if(flag==4)
{
s=(DLinkList)malloc(sizeof(DLNode));
//???,??
?4??????????
s->data=',';
third->next=s;
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;
}
s=(DLinkList)malloc(sizeof(DLNode));
//???,??
?????
s->data=carry;
third->next=s;
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;

}
}
//-------------------------------------------------------------------------------------------------------------//-------------------------------------------------------------------------------------------------------------else
//first?second???????????
{
third->data=first->data ;
while(pfirst!=first&&psecond!=second)
{
s=(DLinkList)malloc(sizeof(DLNode));
if(pfirst->data==',')
//?????,?????
{s->data=pfirst->data;}
else
{
s->data=carry+pfirst->data-psecond->data
;//???????s->data?
carry=0;
if(s->data<0)
//????0,???10,????carry?-1
{
s->data=s->data+len;
carry=-1;
}
}
third->next=s;
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;
pfirst=pfirst->prior;
psecond=psecond->prior;
}
while(pfirst!=first)//first?????-----------------------------------------------------------------{
s=(DLinkList)malloc(sizeof(DLNode));
if(pfirst->data==',')
//?????,?????
{s->data=pfirst->data;}
else
{
s->data=carry+pfirst->data-'0';
//???????s->data?
carry=0;
if(s->data<0)
//????0,???10,????carry?-1
{
s->data=s->data+len;
carry=-1;
}
}
third->next=s;
s->prior=third;
pthird->prior=s;
s->next=pthird;
pthird=s;

pfirst=pfirst->prior;
}
p=third->next;
q=third;
while(p->next!=third&&p->data==0)
//??????????
{
q->next=p->next;p->next->prior=q;free(p);p=q->next;
if(p->data==',')
{q->next=p->next;p->next->prior=q;free(p);p=q->next;}
}
if((p->next==third)&&p->data==0){third->data=' ';}
}
}
//--------------------------------------------------------------------------------------------------------------------//--------------------------------------------------------------------------------------------------------------------void DataOut(DLinkList T)
//???????
{
DLinkList p;
printf("\n?????:");
if(T->data=='+')T->data=' ';
printf("%c",T->data);
p=T->next;
while(p!=T)
{
if(p->data!=',')p->data=p->data+'0';
//?????A
SC11?,???????
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
main()
{
int i=1;
char goon;
DLinkList first,second,third,temp;
while(i<80){printf("*");i++;}//?????????*
printf("?????????\n");
printf("????: -1234,5678,9000\n");
i=1;
while(i<80){printf("*");i++;}
printf("\n");
do{CreateList(first);
CreateList(second);
CreateList(third);
printf("????????:");
DataIn(first);
//???????
while(Judge(first)==ERROR)
??????
{
FreeList(first);
printf("????!\n????????:");
CreateList(first);
DataIn(first);

//????

//??????

Judge(first);
}
printf("\n???????:");
DataIn(second);
//???????
while(Judge(second)==ERROR)
//??????
???
{
FreeList(second);
printf("????!\n????????:");
CreateList(second);
DataIn(second);
Judge(second);
}
if(Compare(first,second)==ERROR){temp=first;first=second;second=temp;}
//???????????????
AddTwoList(first,second,third);
//??????
DataOut(third);
//??????
FreeList(first);
//??????
FreeList(second);
FreeList(third);
printf("\n?????(y/n):");
//???????
scanf("%c",&goon);
getchar();
printf("\n\n");
}while(goon=='y'||goon=='Y');
//???n???,????
return;
}

You might also like