请C++高手看我的代码哪里出现了问题,为何出现了异常,是什么意思
#include<iostream>usingnamespacestd;#defineMax20typedefstruct{floatcoef;intexp;}PolyA...
#include<iostream>
using namespace std;
#define Max 20
typedef struct
{
float coef;
int exp;
}PolyArray[Max];
struct PolyNode
{
float coef;
int exp;
PolyNode *next;
};
class Poly
{
private:
PolyNode *head;
public:
Poly();//构造函数,建立空多项式
~Poly();//析构函数,释放多项式
void Create(PolyArray a,int n);//键盘输入,创建多项式链表
void Disp();//多项式显示
void Sort();//有序表排序
void QD(Poly LA);//多项式求导
};
Poly::Poly()
{ //创建一空多项式
head=new PolyNode;
head->next=NULL;
}
Poly::~Poly()
{//释放多项式所占空间
PolyNode *p;
while(head!=NULL)
{//从头结点开始,依次释放结点
p=head;
head=head->next;
delete p;
}
head=NULL;//头结点指向空
}
void Poly::Disp ()
{
PolyNode *p;
p=head->next;
while(p!=NULL)
{
cout<<p->coef <<"x^"<<p->exp<<'\t' ;
p=p->next ;
}
cout<<endl;
}
void Poly::Create (PolyArray a,int n)
{//由多项式数组创建多项式链表
PolyNode *s,*r;
int i;
r=head;
for(i=0;i<n;i++)
{
s=new PolyNode;
s->coef =a[i].coef;
s->exp =a[i].exp;
s->next =NULL;
r->next =s;
r=s;
}
}
void Poly::Sort()
{
PolyNode *p,*q,*r;
p=head->next ;
if(p!=NULL)
{
r=p->next ;//r指向p的后继结点
p->next =NULL;//构造只有一个结点的有序表
p=r;
while(p!=NULL)
{
r=p->next;
q=head;
while(q->next!=NULL && q->next ->exp < p->exp )
q=q->next ;//在有序表中插入*p的前驱结点*q
p->next =q->next ;//*p插入到*q之后
q->next =p;
p=r;
}
}
}
void Poly::QD(Poly LA)
{
PolyNode *p,*q;
if(head==NULL)
{
cout<<"No Data !"<<endl<<endl;
return;
}
else
{
q=p=head;
while (p!=NULL )
{
if(p->exp==0)
{
if(p==head)
{
head=head->next;
delete p;
q=p=head;
}
else
{
q->next=p->next;
delete p;
p=q->next;
}
}
else
{
p->coef*=p->exp;
p->exp--;
q=p;
p=p->next;
}
}
}
}
int main(void)
{
Poly LA;
PolyArray a={{7.0,5},{3.0,1},{9.0,8},{5.0,16}};
LA.Create (a,4);
cout<<"原多项式A为:";
LA.Disp ();
LA.Sort ();
cout<<"有序多项式A为:";
LA.Disp ();
LA.QD(LA);
cout<<"求导后多项式为:";
LA.Disp ();
return 0;
} 展开
using namespace std;
#define Max 20
typedef struct
{
float coef;
int exp;
}PolyArray[Max];
struct PolyNode
{
float coef;
int exp;
PolyNode *next;
};
class Poly
{
private:
PolyNode *head;
public:
Poly();//构造函数,建立空多项式
~Poly();//析构函数,释放多项式
void Create(PolyArray a,int n);//键盘输入,创建多项式链表
void Disp();//多项式显示
void Sort();//有序表排序
void QD(Poly LA);//多项式求导
};
Poly::Poly()
{ //创建一空多项式
head=new PolyNode;
head->next=NULL;
}
Poly::~Poly()
{//释放多项式所占空间
PolyNode *p;
while(head!=NULL)
{//从头结点开始,依次释放结点
p=head;
head=head->next;
delete p;
}
head=NULL;//头结点指向空
}
void Poly::Disp ()
{
PolyNode *p;
p=head->next;
while(p!=NULL)
{
cout<<p->coef <<"x^"<<p->exp<<'\t' ;
p=p->next ;
}
cout<<endl;
}
void Poly::Create (PolyArray a,int n)
{//由多项式数组创建多项式链表
PolyNode *s,*r;
int i;
r=head;
for(i=0;i<n;i++)
{
s=new PolyNode;
s->coef =a[i].coef;
s->exp =a[i].exp;
s->next =NULL;
r->next =s;
r=s;
}
}
void Poly::Sort()
{
PolyNode *p,*q,*r;
p=head->next ;
if(p!=NULL)
{
r=p->next ;//r指向p的后继结点
p->next =NULL;//构造只有一个结点的有序表
p=r;
while(p!=NULL)
{
r=p->next;
q=head;
while(q->next!=NULL && q->next ->exp < p->exp )
q=q->next ;//在有序表中插入*p的前驱结点*q
p->next =q->next ;//*p插入到*q之后
q->next =p;
p=r;
}
}
}
void Poly::QD(Poly LA)
{
PolyNode *p,*q;
if(head==NULL)
{
cout<<"No Data !"<<endl<<endl;
return;
}
else
{
q=p=head;
while (p!=NULL )
{
if(p->exp==0)
{
if(p==head)
{
head=head->next;
delete p;
q=p=head;
}
else
{
q->next=p->next;
delete p;
p=q->next;
}
}
else
{
p->coef*=p->exp;
p->exp--;
q=p;
p=p->next;
}
}
}
}
int main(void)
{
Poly LA;
PolyArray a={{7.0,5},{3.0,1},{9.0,8},{5.0,16}};
LA.Create (a,4);
cout<<"原多项式A为:";
LA.Disp ();
LA.Sort ();
cout<<"有序多项式A为:";
LA.Disp ();
LA.QD(LA);
cout<<"求导后多项式为:";
LA.Disp ();
return 0;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询