📄 zcd.c
字号:
#include<stdio.h>
#include<stdlib.h>
typedef struct Dulnode
{
int cf;
int data;
struct Dulnode *prior;
struct Dulnode *next;
}Dulnode,*Dullist;
//———————————————————————————新类型定义—————————————————————————
Dullist Create_list(Dullist head,Dullist end)
{
Dullist p;
char a[50];
int i;
i=0;
head->cf=0;
head->data=0;
head->next=NULL;
head->prior=NULL;
printf("输入数据:\n");
scanf("%s",a);
if(a[0]=='-')
{
head->data=1; //负为1
do
{
a[i]=a[i+1];
i++;
}while(a[i]!='\0');
}
i=0;
while(a[i]!='\0')
i++;
if(a[0]<='9'&&a[0]>='0')
{
if(a[1]==','||a[1]=='\0')
{while(i!=-1)
{a[i+3]=a[i];i--;}
a[0]='0';a[1]='0';a[2]='0';}
if(a[2]==','||a[2]=='\0')
{while(i!=-1)
{a[i+2]=a[i];i--;}
a[0]='0';a[1]='0';}
if(a[3]==','||a[3]=='\0')
{while(i!=-1)
{a[i+1]=a[i];i--;}
a[0]='0';}
i=0;
do
{
p=(Dullist)malloc(sizeof(Dulnode));
p->next=NULL;
p->data=(a[i]-48)*1000+(a[i+1]-48)*100+(a[i+2]-48)*10+(a[i+3]-48);
p->cf=0;
p->prior=head;
head->next=p;
head=head->next;
i=i+5;
}while(!(a[i-1]=='\0'));
}
return p;
}
//—————————————————————————————输入算法———————————————————————
void add(Dullist head_1,Dullist end_1,Dullist head_2,Dullist end_2,Dullist head_3)
{
Dullist p,p2,temp;
int i,sign;
sign=0;
i=0;
head_3->data=0;
head_3->cf=0;
head_3->next=NULL;
head_3->prior=NULL;
if(head_1->data==head_2->data) //同号
{
if(head_1->data==1)
head_3->data=1;
if(head_1->data==0)
head_3->data=0;
p=(Dullist)malloc(sizeof(Dulnode));
p->data=0;
p->cf=0;
p->next=NULL;
p->prior=NULL;
head_3->next=p;
p->prior=head_3;
do
{
if(end_1->prior==NULL||end_2->prior==NULL)
{
if(end_1->prior==NULL&&end_2->prior==NULL)
break;
if(end_1->prior!=NULL&&end_2->prior==NULL)
{p->data=end_1->data;end_1=end_1->prior;}
if(end_1->prior==NULL&&end_2->prior!=NULL)
{p->data=end_2->data;end_2=end_2->prior;}
}
else
{p->data=end_1->data+end_2->data;end_1=end_1->prior;end_2=end_2->prior;}
if(i!=0)
p->data+=p->next->cf;
if(p->data>=10000)
{p->cf=1;p->data=p->data-10000;}
if(!(end_1->prior==NULL&&end_2->prior==NULL))
{p=(Dullist)malloc(sizeof(Dulnode));
p->data=0;
p->cf=0;
p->next=head_3->next; //前插
p->prior=head_3;
head_3->next->prior=p;
head_3->next=p;}
}while(++i);
}
if(head_1->data!=head_2->data) //异号
{
i=0;
p=head_1;
p2=head_2;
while(1)
{ //3
p=p->next;
p2=p2->next;
if(p->next==NULL&&p2->next!=NULL)
{temp=head_1;head_1=head_2;head_2=temp;temp=end_1;end_1=end_2;end_2=temp;break;}
if(p->next!=NULL&&p2->next==NULL)
break;
if(p->next==NULL&&p2->next==NULL)
{ //2
p=head_1;
p2=head_2;
while(!(p->next==NULL))
{ //1
i=1;
p=p->next;
p2=p2->next;
if(p->data==p2->data)
{if(p->next==NULL&&p2->next==NULL)
{sign=1;break;}
continue;}
if(p->data<p2->data)
{temp=head_1;head_1=head_2;head_2=temp;temp=end_1;end_1=end_2;end_2=temp;break;}
} //1
if(i==1)
break;
} //2
} //3
i=0;
if(sign==1)
head_3->data=0;
else
head_3->data=head_1->data;
p=(Dullist)malloc(sizeof(Dulnode));
p->data=0;
p->next=NULL;
p->cf=0;
p->prior=head_3;
head_3->next=p;
do
{
if(end_1->prior==NULL||end_2->prior==NULL)
{
if(end_1->prior==NULL&&end_2->prior==NULL)
break;
if(end_1->prior!=NULL&&end_2->prior==NULL)
{p->data=end_1->data;end_1=end_1->prior;}
}
else
{p->data=end_1->data-end_2->data;end_1=end_1->prior;end_2=end_2->prior;}
if(i!=0)
p->data+=p->next->cf;
if(p->data<0)
{p->cf=-1;p->data=p->data+10000;}
if(!(end_1->prior==NULL&&end_2->prior==NULL))
{p=(Dullist)malloc(sizeof(Dulnode));
p->cf=0;
p->data=0;
p->next=head_3->next; //前插
p->prior=head_3;
head_3->next->prior=p;
head_3->next=p;}
}while(++i);
}
}
//——————————————————————————向加算法—————————————————————————————
void print(Dullist p)
{
Dullist temp;
int i;
temp=p;
i=0;
printf("结果是:");
if(p->data==1)
putchar('-');
if(p->next->cf==1)
printf("1,");
do
{
temp=temp->next;
if(temp->next==NULL)
{if(temp->data==0&&p->next->cf==0)
{printf("0\n");return;}
}
if(p->next->cf!=0)
{i=1;break;}
if(temp->data!=0)
break;
}while(1);
do
{
if(i!=0)
{if(temp->data<=999&&temp->data>99)
printf("0");
if(temp->data<=99&&temp->data>9)
printf("00");
if(temp->data<=9)
printf("000");
}
i=1;
printf("%d",temp->data);
if(temp->next!=NULL)
putchar(',');
if(temp->next==NULL)
break;
temp=temp->next;
}while(1);
putchar('\n');
}
//—————————————————————————输出算法—————————————————————-
main()
{
Dullist head1,head2,head3,end1,end2;
head1=(Dullist)malloc(sizeof(Dulnode));
head2=(Dullist)malloc(sizeof(Dulnode));
head3=(Dullist)malloc(sizeof(Dulnode));
end1=Create_list(head1,end1);
end2=Create_list(head2,end2);
add(head1,end1,head2,end2,head3);
print(head3);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -