对如下多项式(Polynomial)编写类定义: +++…+ 其中,n为多项式的次数。完成如下功能: (1)可存储任意大的多项式(提示:可用动态数组实现)。 (2)定义构造函数、析构函数、拷贝构造函数。 (3)包含一个static成员存储定义的多项式数量。 (4)定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出,注意化简) (5)定义一个成员函数计算多项式的值。 (6)重载“+”运算符,实现两个多项式相加。 (7)重载“-”运算符,实现两个多项式相减。 (8)重载“*”运算符,实现两个多项式相乘。 (9)重载“=”运算符,实现两个多项式的赋值运算。 (10)在main函数中编写测试代码。 要求:采用多文件实现。考虑:哪些成员函数可以声明为const, 把其中某个运算符重载为友元函数。
时间: 2023-06-20 12:04:25 浏览: 136
C++语言程序设计实验报告
5星 · 资源好评率100%
下面是多项式类的定义,分为头文件 Polynomial.h 和源文件 Polynomial.cpp:
Polynomial.h:
```cpp
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
public:
Polynomial(int degree = 0);
Polynomial(const Polynomial& rhs);
~Polynomial();
void print() const;
double evaluate(double x) const;
Polynomial operator+(const Polynomial& rhs) const;
Polynomial operator-(const Polynomial& rhs) const;
Polynomial operator*(const Polynomial& rhs) const;
Polynomial& operator=(const Polynomial& rhs);
static int numPolynomials;
private:
int degree;
double* coefficients;
};
#endif // POLYNOMIAL_H
```
Polynomial.cpp:
```cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int Polynomial::numPolynomials = 0;
Polynomial::Polynomial(int degree)
{
this->degree = degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++)
{
coefficients[i] = 0;
}
numPolynomials++;
}
Polynomial::Polynomial(const Polynomial& rhs)
{
degree = rhs.degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++)
{
coefficients[i] = rhs.coefficients[i];
}
numPolynomials++;
}
Polynomial::~Polynomial()
{
delete[] coefficients;
numPolynomials--;
}
void Polynomial::print() const
{
int i;
for (i = degree; i > 1; i--)
{
if (coefficients[i] != 0)
{
cout << coefficients[i] << "x^" << i << " + ";
}
}
if (coefficients[i] != 0)
{
cout << coefficients[i] << "x + ";
}
cout << coefficients[0] << endl;
}
double Polynomial::evaluate(double x) const
{
double result = 0;
for (int i = degree; i >= 0; i--)
{
result = result * x + coefficients[i];
}
return result;
}
Polynomial Polynomial::operator+(const Polynomial& rhs) const
{
int maxDegree = max(degree, rhs.degree);
Polynomial result(maxDegree);
for (int i = 0; i <= maxDegree; i++)
{
result.coefficients[i] = coefficients[i] + rhs.coefficients[i];
}
return result;
}
Polynomial Polynomial::operator-(const Polynomial& rhs) const
{
int maxDegree = max(degree, rhs.degree);
Polynomial result(maxDegree);
for (int i = 0; i <= maxDegree; i++)
{
result.coefficients[i] = coefficients[i] - rhs.coefficients[i];
}
return result;
}
Polynomial Polynomial::operator*(const Polynomial& rhs) const
{
int resultDegree = degree + rhs.degree;
Polynomial result(resultDegree);
for (int i = 0; i <= degree; i++)
{
for (int j = 0; j <= rhs.degree; j++)
{
result.coefficients[i + j] += coefficients[i] * rhs.coefficients[j];
}
}
return result;
}
Polynomial& Polynomial::operator=(const Polynomial& rhs)
{
if (this != &rhs)
{
delete[] coefficients;
degree = rhs.degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++)
{
coefficients[i] = rhs.coefficients[i];
}
}
return *this;
}
```
在 main 函数中,我们可以测试多项式类的各种功能,例如:
```cpp
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main()
{
Polynomial p1(3);
p1 = 3.2 * p1 - 2.7 * p1 * p1 + 0.5 * p1 * p1 * p1;
cout << "p1 = ";
p1.print();
cout << "p1(2) = " << p1.evaluate(2) << endl;
Polynomial p2(4);
p2 = 1 - 2 * p1 + p1 * p1;
cout << "p2 = ";
p2.print();
cout << "p2(2) = " << p2.evaluate(2) << endl;
Polynomial p3 = p1 + p2;
cout << "p3 = p1 + p2 = ";
p3.print();
Polynomial p4 = p1 - p2;
cout << "p4 = p1 - p2 = ";
p4.print();
Polynomial p5 = p1 * p2;
cout << "p5 = p1 * p2 = ";
p5.print();
Polynomial p6 = p1;
cout << "p6 = p1 = ";
p6.print();
cout << "p6(2) = " << p6.evaluate(2) << endl;
return 0;
}
```
阅读全文