对如下多项式(Polynomial)编写类定义: + + +…+ 其中,n为多项式的次数。完成如下功能: (1) 可存储任意大的多项式(提示:可用动态数组实现)。 (2) 定义构造函数、析构函数、拷贝构造函数。 (3) 包含一个static成员存储定义的多项式数量。 (4) 定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出,注意化简) (5) 定义一个成员函数计算多项式的值。 (6) 重载“+”运算符,实现两个多项式相加。 (7) 重载“-”运算符,实现两个多项式相减。 (8) 重载“*”运算符,实现两个多项式相乘。 (9) 重载“=”运算符,实现两个多项式的赋值运算。 (10) 在main函数中编写测试代码。 要求:采用多文件实现。考虑:哪些成员函数可以声明为const, 把其中某个运算符重载为友元函数。
时间: 2023-06-17 07:02:23 浏览: 111
首先,我们可以在一个头文件中定义多项式类Polynomial:
```c++
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial {
private:
int degree; // 多项式次数
double* coefficients; // 动态数组,存储每一项的系数
public:
static int count; // 多项式数量
Polynomial(int degree = 0); // 构造函数
Polynomial(const Polynomial& p); // 拷贝构造函数
~Polynomial(); // 析构函数
void print() const; // 输出多项式
double evaluate(double x) const; // 计算多项式的值
Polynomial operator+(const Polynomial& p) const; // 重载"+"运算符,实现两个多项式相加
Polynomial operator-(const Polynomial& p) const; // 重载"-"运算符,实现两个多项式相减
Polynomial operator*(const Polynomial& p) const; // 重载"*"运算符,实现两个多项式相乘
Polynomial& operator=(const Polynomial& p); // 重载"="运算符,实现两个多项式的赋值运算
friend Polynomial operator*(double a, const Polynomial& p); // 友元函数,实现一个数与多项式相乘
};
#endif
```
其中,count是静态成员,用于记录定义的多项式数量。构造函数、拷贝构造函数、析构函数、print()和evaluate()函数都可以声明为const。
接下来,我们可以在实现文件中定义多项式类的成员函数和友元函数:
```c++
#include "Polynomial.h"
#include <iostream>
#include <cmath>
using namespace std;
int Polynomial::count = 0;
Polynomial::Polynomial(int degree)
{
this->degree = degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
coefficients[i] = 0.0;
}
count++;
}
Polynomial::Polynomial(const Polynomial& p)
{
degree = p.degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
coefficients[i] = p.coefficients[i];
}
count++;
}
Polynomial::~Polynomial()
{
delete[] coefficients;
count--;
}
void Polynomial::print() const
{
bool first = true;
for (int i = degree; i >= 0; i--) {
double coef = coefficients[i];
if (coef != 0.0) {
if (coef > 0.0 && !first) {
cout << "+";
}
if (coef == -1.0) {
cout << "-";
} else if (coef != 1.0) {
cout << coef;
}
if (i > 0) {
cout << "x";
if (i > 1) {
cout << "^" << i;
}
}
first = false;
}
}
if (first) {
cout << "0";
}
cout << endl;
}
double Polynomial::evaluate(double x) const
{
double result = 0.0;
for (int i = degree; i >= 0; i--) {
result = result * x + coefficients[i];
}
return result;
}
Polynomial Polynomial::operator+(const Polynomial& p) const
{
int maxDegree = max(degree, p.degree);
Polynomial result(maxDegree);
for (int i = 0; i <= maxDegree; i++) {
result.coefficients[i] = coefficients[i] + p.coefficients[i];
}
return result;
}
Polynomial Polynomial::operator-(const Polynomial& p) const
{
int maxDegree = max(degree, p.degree);
Polynomial result(maxDegree);
for (int i = 0; i <= maxDegree; i++) {
result.coefficients[i] = coefficients[i] - p.coefficients[i];
}
return result;
}
Polynomial Polynomial::operator*(const Polynomial& p) const
{
int maxDegree = degree + p.degree;
Polynomial result(maxDegree);
for (int i = 0; i <= degree; i++) {
for (int j = 0; j <= p.degree; j++) {
result.coefficients[i + j] += coefficients[i] * p.coefficients[j];
}
}
return result;
}
Polynomial& Polynomial::operator=(const Polynomial& p)
{
if (this != &p) {
delete[] coefficients;
degree = p.degree;
coefficients = new double[degree + 1];
for (int i = 0; i <= degree; i++) {
coefficients[i] = p.coefficients[i];
}
}
return *this;
}
Polynomial operator*(double a, const Polynomial& p)
{
Polynomial result(p.degree);
for (int i = 0; i <= p.degree; i++) {
result.coefficients[i] = a * p.coefficients[i];
}
return result;
}
```
在测试代码中,我们可以定义多个多项式并进行相加、相减、相乘、输出和计算值等操作:
```c++
#include "Polynomial.h"
#include <iostream>
using namespace std;
int main()
{
Polynomial p1(4);
p1.print(); // 输出:0
p1.coefficients[0] = 1;
p1.coefficients[4] = 1;
p1.coefficients[3] = -6;
p1.coefficients[1] = 5;
p1.print(); // 输出:x^4-6x^3+5x
Polynomial p2(2);
p2.coefficients[0] = 1;
p2.coefficients[2] = 1;
p2.print(); // 输出:x^2+1
Polynomial p3 = p1 + p2;
p3.print(); // 输出:x^4-6x^3+x^2+5x+1
Polynomial p4 = p1 - p2;
p4.print(); // 输出:x^4-6x^3-x^2+5x-1
Polynomial p5 = p1 * p2;
p5.print(); // 输出:x^6-x^4-6x^3+5x^3+x^2+5x
double x = 2.0;
cout << "p1(" << x << ") = " << p1.evaluate(x) << endl; // 输出:p1(2) = 10
cout << "p2(" << x << ") = " << p2.evaluate(x) << endl; // 输出:p2(2) = 5
cout << "p3(" << x << ") = " << p3.evaluate(x) << endl; // 输出:p3(2) = 47
cout << "p4(" << x << ") = " << p4.evaluate(x) << endl; // 输出:p4(2) = -19
cout << "p5(" << x << ") = " << p5.evaluate(x) << endl; // 输出:p5(2) = 84
Polynomial p6 = p1;
p6.print(); // 输出:x^4-6x^3+5x
p6 = p2;
p6.print(); // 输出:x^2+1
return 0;
}
```
阅读全文