分配: 在这个作业中,您将开发一个C ++类来描述和操作 多项式。
在标头中完成类声明,然后实现(在匹配的.cpp文件中) 在头文件中声明的函数并测试它们以确保它们像宣传的那样工作。
提供的测试程序非常基础。您需要修改它并添加一些(2个或更多) 测试以确保新类的所有方面都得到执行。
TestThePoly.cpp的示例结果:
(1) Testing cout << A: empty
(2) Testing cin >> A':
Enter the polynomial (integer degree then double coefficients):
3 -1 2.09 -5.3 -0.98
(3) Second look at A: -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(4) Testing Polynomial B(A): -1x^(3) +2.09x^(2) -5.3x^(1) -0.98
(5) Testing Polynomial C(2, clist)': +1x^(2) +4.5x^(1) +8
(6) Testing D = C: +1x^(2) +4.5x^(1) +8
(7) Testing A == B : TRUE
(8) Testing A == D : FALSE
我曾多次尝试过这项任务,但还没有弄清楚。我在网上看了一些我可以帮助我但没有找到任何东西的东西。大多数其他多项式类问题似乎涉及乘法等,并且至少比这更复杂。或者他们在某些方面比我更简单/做得与众不同。
这是头文件
// Polynomial.h
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using std::ostream;
using std::istream;
class Polynomial
{
public:
Polynomial();
Polynomial( int dgr, const double* clist );
Polynomial( const Polynomial& );
~Polynomial();
int setDegree(int dgr);
int getDegree() const;
const Polynomial& operator=( const Polynomial& clist);
bool operator==( const Polynomial& clist) const;
void printPolynomial( int dgr, const double* clist );
double operator[]( int index ) const;
double& operator[]( int index );
private:
int degree;
double *coefficients; // list of coefficients
};
ostream& operator<<( ostream& , const Polynomial& );
istream& operator>>( istream& , Polynomial& );
#endif
这是源文件
// Polynomial.cpp
#include <iostream>
using std::istream;
using std::ostream;
using std::cerr;
using std::endl;
#include <cstdlib>
using std::exit;
#include "Polynomial.h"
Polynomial::Polynomial()
{
degree = 0;
coefficients = 0;
}
Polynomial::Polynomial(int dgr, const double* clist)
{
getDegree();
}
int Polynomial::getDegree() const
{
return degree;
}
Polynomial:: ~Polynomial()
{
setDegree(0);
}
int Polynomial::setDegree(int dgr)
{
if( dgr <0 ) {
cerr << "Error: attempted to set a negative degree" << endl;
exit(1);
} else {
if( coefficients!=0 ) {
delete [] coefficients;
coefficients = 0;
degree = 0;
}
if( dgr !=0 ) {
coefficients = new double [dgr];
if( coefficients==0 ) {
degree =0;
cerr << "Warning: unable to allocate enough space for list" << endl;
exit(2); // this surely is reasonable.
} else {
degree=dgr;
}
}
}
return dgr;
}
//copy constructor
Polynomial::Polynomial(const Polynomial& clist) : degree(0), coefficients(0)
{
if( clist.getDegree()<=0 ) {
setDegree(0);
} else {
setDegree(clist.getDegree());
for (int i=0; i< degree ; i++) {
coefficients[i]=clist.coefficients[i];
}
}
}
const Polynomial& Polynomial :: operator=(const Polynomial& clist)
{
if ( &clist==this )
{
cerr << "Warning: attempt to copy Polynomial onto self" << endl;
}
else {
if( clist.getDegree()<=0 ) {
setDegree(0);
} else {
setDegree(clist.getDegree());
for (int i=0; i< degree ; i++) {
coefficients[i] = clist.coefficients[i];
}
}
}
return *this;
}
bool Polynomial::operator==(const Polynomial& clist) const
{
bool result=true;
if( degree!=clist.getDegree() ) {
result=false;
} else {
for(int i=0; i< degree && result == true; i++) {
result = coefficients[i]==clist.coefficients[i];
}
}
return result;
}
double Polynomial::operator[]( int index ) const
{
if( index<0 || index>= degree ) {
cerr << "Attempt to access element outside index bounds"
<< endl;
exit(3); // Maybe not reasonable.
} else {
return coefficients[index];
}
}
double& Polynomial::operator[]( int index )
{
if( index < 0 || index>= degree ) {
cerr << "Attempt to access element outside index bounds"
<< endl;
exit(4);
} else {
return coefficients[index];
}
}
ostream& operator<<( ostream& left, const Polynomial& right)
{
if( right.getDegree() > 0 ) {
left << endl;
for (int i=0; i< right.getDegree(); i++ ) {
for (int j = 0; j >= 0 ; j -- ){
left << right[i] << "X^(" << j << ") " ;
if (j > 0)
{
left << " + ";
}
}
}
}
else {
cerr << "Warning: Attempt to display empty List" << endl;
}
return left;
}
istream& operator>>( istream& left, Polynomial& right)
{
int tmp;
left >> tmp;
if( tmp<=0 ) {
cerr << "First value expected to be list size, > 0." << endl;
} else {
if( tmp!=right.getDegree() ) {
right.setDegree(tmp);
}
for (int i=0; i<right.getDegree(); i++) {
left >> right[i];
}
}
return left;
}
这是测试/主文件
// Prog11.cpp
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
#include "Polynomial.h"
int main()
{
Polynomial A;
cout << "(1) Testing `cout << A': " << A << endl;
cout << "(2) Testing `cin >> A':\n";
cout << "Enter the polynomial (integer order then double coefficients):\n\t ";
cin >> A;
cout << endl;
cout << "(3) Second look at A: " << A << endl;
Polynomial B(A);
cout << "(4) Testing `Polynomial B(A)': " << B << endl;
double clist[]={8, 4.5, 1};
Polynomial C(2, clist);
cout << "(5) Testing `Polynomial C(2, clist)': " << C << endl;
Polynomial D=C;
cout << "(6) Testing D = C): " << D << endl;
cout << "(7) Testing A == B : " << (A==B ? "TRUE" : "FALSE") << endl;
cout << "(8) Testing A == D : " << (A==D ? "TRUE" : "FALSE")<< endl;
return 0;
}