@
ipwx 我试了一下,大概是下面的形式
```
#include <iostream>
using namespace std;
#include <string>
#include <vector>
class Mono {
public:
int coefficient;
char variable1;
int power1;
char variable2;
int power2;
Mono(int a, char p, int n = 0, char q = '1', int m = 0) : coefficient(a), variable1(p), power1(n), variable2(q), power2(m) {}
bool isSameTerm(const Mono & other) {
return(variable1 == other.variable1 && power1 == other.power1 && variable2 == other.variable2 && power2 == other.power2);
}
};
Mono operator * (int coef, const Mono & mono) {
return Mono(coef * mono.coefficient, mono.variable1, mono.power1, mono.variable2, mono.power2);
}
Mono operator * (const Mono & lhs, const Mono & rhs) {
if (lhs.variable2 == '1' && rhs.variable2 == '1') {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1, rhs.variable1, rhs.power1);
}
else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == '1') {
if (rhs.variable2 == '1') {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1);
}
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2);
}
else if (lhs.variable1 == rhs.variable1 && rhs.variable2 == '1') { // We do not need consider the case when the second variable of lhs is '1', which was already contained in the last case.
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, rhs.variable2, rhs.power2);
}
else if (lhs.variable1 == rhs.variable1 && lhs.variable2 == rhs.variable2) {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power1, lhs.variable2, lhs.power2 + rhs.power2);
}
else if (lhs.variable1 == rhs.variable2 && lhs.variable2 == rhs.variable1) {
return Mono(lhs.coefficient * rhs.coefficient, lhs.variable1, lhs.power1 + rhs.power2, lhs.variable2, lhs.power2 + rhs.power1);
}
throw -1;
}
Mono pow(char variable, int power) {
return Mono(1, variable, power);
}
class Poly {
friend ostream & operator << (ostream & os, const Poly & poly);
private:
vector<Mono> terms;
public:
Poly & addTerm(const Mono & term) {
terms.push_back(term);
return * this;
}
Poly & operator + (const Mono & term) {
addTerm(term);
return * this;
}
void simplify() {
for (size_t i = 0; i < terms.size(); i++) {
for (size_t j = i + 1; j < terms.size(); j++) {
if (terms[i].isSameTerm(terms[j])) {
terms[i].coefficient = terms[i].coefficient + terms[j].coefficient;
terms.erase(terms.begin() + j);
j--;
}
}
}
}
};
ostream & operator << (ostream & os, const Poly & poly) {
for (size_t i = 0; i < poly.terms.size(); i++) {
const auto & term = poly.terms[i];
if (term.coefficient != 0) {
if (term.power1 == 0 && term.power2 == 0) {
os << term.coefficient;
}
else if (term.power1 == 0) {
os << term.coefficient << " * pow(" << term.variable2 << ", " << term.power2 << ")";
}
else if (term.power2 == 0) {
os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ")";
}
else {
os << term.coefficient << " * pow(" << term.variable1 << ", " << term.power1 << ") * " << "pow(" << term.variable2 << ", " << term.power2 << ")";
}
if (i < poly.terms.size() - 1) {
os << " + ";
}
}
}
return os;
}
int main() {
try {
Poly poly;
poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
cout << "Polynomial before simplification:\n" << poly << endl;
poly.simplify();
cout << "Polynomial after simplification:\n" << poly << endl;
}
catch(int err0) {
cout << "You must input polynomials of two variables with aligned variables for multiplication!" << endl;
cout << "Error number: " << err0 << endl;
}
return 0;
}
```
问题是我这样写就必须这样定义一个多项式
```
Poly poly;
poly = poly + (3 * pow('p', 2) * pow('q', 1)) + (5 * pow('p', 2) * pow('q', 1)) + (2 * pow('p', 1) * pow('q', 1));
```
还有就是无法区分 pow(p, n) * pow(q, m) 和 pow(q, m) * pow(p, n) 这两种形式(除非我指定两个 char 的顺序);不过第二个问题就是我一开始想要的对于非交换的多项式的计算。。(然后我期待可以计算两个多项式的对易子,最后计算得到关于 p q 的对易子的一个多项式)