设计一元稀疏多项式计算器C++实现
时间: 2023-10-16 17:11:07 浏览: 130
以下是一个简单的 C++ 实现,可以计算一元稀疏多项式的加减乘除以及求导数和积分。
```cpp
#include <iostream>
#include <vector>
using namespace std;
struct Term {
int coef; // 系数
int expo; // 指数
};
class Polynomial {
private:
vector<Term> terms; // 存储多项式的向量
public:
Polynomial() {} // 默认构造函数
Polynomial(vector<Term> terms) { // 带参构造函数
this->terms = terms;
}
void input() { // 输入多项式
int n;
cout << "请输入多项式的项数: ";
cin >> n;
for (int i = 0; i < n; i++) {
Term t;
cout << "请输入第" << i + 1 << "项的系数和指数: ";
cin >> t.coef >> t.expo;
terms.push_back(t);
}
sort(terms.begin(), terms.end(), [](Term a, Term b) { // 按指数排序
return a.expo > b.expo;
});
}
void output() { // 输出多项式
for (int i = 0; i < terms.size(); i++) {
if (terms[i].coef > 0 && i > 0) {
cout << "+";
}
cout << terms[i].coef << "x^" << terms[i].expo;
}
cout << endl;
}
Polynomial operator+(const Polynomial& b) const { // 加法
vector<Term> result;
int i = 0, j = 0;
while (i < terms.size() && j < b.terms.size()) {
if (terms[i].expo > b.terms[j].expo) {
result.push_back(terms[i]);
i++;
} else if (terms[i].expo < b.terms[j].expo) {
result.push_back(b.terms[j]);
j++;
} else {
int c = terms[i].coef + b.terms[j].coef;
if (c != 0) {
Term t = {c, terms[i].expo};
result.push_back(t);
}
i++;
j++;
}
}
while (i < terms.size()) {
result.push_back(terms[i]);
i++;
}
while (j < b.terms.size()) {
result.push_back(b.terms[j]);
j++;
}
return Polynomial(result);
}
Polynomial operator-(const Polynomial& b) const { // 减法
vector<Term> result;
int i = 0, j = 0;
while (i < terms.size() && j < b.terms.size()) {
if (terms[i].expo > b.terms[j].expo) {
result.push_back(terms[i]);
i++;
} else if (terms[i].expo < b.terms[j].expo) {
Term t = {-b.terms[j].coef, b.terms[j].expo};
result.push_back(t);
j++;
} else {
int c = terms[i].coef - b.terms[j].coef;
if (c != 0) {
Term t = {c, terms[i].expo};
result.push_back(t);
}
i++;
j++;
}
}
while (i < terms.size()) {
result.push_back(terms[i]);
i++;
}
while (j < b.terms.size()) {
Term t = {-b.terms[j].coef, b.terms[j].expo};
result.push_back(t);
j++;
}
return Polynomial(result);
}
Polynomial operator*(const Polynomial& b) const { // 乘法
vector<Term> result;
for (int i = 0; i < terms.size(); i++) {
for (int j = 0; j < b.terms.size(); j++) {
int c = terms[i].coef * b.terms[j].coef;
int e = terms[i].expo + b.terms[j].expo;
bool found = false;
for (int k = 0; k < result.size(); k++) {
if (result[k].expo == e) {
result[k].coef += c;
found = true;
break;
}
}
if (!found) {
Term t = {c, e};
result.push_back(t);
}
}
}
return Polynomial(result);
}
Polynomial differentiate() { // 求导数
vector<Term> result;
for (int i = 0; i < terms.size(); i++) {
if (terms[i].expo > 0) {
Term t = {terms[i].coef * terms[i].expo, terms[i].expo - 1};
result.push_back(t);
}
}
return Polynomial(result);
}
Polynomial integrate() { // 求积分
vector<Term> result;
for (int i = 0; i < terms.size(); i++) {
Term t = {terms[i].coef / (terms[i].expo + 1), terms[i].expo + 1};
result.push_back(t);
}
result.push_back({1, 0}); // 积分常数
return Polynomial(result);
}
Polynomial operator/(const Polynomial& b) const { // 除法
vector<Term> q, r;
Polynomial a = *this;
while (a.terms.size() > 0 && a.terms[0].expo >= b.terms[0].expo) {
int c = a.terms[0].coef / b.terms[0].coef;
int e = a.terms[0].expo - b.terms[0].expo;
vector<Term> t = {{c, e}};
Polynomial temp(t);
q = q + temp;
a = a - b * temp;
}
r = a.terms;
return Polynomial(q);
}
Polynomial operator%(const Polynomial& b) const { // 取模
vector<Term> q, r;
Polynomial a = *this;
while (a.terms.size() > 0 && a.terms[0].expo >= b.terms[0].expo) {
int c = a.terms[0].coef / b.terms[0].coef;
int e = a.terms[0].expo - b.terms[0].expo;
vector<Term> t = {{c, e}};
Polynomial temp(t);
q = q + temp;
a = a - b * temp;
}
r = a.terms;
return Polynomial(r);
}
};
int main() {
Polynomial p1, p2;
cout << "请输入第一个多项式:" << endl;
p1.input();
cout << "请输入第二个多项式:" << endl;
p2.input();
cout << "第一个多项式为: ";
p1.output();
cout << "第二个多项式为: ";
p2.output();
cout << "两个多项式相加为: ";
(p1 + p2).output();
cout << "两个多项式相减为: ";
(p1 - p2).output();
cout << "两个多项式相乘为: ";
(p1 * p2).output();
cout << "第一个多项式求导数为: ";
p1.differentiate().output();
cout << "第一个多项式求积分为: ";
p1.integrate().output();
cout << "第一个多项式除以第二个多项式的商为: ";
(p1 / p2).output();
cout << "第一个多项式除以第二个多项式的余数为: ";
(p1 % p2).output();
return 0;
}
```
注意,这只是一个简单的实现,可能存在一些问题,比如没有处理输入错误的情况,除法和取模可能会出现异常等等。如果需要用于实际应用中,还需要进行更完善的测试和优化。
阅读全文