作业久调不成功啊,急!!
是一个用链表实现任意长整数相加减的程序,就是不行啊,请各位大虾给看看#include <stdio.h>
struct Node{
int data;
struct Node *pre;
struct Node *next;
}Node,*node;
struct Hnode{
int size;
int num;
struct Node* tail;
struct Node* next;
}Hnode,*hnode;
void Lin(struct Hnode* hnode1)
{
struct Node* node1,*p;
int inum;
char ch;
hnode1=(struct Hnode*) malloc (sizeof(struct Hnode));
hnode1->num=0;
hnode1->next=NULL;
scanf("%d",&inum);
scanf("%c",&ch);
if (inum>0) hnode1->size=0;
else hnode1->size=1;
if (ch!=';')
{
node1=(struct Node*) malloc(sizeof(struct Node));
node1->data=inum;
node1->next=NULL;
node1->pre=NULL;
p=node1;
if (inum>0)
hnode1->size=0;
else hnode1->size=1;
hnode1->num=1;
hnode1->next=node1;
}
while(ch!=';')
{
scanf("%d",&inum);
scanf("%c",&ch);
node1=(struct Node*) malloc(sizeof(struct Node));
node1->data=inum;
node1->next=NULL;
p->next=node1;
node1->pre=p;
p=node1;
hnode1->num=hnode1->num+1;
}
hnode1->tail=p;
};
void Lout(struct Hnode* hnode1)
{
struct Node* p;
p=hnode1->next;
while(p->next)
{
printf("%d",p->data);
printf(",");
p=p->next;
}
};
void Ladd(struct Hnode* hnode1,struct Hnode* hnode2,struct Hnode* hnode3)
{
struct Node* p;
struct Node* q;
struct Node* r;
int i=0,cp=0;
p=hnode1->tail;
q=hnode2->tail;
hnode3=new(hnode);
while((i<=hnode1->num) && (i<=hnode2->num))
{
r=new(Node);
r->data=p->data+q->data+cp;
if ((r->data)>10000)
{r->data=r->data-10000;
cp=1;}
else cp=0;
i=i+1;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
p=p->pre;
q=q->pre;
}
while(i<hnode2->num)
{
r=(struct Node*) malloc(sizeof(struct Node));
r->data=q->data;
i++;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
q=q->pre;
}
while(i<hnode1->num)
{
r=(struct Node*) malloc(sizeof(struct Node));
r->data=p->data;
i++;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
p=p->pre;
}
if (cp=1)
{
r=(struct Node*) malloc(sizeof(Node));
r->data=1;
i++;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
p=p->pre;
}
};
void Lsub(struct Hnode* hnode1,struct Hnode* hnode2,struct Hnode* hnode3)
{
struct Node *p,*q,*r;
int i=0,cp=0;
p=hnode1->tail;
q=hnode2->tail;
hnode3=(struct Hnode*) malloc(sizeof(Hnode));
while((i<=hnode1->num) && (i<=hnode2->num))
{
r=new(Node);
r->data=p->data+q->data-cp;
if (r->data<0)
{r->data=r->data+1000;
cp=1;}
else cp=0;
i=i+1;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
p=p->pre;
q=q->pre;
}
while(i<hnode2->num)
{
r=(struct Node*) malloc(sizeof(struct Node));
r->data=q->data-cp;
cp=0;
i++;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
q=q->pre;
}
while(i<hnode1->num)
{
r=(struct Node*) malloc(sizeof(struct Node));
r->data=p->data-cp;
cp=0;
i++;
r->next=hnode3->next;
hnode3->next=r;
r->next->pre=r;
p=p->pre;
}
if (r->data>0)
hnode3->size=1;
else hnode3->size=0;
};
void LAS(struct Hnode* hnode1,struct Hnode* hnode2,struct Hnode* hnode3)
{
int i=hnode1->size*hnode2->size;
int j=hnode1->size+hnode2->size;
if((i==0) && (j==1))
Lsub(hnode1,hnode2,hnode3);
else Ladd(hnode1,hnode2,hnode3);
};
void main()
{
struct Node* hnode1,*hnode2,*hnode3;
Lin(hnode1);
Lin(hnode2);
Lin(hnode3);
LAS(hnode1,hnode2,hnode3);
Lout(hnode3);
}