动态数组 - 多项式 - C ++ - 完成代码...... [英] Dynamic arrays - polynomial - C++ - completing the code...

查看:61
本文介绍了动态数组 - 多项式 - C ++ - 完成代码......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做什么才能使setTerm()正常工作?

如果您发现任何其他明显问题,请告诉我。


使用此头文件:

=======================

#ifndef Polynomial_h

#define Polynomial_h


class Polynomial

{

public:

多项式();

多项式(const多项式&);

多项式(短nTerms);


~多项式();


const多项式和& operator =(const Polynomial&);


const多项式运算符+(const多项式&)const;

const多项式运算符+(double)const;

friend const多项式运算符+(double,const多项式&);


const多项式运算符 - (const多项式&)const;

const多项式运算符 - (double)const;

friend const多项式运算符 - (double,const Polynomial&);


const多项式运算符*(const多项式&)const;

const多项式运算符*(double)const;

friend const多项式运算符*(double,const Polynomial&);


double setTerm(短期,双倍系数);

double getTerm(短期)const;


双重评估(双x) )const;


朋友ostream&运营商LT;< (ostream&,const Polynomial&);


私人:

短mnTerms;

double * mpCoefficients;

};

#endif


==================== ===

和这个main.cpp文件:

=======================

#include< iostream>

#include< iomanip>

使用命名空间std;


#include" Polynomial.h"

int main()

{

多项式tmp1;

多项式tmp2;

tmp1.setTerm(1,1);

tmp1.setTerm(0,2);


tmp2.setTerm(5,2);

tmp2.setTerm(2,1);

tmp2.setTerm(1,2);

tmp2.setTerm(0,4);


cout<< endl;


cout<< tmp1<< " + << tmp2<< endl;

cout<< tmp1 + tmp2<< endl;

cout<< endl;


cout<< tmp1<< " + << 10<< endl;

cout<< tmp1 + 10<< endl;

cout<< endl;


cout<< 10<< " + << tmp1<< endl;

cout<< 10 + tmp1<< endl;

cout<< endl;


cout<< tmp1<< " - << tmp2<< endl;

cout<< tmp1-tmp2<< endl;

cout<< endl;


cout<< tmp1<< " - << 10<< endl;

cout<< tmp1-10<< endl;

cout<< endl;


cout<< 10<< " - << tmp1<< endl;

cout<< 10-tmp1<< endl;

cout<< endl;


cout<< tmp1<< " X << tmp2<< endl;

cout<< tmp1 * tmp2<< endl;

cout<< endl;


cout<< tmp2<< " X << -2<< endl;

cout<< tmp2 * -2<< endl;

cout<< endl;


cout<< 3<< " X << tmp2<< endl;

cout<< 3 * tmp2<< endl;

cout<< endl;


cout<< tmp2<< "其中x是 << 2<< endl;

cout<< tmp2.evaluate(2)<< endl;

cout<< endl;

返回0;

}

==============

poly.cpp文件:

==============

#include< iostream>

#include< iomanip>

#include< cmath>

using namespace std;


#include" Polynomial .h"

//构造函数

多项式::多项式()

{

mnTerms = 3;

mpCoefficients = new double [mnTerms];

}

//复制构造函数

Polynomial :: Polynomial(const多项式& x)

{

mnTerms = x.mnTerms;

mpCoefficients = new double [mnTerms];

for(int i = 0; i< mnTerms; i ++)

mpCoefficients [i] = x.mpCoefficients [i];

}

//构造函数

多项式::多项式(短nTerms)

{

if(nTerms> = 0)

{

mnTerms = nTerms;

mpCoefficients = new double [mnTerms];

for(int i = 0; i< mnTerms ;我++)<无线电通信/>
{

mpCoefficients [i] = 0;

}

}

else

{

cerr<< 无效多项式。 <<结束;

退出(1);

}

}

//析构函数

多项式::〜多项式()

{

delete [] mpCoefficients;

}

const多项式& Polynomial :: operator =(const Polynomial& rhs)

{

if(this ==& rhs)

return *这个;


if(mnTerms!= rhs.mnTerms)

{

delete [] mpCoefficients;

mnTerms = rhs.mnTerms;

mpCoefficients = new double [mnTerms];

}

for(int i = 0; i< ; mnTerms; i ++)

mpCoefficients [i] = rhs.mpCoefficients [i];


返回* this;

}

const多项式多项式:: operator +(const多项式和rhs)const

{

短最大= mnTerms;

if(rhs.mnTerms>最大)

最大= rhs.mnTerms;

多项式答案(最大);


for(int i = largest-1; i> = 0; i ++)

if((i< mnTerms)&&(i< rhs.mn条款))

answer.mpCoefficients [i] = mpCoefficients [i] + rhs.mpCoefficients [i];

else if(i< mnTerms)

answer.mpCoefficients [i] = mpCoefficients [i];

else

answer.mpCoefficients [i] = rhs.mpCoefficients [i];

返回答案;

}

const多项式多项式::运算符+(双rhs)const

{

多项式答案(* this);

answer.mpCoefficients [0] + = rhs;

返回答案;

}

const多项式算子+(双lhs,const多项式和rhs)

{

多项式答案(rhs);

答案.mpCoefficients [0] + = lhs;

返回答案;

}

const多项式多项式:: operator-(const Polynomial& rhs )const

{

short short = mnTerms;


if(rhs.mnTerms>最大)

最大= rhs.mnTerms;

多项式答案(最大);


for(int i = big-1; i> = 0; i ++)

if((i< mnTerms)&&(i< rhs.mnTerms))

answer.mpCoefficients [i] = mpCoefficients [i] -rhs.mpCoefficients [i];

else if(i< mnTerms)

answer.mpCoefficients [i] = mpCoefficients [i];

else

answer.mpCoefficients [i] = - rhs.mpCoefficients [i];

返回答案;

}

const多项式多项式:: operator-(double rhs)const

{

多项式答案(* this);

answer.mpCoefficients [0] - = rhs;

返回答案;

}

const多项式运算符 - (double lhs,const Polynomial& rhs)

{

多项式答案(lhs);

answer.mpCoefficients [0] - = lhs;

返回答案;

}

const多项式多项式:: operator *(const多项式和rhs)const

{

多项式答案(mnTerms + rhs.mnTerms -1);

for(int i = 0; i< mnTerms; i ++)

for(int j = 0; j< rhs.mnTerms; j ++)

answer.m pCoefficients [i + j] + = mpCoefficients [i] * rhs.mpCoefficients [j];

返回答案;

}

const多项式多项式::运算符*(双rhs)const

{

多项式答案(* this);

for(int i = 0; i< ; mnTerms; i ++)

answer.mpCoefficients [i] * = rhs;

返回答案;

}

const多项式多项式:: operator *(double lhs,const Polynomial& rhs)

{

short short = mnTerms;


if(rhs.mnTerms>最大)

最大= rhs.mnTerms;

多项式答案(最大);


for(int i = largest-1; i> = 0; i ++)

if((i< mnTerms)&&(i< rhs.mnTerms))

answer.mpCoefficients [i] = mpCoefficients [i] * rhs.mpCoefficients [i];

else if(i< mnTerms)

answer.mpCoefficients [i] = mpCoefficients [i];

else

answer.mpCoefficients [i] * = rhs.mpCoefficients [i];

返回答案;

}

d ouble多项式:: setTerm(短期,双系数)

{


}

double多项式:: getTerm(短期)const

{

返回mpCoefficients [term];

}

double多项式:: evaluate(double x )const

{

双回答;

for(int i = 1; i< mnTerms + 1; i ++)

回答+ = mpCoefficients [i] * pow(x,i);

返回答案;

}

ostream& operator< ;< (ostream& stream,const Polynomial& x)

{

for(int i = x.mnTerms; i> 1; i--)

{

stream<< x.mpCoefficients [i]<<英寸×^" << i<< " +" ;;

}

if(x.mnTerms> 0)

{

stream< < x.mpCoefficients [1]<< x +;

}

stream<< x.mpCoefficients [0];

返回流;

}

解决方案



" strotee76" < ST ***** @ hotmail.com>在留言中写道

新闻:a0 ************************** @ posting.google.c om ...

我需要做什么才能使setTerm()正常工作?

如果你发现任何其他明显的问题,请告诉我。


我只是快速查看了编码,发现了一些我会解释的事情

以下。

这个头文件:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
公共:
多项式();
多项式(const多项式&);
多项式(短nTerms);
〜多项式();


你可能会考虑宣布dtor是虚拟的。

const Polynomial& operator =(const Polynomial&);

const多项式运算符+(const多项式&)const;
const多项式运算符+(double)const;
friend const多项式运算符+(double ,const多项式&);

const多项式运算符 - (const多项式&)const;
const多项式运算符 - (双)const;
朋友const多项式运算符 - ( double,const Polynomial&);

const多项式运算符*(const多项式&)const;
const多项式运算符*(double)const;
friend const多项式运算符* (double,const Polynomial&);


const声明可以防止链接。你确定要的吗?

双setTerm(短期,双倍系数);
双getTerm(短期)const;

双重评价(双x) )const;

朋友ostream&运营商LT;< (ostream&,const Polynomial&);

私人:
短mnTerms;
double * mpCoefficients;
};
#endif

[SNIP] ==============
poly.cpp文件:
==============
#include< iostream>
#include< iomanip>
#include< cmath>
使用命名空间std;

#include" Polynomial.h"

//构造函数
多项式::多项式()
{
mnTerms = 3;
mpCoefficients = new double [mnTerms];
}
//复制构造函数
多项式::多项式(const多项式& x)
{
mnTerms = x.mnTerms;
mpCoefficients = new双[mnTerms];


注意,可能已经分配了一些mpCoefficients

指向的内存。如果没有检查和解除分配,这将导致内存泄漏。

for(int i = 0; i< mnTerms; i ++)
mpCoefficients [i] = x.mpCoefficients [i];
}


[SNIP]我没有检查其余的

double Polynomial :: setTerm(短期,双倍系数)
{

}




那么问题是什么?只需检查变量项是否是有效的

索引,并将数据数组的值设置为系数。

[SNIP]


问候

克里斯


strotee76写道:

我需要做什么才能setTerm ()它是否正常工作?

如果你发现任何其他明显的问题,请告诉我。


我没有发现很多真正的错误,但我添加了一个注释,我会这样做

不同。

使用此头文件:
=======================
#ifndef Polynomial_h
#define Polynomial_h

类多项式
{
公共:
多项式();
多项式(const多项式&);
多项式(短nTerms);


您可能应该明确上述构造函数以避免

从short到意外转换为Polynomal。

~Polynomial();

const多项式& operator =(const Polynomial&);

const多项式运算符+(const多项式&)const;
const多项式运算符+(double)const;
friend const多项式运算符+(double ,const多项式&);

const多项式运算符 - (const多项式&)const;
const多项式运算符 - (双)const;
朋友const多项式运算符 - ( double,const Polynomial&);

const多项式运算符*(const多项式&)const;
const多项式运算符*(double)const;
friend const多项式运算符* (double,const Polynomial&);


为什么以上所有操作符都返回const对象?

你可能想要添加+ =, - =和* =。

double setTerm(短期,双倍系数);
double getTerm(短期)const;

双重评价(双x)const;

朋友ostream&运营商LT;< (ostream&,const Polynomial&);


该运营商是否需要成为朋友?事实上,如果

你能以这样的方式写出所有非会员运营商,那就更好了。

不需要成为朋友。这应该很容易。

私人:
短mnTerms;
double * mpCoefficients;
};
#endif

=======================
和这个main.cpp文件:
============ ===========
#include< iostream>
#include< iomanip>
使用命名空间std;

#include" ; Polynomial.h"

int main()
{
多项式tmp1;
多项式tmp2;

tmp1.setTerm(1, 1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout<< ENDL;


这里不需要使用endl。 ''\\ n''就足够了。区别在于,
endl会在发送''\ n''之后刷新输出缓冲区,这在那个地方确实不需要
。对于其他endl'相同。

cout<< tmp1<< " + << tmp2<< endl;
cout<< tmp1 + tmp2<< endl;
cout<< endl;

cout<< tmp1<< " + << 10<< endl;
cout<< tmp1 + 10<< endl;
cout<< endl;

cout<< 10<< " + << tmp1<< endl;
cout<< 10 + tmp1<< endl;
cout<< endl;

cout<< tmp1<< " - << tmp2<< endl;
cout<< tmp1-tmp2<< endl;
cout<< endl;

cout<< tmp1<< " - << 10<< endl;
cout<< tmp1-10<< endl;
cout<< endl;

cout<< 10<< " - << tmp1<< endl;
cout<< 10-tmp1<< endl;
cout<< endl;

cout<< tmp1<< " X << tmp2<< endl;
cout<< tmp1 * tmp2<< endl;
cout<< endl;

cout<< tmp2<< " X << -2<< endl;
cout<< tmp2 * -2<< endl;
cout<< endl;

cout<< 3<< " X << tmp2<< endl;
cout<< 3 * tmp2<< endl;
cout<< endl;

cout<< tmp2<< "其中x是 << 2<< endl;
cout<< tmp2.evaluate(2)<< endl;
cout<< endl;
返回0;
}
==============
poly.cpp文件:
===== =========
#include< iostream>
#include< iomanip>
#include< cmath>
使用命名空间std;

#include" Polynomial.h"

//构造函数
多项式::多项式()
{
mnTerms = 3;
mpCoefficients = new double [mnTerms];
}


使用赋值的初始化intead:


多项式::多项式()

:mnTerms(3),

mpCoefficients(new double [mnTerms])

{

}


//复制构造函数
多项式::多项式(const多项式& x)
{
mnTerms = x.mnTerms;
mpCoefficients = new double [mnTerms];
for(int i = 0; i< mnTerms; i ++)
mpCoefficients [i] = x.mpCoefficients [i];
}


多项式::多项式(const多项式和x)

:mnTerms(x.mnTerms),

mpCoefficients(new double [mnTerms] )

{

// #include< algorithm>以上

std :: copy(x.mpCoefficients,x.mpCoefficients + mnTerms,

mpCoefficients);

}

//构造函数
多项式::多项式(短nTerms)
{
if(nTerms> = 0)
{
mnTerms = nTerms;
mpCoefficients = new double [mnTerms];
for(int i = 0; i< mnTerms; i ++)
{
mpCoefficients [i] = 0;
}
}
其他
{
cerr<< 无效多项式。 << endl;
exit(1);
}
}


exit()应该很少使用,如果有的话。如果你决定在一个更大的程序中使用那个

代码,那么如果你的代码在某些库代码深处的某个地方终止了

程序,那就太烦人了。因此,最好从main返回

程序。


多项式::多项式(短nTerms)

:mnTerms(nTerms)

{

if(nTerms< 0)

throw std :: range_error("条款数量必须是非负的);


mpCoefficients = new double [mnTerms];


std :: fill(mpCoefficients,mpCoefficients + nTerms, 0);

}

//析构函数
多项式::〜多项式()
{
删除[] mpCoefficients;
}
const多项式和多项式:: operator =(const多项式和rhs)
{
if(this ==& rhs)
return * this;

if(mnTerms!= rhs.mnTerms)
{
删除[] mpCoefficients;
mnTerms = rhs.mnTerms;
mpCoefficients = new double [mnTerms ];
}
for(int i = 0; i< mnTerms; i ++)
mpCoefficients [i] = rhs.mpCoefficients [i];


std :: copy(rhs.mpCoefficients,rhs + mpCoefficients + i,mpCoefficients);

返回* this;
}



const多项式多项式:: operator +(const多项式和rhs)const
{short short = mnTerms;
if(rhs.mnTerms> ;最大的)
最大= rhs.mnTerms;


short short = std :: max(mnTerms,rhs.mnTerms);

多项式答案(最大);

(int i = maximum-1; i> = 0; i ++)
if((i< mnTerms)&&(i< rhs.mnTerms))
answer.mpCoefficients [i] = mpCoefficients [i] + rhs.mpCoefficients [i];
if if(i< mnTerms)
answer.mpCoefficients [i] = mpCoefficients [i];
else
answer.mpCoefficients [i] = rhs.mpCoefficients [i];
返回答案;
}
const多项式多项式:: operator +(double rhs)const
{
多项式答案( * this);
answer.mpCoefficients [0] + = rhs;
返回答案;
}
const多项式运算符+(double lhs,const Polynomial& rhs)
{
多项式答案(rhs);
answer.mpCoefficients [0] + = lhs;
返回答案;
}


我会添加一个运算符+ =并使用它来实现另外两个。

类似于:


Polynomal& Polynomal :: operator + =(double rhs)

{

mpCoefficients [0] + = rhs;

}

多项式多项式::运算符+(双rhs)const

{

返回Polynomal(* this)+ = rhs;

} $ / $

多项式算子+(双lhs,const多项式和rhs)

{

返回Polynomal(rhs)+ = lhs ;

}


当然相同 - 和*。

const多项式多项式:: operator-(const Polynomial& rhs)const
{
短最大= mnTerms;

如果(rhs.mnTerms>最大)
最大= rhs.mnTerms;
多项式答案( (int i = max-1; i> = 0; i ++)
if((i< mnTerms)&&(i< rhs.mnTerms))
answer.mpCoefficients [i] = mpCoefficients [i] -rhs.mpCoefficients [i];
if if(i< mnTerms)
answer.mpCoefficients [i] = mpCoefficients [i];

answer.mpCoefficients [i] = - rhs.mpCoefficients [i];
返回答案;
}
利弊t多项式多项式:: operator-(double rhs)const
{多项式答案(* this);
answer.mpCoefficients [0] - = rhs;
返回答案;
}
const多项式算子 - (double lhs,const Polynomial& rhs)
{
多项式答案(lhs);
answer.mpCoefficients [0] - = lhs ;
返回答案;
}
const多项式多项式:: operator *(const多项式和rhs)const
{
多项式答案(mnTerms + rhs.mnTerms- 1);
for(int i = 0; i< mnTerms; i ++)
for(int j = 0; j< rhs.mnTerms; j ++)
answer.mpCoefficients [i + j ] + = mpCoefficients [i] * rhs.mpCoefficients [j];
返回答案;
}
const多项式多项式:: operator *(double rhs)const
{
多项式答案(* this);
for(int i = 0; i< mnTerms; i ++)
answer.mpCoefficients [i] * = rhs;
返回答案;
}
const多项式多项式:: operator *(double lhs,const Polynomial
&rhs)
{
短最大= mnTerms;
if(rhs.mnTerms>最大)
最大= rhs.mnTerms;
多项式答案(最大);

for(int i = largest-1; i> = 0 ; i ++)
if((i< mnTerms)&&(i< rhs.mnTerms))
answer.mpCoefficients [i] = mpCoefficients [i] * rhs.mpCoefficients [i];
if if(i< mnTerms)
answer.mpCoefficients [i] = mpCoefficients [i];
else
answer.mpCoefficients [i] * = rhs.mpCoefficients [i];
返回答案;
}
double Polynomial :: setTerm(短期,双倍系数)
{


if(term< 0 ||术语> mnTerms)

throw std :: range_error();


返回mpCoefficients [term] =系数;

}



不知道为什么这个函数会有双返回类型而不是

void。

double Polynomial :: getTerm(short术语
{
返回mpCoefficients [term];
}
double多项式:: evaluate(double x)const
{
双回答;
for(int i = 1; i< mnTerms + 1; i ++)
answer + = mpCoefficients [i] * pow(x,i);
返回答案;
}
ostream& operator<< (ostream& stream,const Polynomial& x)
{
for(int i = x.mnTerms; i> 1; i--)
{
stream< < x.mpCoefficients [i]<<英寸×^" << i<< " +" ;;
}
if(x.mnTerms> 0)
{
stream<< x.mpCoefficients [1]<< x +;
}
stream<< x.mpCoefficients [0];


我想这应该看起来不同。如果有一个mpCoefficients [0],

mnTerms必须已经为1.对于mpCoefficients [1]存在,mnTerms必须

为2.

返回流;
}




Chris Theis写道:

< blockquote class =post_quotes> // copy constructor
Polynomial :: Polynomial(const Polynomial& x)
{mnTerms = x.mnTerms;
mpCoefficients = new double [ mnTerms];



注意,可能已经分配了一些
mpCoefficients指向的内存。




Huh ?你有没有想念这是一个构造函数?


What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.

With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);

~Polynomial ();

const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);

double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);

private:
short mnTerms;
double *mpCoefficients;
};
#endif

=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;

#include "Polynomial.h"

int main ()
{
Polynomial tmp1;
Polynomial tmp2;
tmp1.setTerm(1,1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout << endl;

cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;

cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;

cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;

cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;

cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;

cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;

cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;

cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;

cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;

cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"
//constructor
Polynomial::Polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial::Polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients[i]=x.mpCoefficients[i];
}
//constructor
Polynomial::Polynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients[i]=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial::operator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;

if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients[i] = rhs.mpCoefficients[i];

return *this;
}
const Polynomial Polynomial::operator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]+rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}
const Polynomial Polynomial::operator- (const Polynomial &rhs) const
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]-rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=-rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial::operator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients[i]*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::operator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients[i]*=rhs;
return answer;
}
const Polynomial Polynomial::operator* (double lhs,const Polynomial &rhs)
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]*rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]*=rhs.mpCoefficients[i];
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{

}
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients[i]*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients[i] << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
return stream;
}

解决方案


"strotee76" <st*****@hotmail.com> wrote in message
news:a0**************************@posting.google.c om...

What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.
I just looked through the coded quickly and found some things I''ll explain
below.

With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);

~Polynomial ();
You might consider declaring the dtor virtual.

const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);
The const declarations prevents chaining. Are you sure you want that?

double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);

private:
short mnTerms;
double *mpCoefficients;
};
#endif
[SNIP] ==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"
//constructor
Polynomial::Polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
// copy constructor
Polynomial::Polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
Attention, there might already be some memory allocated which mpCoefficients
points to. Without checking and deallocation this will result in a memory
leak.
for (int i=0;i<mnTerms;i++)
mpCoefficients[i]=x.mpCoefficients[i];
}
[SNIP] I did not check the rest
double Polynomial::setTerm (short term,double coefficient)
{

}



So what is the problem? Just check whether the variable term is a valid
index and set the value of your data array to coefficient.
[SNIP]

Regards
Chris


strotee76 wrote:

What do I need to do to setTerm() for it to work properly?

If you notice any other glaring issues, then please let me know.
I didn''t find many real errors, but I added a note where I would do it
different.

With this header file:
=======================
#ifndef Polynomial_h
#define Polynomial_h

class Polynomial
{
public:
Polynomial ();
Polynomial (const Polynomial &);
Polynomial (short nTerms);
You probably should make the above constructor explicit to avoid
accidental conversion from short to Polynomal.
~Polynomial ();

const Polynomial & operator= (const Polynomial &);

const Polynomial operator+ (const Polynomial &) const;
const Polynomial operator+ (double) const;
friend const Polynomial operator+ (double, const Polynomial &);

const Polynomial operator- (const Polynomial &) const;
const Polynomial operator- (double) const;
friend const Polynomial operator- (double, const Polynomial &);

const Polynomial operator* (const Polynomial &) const;
const Polynomial operator* (double) const;
friend const Polynomial operator* (double, const Polynomial &);
Why do all the above operators return const objects?
And you might want to add +=, -= and *=.
double setTerm (short term, double coefficient);
double getTerm (short term) const;

double evaluate (double x) const;

friend ostream & operator<< (ostream &, const Polynomial &);
Does that operator need to be a friend? In fact, it would be better if
you could write all the nonmember operators in such a way that they
don''t need to be friends. That should be easy.
private:
short mnTerms;
double *mpCoefficients;
};
#endif

=======================
and this main.cpp file:
=======================
#include <iostream>
#include <iomanip>
using namespace std;

#include "Polynomial.h"

int main ()
{
Polynomial tmp1;
Polynomial tmp2;
tmp1.setTerm(1,1);
tmp1.setTerm(0,2);

tmp2.setTerm(5,2);
tmp2.setTerm(2,1);
tmp2.setTerm(1,2);
tmp2.setTerm(0,4);

cout << endl;
No need to use endl here. ''\n'' would suffice. The difference is that
endl will flush the output buffer after sending a ''\n'', which isn''t
really needed at that place. Same for the other endl''s.

cout << tmp1 << " + " << tmp2 << endl;
cout << tmp1+tmp2 << endl;
cout << endl;

cout << tmp1 << " + " << 10 << endl;
cout << tmp1+10 << endl;
cout << endl;

cout << 10 << " + " << tmp1 << endl;
cout << 10+tmp1 << endl;
cout << endl;

cout << tmp1 << " - " << tmp2 << endl;
cout << tmp1-tmp2 << endl;
cout << endl;

cout << tmp1 << " - " << 10 << endl;
cout << tmp1-10 << endl;
cout << endl;

cout << 10 << " - " << tmp1 << endl;
cout << 10-tmp1 << endl;
cout << endl;

cout << tmp1 << " X " << tmp2 << endl;
cout << tmp1*tmp2 << endl;
cout << endl;

cout << tmp2 << " X " << -2 << endl;
cout << tmp2*-2 << endl;
cout << endl;

cout << 3 << " X " << tmp2 << endl;
cout << 3*tmp2 << endl;
cout << endl;

cout << tmp2 << " where x is " << 2 << endl;
cout << tmp2.evaluate(2) << endl;
cout << endl;
return 0;
}
==============
poly.cpp file:
==============
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

#include "Polynomial.h"
//constructor
Polynomial::Polynomial()
{
mnTerms = 3;
mpCoefficients = new double[mnTerms];
}
Use initialization intead of assignment:

Polynomial::Polynomial()
: mnTerms(3),
mpCoefficients(new double[mnTerms])
{
}

// copy constructor
Polynomial::Polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];
for (int i=0;i<mnTerms;i++)
mpCoefficients[i]=x.mpCoefficients[i];
}
Polynomial::Polynomial(const Polynomial &x)
: mnTerms(x.mnTerms),
mpCoefficients(new double [mnTerms])
{
// #include <algorithm> above
std::copy(x.mpCoefficients, x.mpCoefficients + mnTerms,
mpCoefficients);
}
//constructor
Polynomial::Polynomial(short nTerms)
{
if (nTerms>=0)
{
mnTerms=nTerms;
mpCoefficients=new double[mnTerms];
for(int i=0;i<mnTerms;i++)
{
mpCoefficients[i]=0;
}
}
else
{
cerr << "Invalid polynomial." << endl;
exit(1);
}
}
exit() should be used very rarely, if at all. If you decide to use that
code in a bigger program, it''s annoying if your code terminates the
program somewhere deep in some library code. So it''s better to quit the
program by just returning from main.

Polynomial::Polynomial(short nTerms)
: mnTerms(nTerms)
{
if (nTerms<0)
throw std::range_error("number of terms must be non-negative");

mpCoefficients=new double[mnTerms];

std::fill(mpCoefficients, mpCoefficients + nTerms, 0);
}
//destructor
Polynomial::~Polynomial()
{
delete [] mpCoefficients;
}
const Polynomial &Polynomial::operator= (const Polynomial &rhs)
{
if (this==&rhs)
return *this;

if (mnTerms!=rhs.mnTerms)
{
delete [] mpCoefficients;
mnTerms=rhs.mnTerms;
mpCoefficients=new double [mnTerms];
}
for (int i=0;i<mnTerms;i++)
mpCoefficients[i] = rhs.mpCoefficients[i];
std::copy(rhs.mpCoefficients, rhs+mpCoefficients + i, mpCoefficients);

return *this;
}

const Polynomial Polynomial::operator+ (const Polynomial &rhs) const
{
short biggest=mnTerms;
if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
short biggest = std::max(mnTerms, rhs.mnTerms);
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]+rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator+ (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]+=rhs;
return answer;
}
const Polynomial operator+ (double lhs,const Polynomial &rhs)
{
Polynomial answer(rhs);
answer.mpCoefficients[0]+=lhs;
return answer;
}
I would add an operator+= and implement the two others by using it.
Something like:

Polynomal& Polynomal::operator+=(double rhs)
{
mpCoefficients[0]+=rhs;
}

Polynomial Polynomial::operator+ (double rhs) const
{
return Polynomal(*this)+=rhs;
}

Polynomial operator+ (double lhs,const Polynomial &rhs)
{
return Polynomal(rhs)+=lhs;
}

Same of course for - and *.
const Polynomial Polynomial::operator- (const Polynomial &rhs) const
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]-rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]=-rhs.mpCoefficients[i];
return answer;
}
const Polynomial Polynomial::operator- (double rhs) const
{
Polynomial answer (*this);
answer.mpCoefficients[0]-=rhs;
return answer;
}
const Polynomial operator- (double lhs, const Polynomial &rhs)
{
Polynomial answer(lhs);
answer.mpCoefficients[0]-=lhs;
return answer;
}
const Polynomial Polynomial::operator* (const Polynomial &rhs) const
{
Polynomial answer(mnTerms+rhs.mnTerms-1);
for (int i=0;i<mnTerms;i++)
for (int j=0;j<rhs.mnTerms;j++)
answer.mpCoefficients[i+j]+=mpCoefficients[i]*rhs.mpCoefficients[j];
return answer;
}
const Polynomial Polynomial::operator* (double rhs) const
{
Polynomial answer (*this);
for (int i=0;i<mnTerms;i++)
answer.mpCoefficients[i]*=rhs;
return answer;
}
const Polynomial Polynomial::operator* (double lhs,const Polynomial
&rhs)
{
short biggest=mnTerms;

if (rhs.mnTerms>biggest)
biggest=rhs.mnTerms;
Polynomial answer(biggest);

for (int i=biggest-1;i>=0;i++)
if ((i<mnTerms)&&(i<rhs.mnTerms))
answer.mpCoefficients[i]=mpCoefficients[i]*rhs.mpCoefficients[i];
else if (i<mnTerms)
answer.mpCoefficients[i]=mpCoefficients[i];
else
answer.mpCoefficients[i]*=rhs.mpCoefficients[i];
return answer;
}
double Polynomial::setTerm (short term,double coefficient)
{
if (term < 0 || term > mnTerms)
throw std::range_error();

return mpCoefficients[term] = coefficient;
}

Not sure why this function would have double return type instead of
void.
double Polynomial::getTerm (short term) const
{
return mpCoefficients[term];
}
double Polynomial::evaluate (double x) const
{
double answer;
for (int i=1;i<mnTerms+1;i++)
answer += mpCoefficients[i]*pow(x,i);
return answer;
}
ostream &operator<< (ostream &stream,const Polynomial &x)
{
for(int i=x.mnTerms;i>1;i--)
{
stream << x.mpCoefficients[i] << "x^" << i << " + ";
}
if (x.mnTerms > 0)
{
stream << x.mpCoefficients[1] << "x + ";
}
stream << x.mpCoefficients[0];
I suppose that should look different. If there is a mpCoefficients[0],
mnTerms must already be 1. For mpCoefficients[1] to exist, mnTerms must
be 2.
return stream;
}




Chris Theis wrote:

// copy constructor
Polynomial::Polynomial(const Polynomial &x)
{
mnTerms=x.mnTerms;
mpCoefficients=new double [mnTerms];



Attention, there might already be some memory allocated which
mpCoefficients points to.



Huh? Did you miss the fact that this is a constructor?


这篇关于动态数组 - 多项式 - C ++ - 完成代码......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆