第一题: 对如下多项式(Polynomial)编写类定义:其中,n为多项式的次数。完成如下功能: (1)可存储任意大的多项式(提示:可用动态数组实现)。 (2)定义构造函数、析构函数、拷贝构造函数。 (3)包含一个static成员存储定义的多项式数量。 (4)定义一个成员函数输出多项式。(可参照-x^4-6x^3+5格式输出,注意化简) (5)定义一个成员函数计算多项式的值。 (6)重载“+”运算符,实现两个多项式相加。 (7)重载“-”运算符,实现两个多项式相减。 (8)重载“*”运算符,实现两个多项式相乘。 (9)重载“=”运算符,实现两个多项式的赋值运算。 (10)在main函数中编写测试代码。 要求:采用多文件实现。考虑:哪些成员函数可以声明为const, 把其中某个运算符重载为友元函数。
时间: 2023-06-17 17:03:05 浏览: 215
Polynomial-Js:具有处理多项式方法的JS类
在头文件 Polynomial.h 中编写类定义:
```c++
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial {
private:
int n; // 多项式的次数
double *coefficients; // 多项式系数数组
static int count; // 定义的多项式数量
public:
Polynomial(); // 默认构造函数,创建一个0次多项式
Polynomial(int n, double *coefficients); // 指定次数和系数数组的构造函数
Polynomial(const Polynomial &p); // 拷贝构造函数
~Polynomial(); // 析构函数
void output(); // 输出多项式
double calculate(double x); // 计算多项式的值
Polynomial operator+(const Polynomial &p) const; // 重载+运算符
Polynomial operator-(const Polynomial &p) const; // 重载-运算符
Polynomial operator*(const Polynomial &p) const; // 重载*运算符
Polynomial &operator=(const Polynomial &p); // 重载=运算符
static int getCount(); // 获取定义的多项式数量
};
#endif
```
在源文件 Polynomial.cpp 中实现成员函数:
```c++
#include "Polynomial.h"
#include <iostream>
using namespace std;
int Polynomial::count = 0; // 初始化定义的多项式数量为0
Polynomial::Polynomial() {
n = 0;
coefficients = new double[1];
coefficients[0] = 0.0;
count++;
}
Polynomial::Polynomial(int n, double *coefficients) {
this->n = n;
this->coefficients = new double[n + 1];
for (int i = 0; i <= n; i++) {
this->coefficients[i] = coefficients[i];
}
count++;
}
Polynomial::Polynomial(const Polynomial &p) {
n = p.n;
coefficients = new double[n + 1];
for (int i = 0; i <= n; i++) {
coefficients[i] = p.coefficients[i];
}
count++;
}
Polynomial::~Polynomial() {
delete[] coefficients;
count--;
}
void Polynomial::output() {
if (n == 0) {
cout << coefficients[0] << endl;
return;
}
if (coefficients[0] != 0) {
cout << coefficients[0];
if (n != 0) {
cout << " + ";
}
}
for (int i = 1; i < n; i++) {
if (coefficients[i] != 0) {
cout << coefficients[i] << "x^" << n - i << " + ";
}
}
if (coefficients[n] != 0) {
cout << coefficients[n] << "x^0" << endl;
}
}
double Polynomial::calculate(double x) {
double result = coefficients[0];
for (int i = 1; i <= n; i++) {
result += coefficients[i] * pow(x, i);
}
return result;
}
Polynomial Polynomial::operator+(const Polynomial &p) const {
int maxDegree = max(n, p.n);
double *newCoefficients = new double[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
newCoefficients[i] = (i <= n ? coefficients[i] : 0) + (i <= p.n ? p.coefficients[i] : 0);
}
Polynomial result(maxDegree, newCoefficients);
delete[] newCoefficients;
return result;
}
Polynomial Polynomial::operator-(const Polynomial &p) const {
int maxDegree = max(n, p.n);
double *newCoefficients = new double[maxDegree + 1];
for (int i = 0; i <= maxDegree; i++) {
newCoefficients[i] = (i <= n ? coefficients[i] : 0) - (i <= p.n ? p.coefficients[i] : 0);
}
Polynomial result(maxDegree, newCoefficients);
delete[] newCoefficients;
return result;
}
Polynomial Polynomial::operator*(const Polynomial &p) const {
int newDegree = n + p.n;
double *newCoefficients = new double[newDegree + 1];
for (int i = 0; i <= newDegree; i++) {
newCoefficients[i] = 0;
}
for (int i = 0; i <= n; i++) {
for (int j = 0; j <= p.n; j++) {
newCoefficients[i + j] += coefficients[i] * p.coefficients[j];
}
}
Polynomial result(newDegree, newCoefficients);
delete[] newCoefficients;
return result;
}
Polynomial &Polynomial::operator=(const Polynomial &p) {
if (this != &p) {
n = p.n;
delete[] coefficients;
coefficients = new double[n + 1];
for (int i = 0; i <= n; i++) {
coefficients[i] = p.coefficients[i];
}
}
return *this;
}
int Polynomial::getCount() {
return count;
}
```
在主函数中编写测试代码:
```c++
#include "Polynomial.h"
#include <iostream>
using namespace std;
int main() {
double c1[] = {3, 2, 1};
Polynomial p1(2, c1);
cout << "p1: ";
p1.output(); // 输出 p1
double c2[] = {4, 5, 6};
Polynomial p2(2, c2);
cout << "p2: ";
p2.output(); // 输出 p2
Polynomial p3 = p1 + p2;
cout << "p3 = p1 + p2: ";
p3.output(); // 输出 p3
Polynomial p4 = p1 - p2;
cout << "p4 = p1 - p2: ";
p4.output(); // 输出 p4
Polynomial p5 = p1 * p2;
cout << "p5 = p1 * p2: ";
p5.output(); // 输出 p5
Polynomial p6;
p6 = p1;
cout << "p6 = p1: ";
p6.output(); // 输出 p6
cout << "p1(1) = " << p1.calculate(1) << endl; // 计算 p1 在 x=1 处的值
cout << "已定义多项式数量:" << Polynomial::getCount() << endl; // 输出已定义多项式数量
return 0;
}
```
阅读全文