//头文件
#include <iostream>
#include <stdlib.h>
using namespace std;
typedef struct Polynode{
float coef;
int expn;
struct Polynode *next;
}Polynomial;
void PolyCreate(Polynomial *&head,int n);
void ShowPolynomial(Polynomial *L);
void Add(Polynomial *a,Polynomial *b,Polynomial *c);
void PolyCreate(Polynomial *&head,int n)//建立一元多项式的链表
{
Polynomial *p, *s;
float c;
int e,i;
p=head;
for(i=1;i<=n;i++)
{
cout<<"输入第"<<i<<"项的系数:";
cin>>c;
cout<<"输入第"<<i<<"项的指数:";
cin>>e;
s = new Polynomial;
s->coef = c;
s->expn = e;
p->next = s;
p = p->next;
}
p->next = NULL;
}
void Add(Polynomial *a,Polynomial *b,Polynomial *c)//相加
{
Polynomial *p,*q,*s,*l;
float co;
p = a->next;
q = b->next;
l=c;
while( p&&q )
{
s = new Polynomial;
if(p->expn>q->expn)
{
s->coef = p->coef;
s->expn = p->expn;
l->next = s;
p = p->next;
l = l->next;
}
else if(p->expn<q->expn)
{
s->coef = q->coef;
s->expn = q->expn;
l->next = s;
q = q->next;
l = l->next;
}
else
{
co=p->coef+q->coef;
s->coef=co;
if(co!=0)
{
s->expn=p->expn;
l->next=s;
l=l->next;
p=p->next;
q=q->next;
}
else
p=p->next;
q=q->next;
}
}
if(p==NULL)
{
while(q)
{
s = new Polynomial;
s->coef = q->coef;
s->expn = q->expn;
l->next = s;
q = q->next;
l = l->next;
}
}
else if(q==NULL)
{
while(p)
{
s = new Polynomial;
s->coef = p->coef;
s->expn = p->expn;
l->next = s;
p = p->next;
l = l->next;
}
}
l->next = NULL;
}
void ShowPolynomial(Polynomial *head)
{
Polynomial *p;
p=head;
while(p)
{
if(p->next=NULL)
cout<<p->coef<<"x^"<<p->expn;
else
cout<<p->coef<<"x^"<<p->expn<<"+";
p=p->next;
}
}
//主函数
#include <iostream>
#include "polynominal.h"
using namespace std;
int main()
{
int n,m;
Polynomial *la,*lb,*lc;
lc=NULL;
cout<<"请输入多项式la的项数:";
cin>>n;
PolyCreate(la,n);
cout<<"请输出一元多项式la:";
ShowPolynomial(la);
cout<<"请输入多项式lb的项数:";
cin>>m;
PolyCreate(lb,m);
cout<<"请输出一元多项式lb:";
ShowPolynomial(lb);
Add(la,lb,lc);
cout<<"请输出相加后的一元多项式lc";
ShowPolynomial(lc);
}
问题:输入到一半程序就自动结束。