该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
if(s->data.coef!=0.0)
{
s->data.expn=p->data.expn;
r->next=s;
r=s;
}
p=p->next;
q=q->next;
break;
case 1:
s->data.coef=q->data.coef;
s->data.expn=q->data.expn;
r->next=s;
r=s;
q=q->next;
break;
}//switch
}//while
/*if(p!=NULL)
r->next=p;
else
if(q!=NULL)
r->next=q;
else
r->next=NULL;*/ //这样不行,q,p为局部变量,传不到主函数中去
while(p)
{
s=(LNode*)malloc(sizeof(LNode));
s->data.coef=p->data.coef;
s->data.expn=p->data.expn;
r->next=s;
r=s;
p=p->next;
}
while(q)
{
s=(LNode*)malloc(sizeof(LNode));
s->data.coef=q->data.coef;
s->data.expn=q->data.expn;
r->next=s;
r=s;
q=q->next;
}
r->next=NULL;
for(q=newp->next;q->next!=NULL;q=q->next)//合并同类项
for(p=q;p!=NULL&&p->next!=NULL;p=p->next)
if(q->data.expn==p->next->data.expn)
{
q->data.coef=q->data.coef+p->next->data.coef;
r=p->next;
p->next=p->next->next;
free(r);
}
return newp;
}
/*3、两多项式相减*/
polynomail subpolyn(polynomail pa,polynomail pb)
{
polynomail s,newp,q,p,r,Q;
p=pa->next;q=pb->next;
newp=(LNode*)malloc(sizeof(LNode));
r=newp;
while(p&&q)
{
s=(LNode*)malloc(sizeof(LNode));
switch(cmp(p->data,q->data))
{
case -1:
s->data.coef=p->data.coef;
s->data.expn=p->data.expn;
r->next=s;
r=s;
p=p->next;
break;
case 0:
s->data.coef=p->data.coef-q->data.coef;
if(s->data.coef!=0.0)
{
s->data.expn=p->data.expn;
r->next=s;
r=s;
}
p=p->next;
q=q->next;
break;
case 1:
s->data.coef=-q->data.coef;
s->data.expn=q->data.expn;
r->next=s;
r=s;
q=q->next;
break;
}//switch
}//while
while(p)
{
s=(LNode*)malloc(sizeof(LNode));
s->data.coef=p->data.coef;
s->data.expn=p->data.expn;
r->next=s;
r=s;
p=p->next;
}
while(q)
{
s=(LNode*)malloc(sizeof(LNode));
s->data.coef=-q->data.coef;
s->data.expn=q->data.expn;
r->next=s;
r=s;
q=q->next;
}
r->next=NULL;
if(newp->next!=NULL&&newp->next->next!=NULL)//合并同类项
{
for(q=newp->next;q!=NULL;q=q->next)
for(p=q->next,r=q;p!=NULL;)
if(q->data.expn==p->data.expn)
{
q->data.coef=q->data.coef+p->data.coef;
r->next=p->next;
Q=p;p=p->next;
free(Q);
}
else
{
r=r->next;
p=p->next;
}
}
return newp;
}
/*4、销毁已建立的两个多项式*/
void delpolyn(polynomail pa,polynomail pb)
{
polynomail p,q;
p=pa;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
p=pb;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
printf("两个多项式已经销毁\n");
}
void main()
{
polynomail pa=NULL,pb=NULL;
polynomail addp=NULL,subp=NULL,p,q;
int n,m;
char sign='y';
printf("1、创建两个一元多项式\n");
printf("2、两多项式相加得一新多项式\n");
printf("3、两多项式相减得一新多项式\n");
printf("4、销毁已建立的两个多项式\n");
printf("5、退出\n");
printf("\n");
while(sign!='n')
{
printf("请选择:");
scanf("%d",&n);
switch(n)
{
case 1:
if(pa!=NULL)
{
printf("已建立两个一元多项式,请选择其他操作!");
break;
}
printf("请输入第一个多项式:\n");
printf("要输入几项:");
scanf("%d",&m);
while(m==0)
{
printf("m不能为0,请重新输入m:");
scanf("%d",&m);
}
pa=creatpolyn(pa,m);
printpolyn(pa);
printf("请输入第二个多项式:\n");
printf("要输入几项:");
scanf("%d",&m);
pb=creatpolyn(pb,m);
printpolyn(pb);
break;
case 2:
if(pa==NULL)
{
printf("请先创建两个一元多项式!\n");
break;
}
addp=addpolyn(pa,pb);
printpolyn(addp);
break;
case 3:
if(pa==NULL)
{
printf("请先创建两个一元多项式!\n");
break;
}
subp=subpolyn(pa,pb);
printpolyn(subp);
break;
case 4:
if(pa==NULL)
{
printf("请先创建两个一元多项式!\n");
break;
}
delpolyn(pa,pb);
pa=pb=NULL;
break;
case 5:
if(addp!=NULL)
{
p=addp;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
}
if(subp!=NULL)
{
p=subp;
while(p!=NULL)
{
q=p;
p=p->next;
free(q);
}
}
exit(-2);
}//switch
}//while
}