1

我必须构建一个从类 lista (list) 继承的类多项式 (polinom)。我必须从多项式类中加、减、乘、除 2 个对象。我有这段代码。我不明白为什么我的析构函数不起作用。我还必须重载运算符:+,-,<<,>> 但我不知道该怎么做。

#include <iostream>
#include <conio.h>

using namespace std;

struct nod
{float coef;
 int grad;
 nod* adr_urm;
};

class lista
{ 
public:
   nod *vf,*sf;
   lista();
   nod* adaug(nod *&vf,nod*& sf , int gr,float cf);
   void afis(nod* vz); 
   ~lista();
};

class polinom : public lista
{public:
polinom();
~polinom();

};




void lista::afis(nod* vz)
{nod *c=vz;
cout<<"Elementele polinomului"<<endl;
int i=0;
while (c)

 {if (i) cout<<"+";
 cout<<c->coef<<"X^"<<c->grad;
 c=c->adr_urm;
 i++;
 }
 cout<<endl<<endl;
}

nod* lista::adaug(nod *&vf,nod*& sf ,int gr,float cf)
{ nod *c=new nod;
c->coef=cf;
c->grad=gr;
c->adr_urm=0;
if (vf==0) vf=sf=c;
else {sf->adr_urm=c;
      sf=c;}
return vf;
}

lista::lista()
{vf=0;
}

polinom::polinom()
{vf=0;
}

lista::~lista()
{nod *m=vf, *m1=vf->adr_urm;
 while (m1)
       {delete m;
       m=m1;
       m1=m->adr_urm;
       }
 vf=0;
}

polinom::~polinom()
{nod *man=vf, *man1=vf->adr_urm;
 while (man1)
       {delete man;
       man=man1;
       man1=man->adr_urm;
       }
 vf=NULL;
}




int main()
{
int m,nr,nr1;
float n;
nod* vf=0 ;nod *sf;
nod* varFl=0 ;nod *varFv=0;

polinom l,v;

cout<<"Nr de elemente primul pol nr= ";
cin>>nr;
for (int i=1;i<=nr;i++)
{   cout<<"Elementul "<<i<<endl;
    cout<<"Coeficientul = ";
    cin>>n;
    cout<<"Gradul = ";
    cin>>m;
    l.adaug(vf,sf,m,n);
    varFl=vf;
}
l.afis(varFl);

vf=0;

cout<<"Nr de elemente al doilea pol nr= ";
cin>>nr1;
for (int j=1;j<=nr1;j++)
{cout<<"Elementul "<<j<<endl;
cout<<"Coeficientul = ";
cin>>n;
cout<<"Gradul = ";
cin>>m;
v.adaug(vf,sf,m,n);
varFv=vf;
}
v.afis(varFv);

l.~polinom();
v.~polinom();
_getch();


}
4

4 回答 4

1

您的析构函数可能运行良好,但您调用它们两次,第二次调用可能会导致段错误。只需像这样挂断电话

l.~polinom()

从您的代码中;析构函数被自动调用。

第二件事是,您不需要将lista析构函数复制到polinom; 基类的析构函数也会被自动调用。

于 2009-04-08T22:10:40.117 回答
1

假设“polinom”的意思是“多项式”,那么你有一个基本的设计缺陷——多项式不是链表。您应该使用包含而不是继承来表示可以根据链表实现多项式。

于 2009-04-08T22:16:25.470 回答
0

You ought to have two classes: a monomial class that abstracts a single term with a coefficient and exponent and a polynomial that has a list of monomials as private data member.

The monomial will only allow you to add or subtract terms whose exponents are equal; the result will be a monomial that has that same exponent and the sum or difference of the coefficients. The result from the mul and div methods will also be a monomial that has the product or quotient of coefficients as its coefficient and the sum or different of exponents as its exponent.

The polynomial methods will iterate over its list of monomials to perform the arithmetic operations.

I agree with the comment that discourages you from extending a container like linked list. This is best expressed as a polynomial HAS-A list of monomials rather than IS-A list of monomials. It's a subtle, important difference.

One advantage of HAS-A is that it will allow you to change the data structure you use to store the monomials without affecting clients. Array, list, map - your user doesn't care, as long as you uphold the contract.

I don't know if there's an advantage in treating monomials and polynomials the same, but if there is you'll want an interface defining common methods and a GoF Composite pattern.

于 2009-04-08T23:10:24.793 回答
0

这是一个家庭作业问题吗?

一方面,lista 中的析构函数不是虚拟的,但更大的问题是 polynom 中的析构函数只是 lista 中析构函数的复制粘贴。

我认为你真的需要在这里修改你的设计和代码......

于 2009-04-08T22:06:25.337 回答