谁帮我测试下这个代码找出为什么结果会那样
//*两个多项式的输入必须依照幂次数的升序依次输入,并且没有常数项*//#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define LENTH sizeof(struct Polyfactor)
//*构造多项式因子的结构体*//
typedef struct Polyfactor
{
//*声明多项式因子的系数*//
int coeff;
//*多项式因子的幂次数*//
int power;
//*用于指向下一个多项式因子的指针*//
struct Polyfactor *next;
}Factor;
//*建立链表存储多项式函数*//
Factor *createpolyn(Factor *head)
{
//*设置两个指针用于创建链表*//
Factor *p1=0;
Factor *p2=0;
int m=0;
int n=0;
//*当输入是q时多项式输入结束*//
do
{
printf("请输入多项式因子的系数:");
scanf("%d",&m);
printf("请输入多项式因子的幂次数:");
scanf("%d",&n);
if(head==0)
{
p1=(Factor*)malloc(LENTH);
if(p1==0)
{
exit(0);
}
head=p1;
p2=p1;
}
else
{
p1=(Factor*)malloc(LENTH);
if(p1==0)
{
exit(0);
}
else
{
p2->next=p1;
p2=p1;
}
p2->coeff=m;
p2->power=n;
}
}while(m!=0&&n!=0);
p2->next=0;
return head;
} //*多项式的输入*//
//*对链表进行插入,其中p3指向P1的前驱*//
Factor *addpolyn(Factor *head,Factor *p1,Factor *p2,Factor *p3)
{
//p1所指向的链表结点为尾结点时,并且P2没有指向链表尾结点*//
if(p1->power==0&&p1->coeff==0)
{
p1->power=p2->power;
p1->coeff=p2->coeff;
p1->next=p2->next;
return head;
}
//p1所指向的链表结点的幂次数比p2的小时,将p2插入p1后*//
if(p1->power<p2->power)
{
if(p3==p1)
{
head=p2;
p2->next=p1;
return head;
}
else
{
p2->next=p1->next;
p1->next=p2;
}
}
//p1所指向的链表结点的幂次数比p2的大时,将p2插入p1前*//
else
{
p2->next=p1;
p3->next=p2;
}
return head;
}
//*对链表进行删除,其中P3指向要删除点的前驱*//
Factor *delpolyn(Factor *head,Factor *p1,Factor *p3)
{
//*把要删除点的后继赋值给删除点的前驱的后继*//
p3->next=p1->next;
return head;
}
int main()
{
Factor *p=0,*p1=0,*p2=0,*p3=0;
Factor *head1=0,*head2=0;
Factor *createpolyn(Factor *head1);
Factor *addpolyn(Factor *head,Factor *p1,Factor *p2,Factor *p3);
Factor *delpolyn(Factor *head,Factor *p1,Factor *p3);
//*创建两个链表分别把两个头指针赋值给head1和head2*//
head1=p1=createpolyn(head1);
head2=p2=createpolyn(head2);
p3=p1;
do
{
//*两个多项式因子的幂次数相同*//
if(p1->power==p2->power)
{
if(p1->coeff==-p2->coeff)
{
head1=delpolyn(head1,p1,p3);
}
else
{
p1->coeff+=p2->coeff;
}
}
//*两个多项式的幂次数不同*//
else
{
head1=addpolyn(head1,p1,p2,p3);
}
p3=p1;
p1=p1->next;
p2=p2->next;
}while(p1&&p2);
//*当两个链表都还没指向链表结尾时*//
while(p2)
{
head1=addpolyn(head1,p1,p2,p3);
}
p=head1;
printf("所得的多项式:\n");
do
{
printf("%dX^%d",p->coeff,p->power);
printf("+");
p=p->next;
//*输出所得多项式的结果*//
}while(p);
return 0;
}
谁帮我测试下看下结果为什么会那样?
[ 本帖最后由 清风拂晓 于 2010-10-21 19:19 编辑 ]