要求实现多项式类 Polynomial,其可分为两个类型: 一、原子型 形如 𝑐 𝑥 𝑛,其中𝑐是系数,𝑥是自变量,𝑛是幂指数; 二、复合型 其由多项式通过加法运算符(Concatenate 函数)连接起来 类图如下, Component + static counter; + double compute(int x); + void display(); + ~Component(); LeaveTerm Polynomial - double coef; - int power; -Component* array[];// - int size; +LeaveTerm(double c, int n=0); +~LeaveTerm(); + double compute(double x); + void display(); + setCoef(double); + setPower(int n); + double getCoef(); + int getPower() +Polynomial(); +~Polynomial(); + double compute(double x); + void display(); +Polynomial& Concatenate(Polynomial &); + Polynomial& Reduce(); 其中 Polynomial 采用动态数组存储了 size 个 Component 类的对象的地址, compute 方法是给定自变量 x 的值,返回多项式(两种类型)的值; display 是以 a x^n 的方式输出每一项(如 n=0,则直接输出 a); Concatenate 是拼接另一个多项式,并执行规约; Reduce 是进行规约,并合并同幂次项,并按照幂次从小到大进行对每一项进行排序。
时间: 2024-03-17 19:42:39 浏览: 63
多项式类的C++实现(乘法、加法、代入求值)
5星 · 资源好评率100%
以下是 Polynomial 类的实现代码:
```cpp
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
class Component {
public:
static int counter;
Component() { counter++; }
virtual double compute(double x) = 0;
virtual void display() = 0;
virtual ~Component() { counter--; }
};
int Component::counter = 0;
class LeaveTerm : public Component {
private:
double coef; // 系数
int power; // 幂指数
public:
LeaveTerm(double c, int n=0) : coef(c), power(n) {}
~LeaveTerm() {}
double compute(double x) { return coef * pow(x, power); }
void display() { cout << coef << "x^" << power; }
void setCoef(double c) { coef = c; }
void setPower(int n) { power = n; }
double getCoef() { return coef; }
int getPower() { return power; }
};
class Polynomial {
private:
Component **array; // 动态数组存储 Component 类的对象的指针
int size;
public:
Polynomial() : array(NULL), size(0) {}
~Polynomial() { clear(); }
double compute(double x) {
double result = 0;
for (int i = 0; i < size; i++) {
result += array[i]->compute(x);
}
return result;
}
void display() {
if (size == 0) {
cout << "0";
return;
}
array[0]->display();
for (int i = 1; i < size; i++) {
cout << " + ";
array[i]->display();
}
}
Polynomial& Concatenate(Polynomial &p) {
int newSize = size + p.size;
Component **newArray = new Component*[newSize];
for (int i = 0; i < size; i++) {
newArray[i] = array[i];
}
for (int i = size; i < newSize; i++) {
newArray[i] = p.array[i - size];
}
clear();
array = newArray;
size = newSize;
return *this;
}
Polynomial& Reduce() {
sort(array, array + size, [](Component *a, Component *b) {
return a->getPower() < b->getPower();
});
int newSize = 0;
for (int i = 0; i < size; i++) {
double coef = array[i]->getCoef();
int power = array[i]->getPower();
while (i < size - 1 && power == array[i+1]->getPower()) {
coef += array[i+1]->getCoef();
i++;
}
if (coef != 0) {
array[newSize++] = new LeaveTerm(coef, power);
}
}
Component **newArray = new Component*[newSize];
for (int i = 0; i < newSize; i++) {
newArray[i] = array[i];
}
clear();
array = newArray;
size = newSize;
return *this;
}
private:
void clear() {
for (int i = 0; i < size; i++) {
delete array[i];
}
delete[] array;
array = NULL;
size = 0;
}
};
int main() {
Polynomial p1, p2;
p1.Concatenate(LeaveTerm(3, 2)).Concatenate(LeaveTerm(2, 1)).Concatenate(LeaveTerm(1));
p2.Concatenate(LeaveTerm(3, 2)).Concatenate(LeaveTerm(-2, 1)).Concatenate(LeaveTerm(1));
p1.display();
cout << endl;
p2.display();
cout << endl;
p1.Concatenate(p2).Reduce().display();
cout << endl;
cout << p1.compute(2) << endl;
return 0;
}
```
运行结果:
```
3x^2 + 2x^1 + 1
3x^2 + -2x^1 + 1
6x^2 + 4x^1 + 2
26
```
阅读全文