已结贴√
问题点数:20 回复次数:10
这个程序为什么输入第一个数字后就提示已123.exe(我的程序名)停止工作?
//*两个多项式的输入必须依照幂次数的升序依次输入*//
#include
#include
#include
#define LENTH sizeof(struct Polyfactor)
#define NULL 0
typedef struct Polyfactor //*构造多项式因子*//
{
int coeff; //*多项式因子的系数*//
int power; //*多项式因子的幂次数*//
struct Polyfactor *next; //*用于指向下一个多项式因子的指针*//
};
struct Polyfactor *createpolyn(struct Polyfactor *head) //*建立链表存储多项式*//
{
struct Polyfactor *p1=NULL; //*设置两个指针用于创建链表*//
struct Polyfactor *p2=NULL;
int m=0;
int n=0;
while(m!='q'&&n!='q') //*当输入不是数字时多项式输入结束*//
{
printf("请输入多项式因子的系数:");
scanf("%d\n",m);
printf("请输入多项式因子的幂次数:");
scanf("%d\n",n);
if(head=NULL)
{
p1=(struct Polyfactor*)malloc(LENTH);
if(p1==NULL)
{exit(0);}
head=p1;
p2=p1;
}
else
{
p1=(struct Polyfactor*)malloc(LENTH);
if(p1==NULL)
{exit(0);}
else
{
p2->next=p1;
p2=p1;
}
p2->coeff=m;
p2->power=n;
}
} //*多项式的输入*//
return head;
}
struct Polyfactor *addpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p2,struct Polyfactor *p3) //*对链表进行插入,其中p3指向P1的前驱*//
{
if(p1->powerpower) //p1所指向的链表结点的幂次数比p2的小时,将p2插入p1后*//
{
p2->next=p1->next;
p1->next=p2;
}
else //p1所指向的链表结点的幂次数比p2的大时,将p2插入p1前*//
{
p2->next=p1;
p3->next=p2;
}
return head;
}
struct Polyfactor *delpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p3) //*对链表进行删除,其中P3指向要删除点的前驱*//
{
p3->next=p1->next; //*把要删除点的后继赋值给删除点的前驱的后继*//
return head;
}
void main()
{
struct Polyfactor *p,*p1,*p2,*p3=NULL;
struct Polyfactor *head1=NULL,*head2=NULL;
struct Polyfactor *createpolyn(struct Polyfactor *head1);
struct Polyfactor *addpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p2,struct Polyfactor *p3);
struct Polyfactor *delpolyn(struct Polyfactor *head,struct Polyfactor *p1,struct Polyfactor *p3);
head1=p1=createpolyn(head1); //*创建两个链表分别把两个头指针赋值给head1和head2*//
head2=p2=createpolyn(head2);
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;
do
{
printf("%dX^%d",p->coeff,p->power);
printf("+");
p=p->next;
}while(p); //*输出所得多项式的结果*//
}
这个程序为什么输入第一个数字后就提示已123.exe(我的程序名)停止工作?
搜索更多相关主题的帖子:
数字 提示 输入