link Jiafa(link L1,link L2)//加法运算
{
link p,q,x,y,z,t,L3,L4;
L3=(link)malloc(sizeof(linknode));
L3->next=NULL;
L3->prior=NULL;//先建立一个带头结点的双向链表
x=(link)malloc(sizeof(linknode));
y=(link)malloc(sizeof(linknode));
x=L1;
y=L2;
while(x->data!='.')//寻找小数点的位置
{
x=x->next;
}
while(y->data!='.')
{
y=y->next;
}
while(x->next&&y->next)
{
x=x->next;
y=y->next;
}
q=(link)malloc(sizeof(linknode));
q=L3;
if(!x->next&&y->next)//处理两链表小数点位数不同的情况
{
z=(link)malloc(sizeof(linknode));
z=y;
while(y->next)
{
y=y->next;
}
while(y!=z)
{
p=(link)malloc(sizeof(linknode));
p->data=y->data;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
y=y->prior;
}
}
if(!y->next&&x->next)
{
z=(link)malloc(sizeof(linknode));
z=x;
while(x->next)
{
x=x->next;
}
while(x!=z)
{
p=(link)malloc(sizeof(linknode));
p->data=x->data;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
x=x->prior;
}
}//处理两链表小数点位数不同的情况结束
if(!x->next&&!y->next)//位数相同时设的监视变量
{
p=(link)malloc(sizeof(linknode));
p=x;
}
while(x!=NULL&&y!=NULL)//小数部分先相加
{
if(x->data!='.'&&y->data!='.')//处理小数点
{
if(x->data+y->data-48<='9'&&p->frep==0)
{
p=(link)malloc(sizeof(linknode));//生成新结点
p->data=x->data+y->data-48;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
else
if(x->data+y->data-48<='9'&&p->frep==1)
{
if(x->data+y->data-47<='9')
{
p=(link)malloc(sizeof(linknode));//生成新结点
p->data=x->data+y->data-47;
p->frep=0;
}
else
{
p=(link)malloc(sizeof(linknode));//生成新结点
p->data='0';
p->frep=1;
}
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
else
if(x->data+y->data-48>'9'&&p->frep==0)
{
p=(link)malloc(sizeof(linknode));
p->data=x->data+y->data-':';
p->frep=1;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
else
if(x->data+y->data-48>'9'&&p->frep==1)
{
p=(link)malloc(sizeof(linknode));
p->data=x->data+y->data-':'+1;
p->frep=1;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
x=x->prior;
y=y->prior;
}
else
{
if(q->frep==0)
{
p=(link)malloc(sizeof(linknode));
p->data='.';
p->frep=0;
}//if
else
{
p=(link)malloc(sizeof(linknode));
p->data='.';
p->frep=1;
}//else
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
x=x->prior;
y=y->prior;
}//处理小数点
}//while循环
if(!x&&y)
{
while(y)
{
if(p->frep==0)
{
p=(link)malloc(sizeof(linknode));
p->data=y->data;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
y=y->prior;
}
if(p->frep==1&&y->data<'9')
{
p=(link)malloc(sizeof(linknode));
p->data=y->data+1;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
y=y->prior;
}
if(p->frep==1&&y->data=='9')
{
p=(link)malloc(sizeof(linknode));
p->data='0';
p->frep=1;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
y=y->prior;
}
}//while循环
}//if
if(x&&!y)
{
while(x)
{
if(p->frep==0)
{
p=(link)malloc(sizeof(linknode));//生成新结点
p->data=x->data;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
x=x->prior;
}
if(p->frep==1&&x->data<'9')
{
p=(link)malloc(sizeof(linknode));
p->data=x->data+1;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
x=x->prior;
}
if(p->frep==1&&x->data=='9')
{
p=(link)malloc(sizeof(linknode));
p->data='0';
p->frep=1;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
x=x->prior;
}
}//while循环
}
if(!x&&!y&&p->frep==1)
{
p=(link)malloc(sizeof(linknode));
p->data='1';
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
L3=L3->next;
L3->prior=NULL;
t=L3;
while(t->next)
{
t=t->next;
}
L4=(link)malloc(sizeof(linknode));
L4->next=NULL;
L4->prior=NULL;
q=L4;
while(t)
{
p=(link)malloc(sizeof(linknode));
p->data=t->data;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
t=t->prior;
}
L4=L4->next;
L4->prior=NULL;
return L4;
}//结束加法运算
link Input(link L) //从文件输入双链表
{
link p,q;
char ch,filename[30];
int i;
FILE *fp;
L=(link)malloc(sizeof(linknode));
if(!L)
exit(0);
L->prior=L->next=NULL;
q=L;
printf("输入文件名及路径:");
scanf("%s",filename);
if((fp=fopen(filename,"r"))==NULL)
{
printf("Can not open the file!\n");
exit(0);
}
while(!feof(fp))
{
ch=fgetc(fp);
p=(link)malloc(sizeof(linknode));
p->data=ch;
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
p=q->prior;
q->prior->prior->next=p;
p->next=NULL;
p->prior=q->prior->prior;
q=p;
p=L->next;
while(p->next)
{
if(p->data=='.')
break;
p=p->next;
}
if(p->next==NULL)
{
p=(link)malloc(sizeof(linknode));
p->data='.';
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
for(i=4;i>0;i--)
{
p=(link)malloc(sizeof(linknode));
p->data='0';
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
}
else if(!p->next->next)
{
p=p->next;
for(i=2;i>0;i--)
{
p=(link)malloc(sizeof(linknode));
p->data='0';
p->frep=0;
p->next=q->next;
q->next=p;
p->prior=q;
q=p;
}
}
q=L;
L=q->next;
q->next->next->prior=L;
L->prior=NULL;
return L;
}
void outprint(link L) //输出结果
{
link p,q;
char outfile[20];
FILE *fp;
scanf("%s",outfile);
if((fp=fopen(outfile,"w"))==NULL) //打开文件
{
printf("cannot open file.\n");
exit(0);
}
p=L;
q=L;
while(1)
{
if(p->data=='0')
{
p=p->next;
continue;
}
else
break;
}
while(q->next)
{
q=q->next;
}
while(1)
{
if(q->data=='0')
{
q=q->prior;
continue;
}
else
break;
}
if(q->data=='.')
{
q=q->prior;
}
while(p!=q) //输出结果数字
{
fputc((p->data),fp);
p=p->next;
}
fputc((q->data),fp);
fclose(fp); //关闭文件
}
void main()//主函数
{
link p;
link L10,L20,L1,L11,L12,L2,L21,L22,L3;
L1=L11=L12=Input(L10);
p=L1;
while(p)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
L2=L21=L22=Input(L20);
p=L2;
while(p)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
//*****************************************************************************//
//以下是加法运算
if(L1->data!='-'&&L2->data!='-')
{
L3=(link)malloc(sizeof(linknode));
L3=Jiafa(L1,L2);
printf("输入加法结果保存文件:");
outprint(L3);
}
else
{
if(L1->data=='-'&&L2->data=='-')
{
L1=L1->next;
L1->next->prior=L1;
L1->prior=NULL;
L2=L2->next;
L2->next->prior=L2;
L2->prior=NULL;
L3=(link)malloc(sizeof(linknode));
L3=Jiafa(L1,L2);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
printf("输入加法结果保存文件:");
outprint(L3);
}
else
{
if(L1->data!='-'&&L2->data=='-')
{
L2=L2->next;
L2->next->prior=L2;
L2->prior=NULL;
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L1,L2);
if(!L3)
{
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L2,L1);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
}
printf("输入加法结果保存文件:");
outprint(L3);
}
else
{
L1=L1->next;
L1->next->prior=L1;
L1->prior=NULL;
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L2,L1);
if(!L3)
{
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L1,L2);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
}
printf("输入加法结果保存文件:");
outprint(L3);
}
}
}
//******************************************************************************//
//以下是减法运算
if(L11->data!='-'&&L21->data=='-')
{
L21=L21->next;
L21->next->prior=L21;
L21->prior=NULL;
L3=(link)malloc(sizeof(linknode));;
L3=Jiafa(L21,L11);
printf("输入减法结果保存文件:");
outprint(L3);
}
else
if(L11->data=='-'&&L21->data=='-')
{
L11=L11->next;
L11->next->prior=L11;
L11->prior=NULL;
L21=L21->next;
L21->next->prior=L21;
L21->prior=NULL;
L3=(link)malloc(sizeof(linknode));
L3=Jianfa(L21,L11);
if(!L3)
{
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L11,L21);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
}
printf("输入减法结果保存文件:");
outprint(L3);
}
else
if(L11->data!='-'&&L21->data!='-')
{
L3=(link)malloc(sizeof(linknode));
L3=Jianfa(L11,L21);
if(!L3)
{
L3=(link)malloc(sizeof(linknode));;
L3=Jianfa(L21,L11);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
}
printf("输入减法结果保存文件:");
outprint(L3);
}
else
{
L11=L11->next;
L11->next->prior=L11;
L11->prior=NULL;
L3=(link)malloc(sizeof(linknode));
L3=Jiafa(L11,L12);
p=(link)malloc(sizeof(linknode));
p->data='-';
p->frep=0;
p->next=L3;
L3->prior=p;
L3=p;
printf("输入减法结果保存文件:");
outprint(L3);
}
}//主函数结束