任意长整数的加法
问题描述:设计一个程序实现两个任意长的整数的求和运算。要求:
利用双向循环链表,设计一个实现任意长的整数进行加法运算的演示程序。要求输入和输出每四位一组,组间用逗号隔开。如:1,0000,0000,0000,0000。
用C语言实现
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
int data;
struct Node *prior;
struct Node *next;
}LNode,*LinkList;
void InitLink(LinkList *L)
{
*L=(LinkList)malloc(sizeof(LNode));
(*L)->prior=*L;
(*L)->next=*L;
}
void Delete(LinkList L)
{
LinkList p=L->next;
LinkList temp;
while(p!=L)
{
temp=p;
p->next;
free(temp);
}
L->next=L;
L->prior=L;
}
void Append(LinkList L,int s)
{
LinkList p=L;
LinkList temp=(LinkList)malloc(sizeof(LNode));
temp->next=L;
temp->data=s;
temp->prior=L->prior;
L->prior->next=temp;
L->prior=temp;
}
void Output(LinkList L)
{
LinkList p=L->next;
if(p==L)
{
printf("0");
return;
}
if(L->data==-1)
printf("-");
printf("%d",p->data);
p=p->next;
while(p!=L)
{
printf(",%04d",p->data);
p=p->next;
}
}
void Output2(LinkList L)
{
LinkList p=L->prior;
if(p==L)
{
printf("0");
return;
}
if(L->data==-1)
printf("-");
printf("%d",p->data);
p=p->prior;
while(p!=L)
{
printf(",%04d",p->data);
p=p->prior;
}
}
void Sum_Linksum(LinkList A,LinkList B,LinkList C)
{
LinkList a=A->prior;
LinkList b=B->prior;
int sum;
int carry=0;
C->data=1;
if(A->data<0)
C->data=-1;
while(a!=A&&b!=B)
{
sum=a->data+b->data+carry;
carry=sum/10000;
Append(C,sum%10000);
a=a->prior;
b=b->prior;
}
while(a!=A)
{
sum=a->data+carry;
carry=sum/10000;
Append(C,sum%10000);
a=a->prior;
}
while(b!=B)
{
sum=b->data+carry;
carry=sum/10000;
Append(C,sum%10000);
b=b->prior;
}
if(carry)Append(C,carry);
}
void DelZero(LinkList L)
{
LinkList p=L;
LinkList q;
while(p->prior!=L)
{
if(p->prior->data!=0)
break;
else
{
q=p->prior;
p->prior=q->prior;
q->prior->next=p;
free(q);
}
}
}
void Sub_Linknum(LinkList A,LinkList B,LinkList C)
{
LinkList HA=A;
LinkList HB=B;
LinkList a=HA->prior;
LinkList b=HB->prior;
int sub;
int borrow=0;
int flag=0;
C->data=1;
while(a!=HA&&b!=HB)
{
if(a->data<b->data) flag=1;
if(a->data>b->data) flag=0;
a=a->prior;
b=b->prior;
}
if(b!=HB) flag=1;
if(a!=HA) flag=0;
if(flag==1)
{
HA=B;
HB=a;
C->data=-1;
}
a=HA->prior;
b=HB->prior;
while(b!=HB){
sub=a->data-borrow-b->data;
if(sub>=0)
borrow=0;
else
{
borrow=1;
sub+=10000;
}
Append(C,sub);
a=a->prior;
b=b->prior;
}
while(a!=HA){
sub=a->data-borrow;
if(sub>=0)
borrow=0;
else
{
borrow=1;
sub+=10000;
}
Append(C,sub);
a=a->prior;
}
DelZero(C);
}
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int i,k;
int dex=0,flag=0;
char ch;
LinkList num[3];
printf("\nplease input Number:\n");
for(i=0;i<3;i++)
InitLink(&num[i]);
while(scanf("%d",&k)!=EOF)
{
if(flag==0)
{
if(k<0)
{
num[dex]->data=-1;
k*=-1;
}
else if(k>0)
num[dex]->data=1;
else
num[dex]->data=0;
flag=1;
}
ch=getchar();
Append(num[dex],k);
if(ch!=',')
{
if(dex==1)
{
if(num[0]->data==0&&num[1]->data==0)
break;
Output(num[0]);
printf("+");
Output(num[1]);
printf("=");
if(num[0]->data==num[1]->data)
Sum_Linknum(num[0],num[1],num[2]);
else if(num[0]->data>0)
Sub_Linknum(num[0],num[1],num[2]);
else
Sub_Linknum(num[0],num[1],num[2]);
Output(num[2]);
puts("");
for(i=0;i<3;i++)
Delete(num[i]);
}
flag=0;
dex=!dex;
}
}
system("pause");
return 0;
}
这个代码错在哪里,怎么运行不出来,求大神帮忙!