در اینجا یک نمونه برنامه در C++ وجود دارد که به کاربر اجازه می دهد دو چند جمله ای را وارد کرده و عملیات مختلفی را روی آنها انجام دهد:
#include <iostream>
#include <vector>
using namespace std;
struct Term {
int coefficient;
int exponent;
};
vector<Term> add(vector<Term> a, vector<Term> b) {
vector<Term> result;
int i = 0, j = 0;
while (i < a.size() && j < b.size()) {
if (a[i].exponent == b[j].exponent) {
int coefficient = a[i].coefficient + b[j].coefficient;
if (coefficient != 0) {
result.push_back({coefficient, a[i].exponent});
}
i++;
j++;
} else if (a[i].exponent > b[j].exponent) {
result.push_back(a[i++]);
} else {
result.push_back(b[j++]);
}
}
while (i < a.size()) {
result.push_back(a[i++]);
}
while (j < b.size()) {
result.push_back(b[j++]);
}
return result;
}
vector<Term> subtract(vector<Term> a, vector<Term> b) {
for (int i = 0; i < b.size(); i++) {
b[i].coefficient = -b[i].coefficient;
}
return add(a, b);
}
vector<Term> multiply(vector<Term> a, vector<Term> b) {
vector<Term> result;
for (int i = 0; i < a.size(); i++) {
for (int j = 0; j < b.size(); j++) {
int coefficient = a[i].coefficient * b[j].coefficient;
int exponent = a[i].exponent + b[j].exponent;
result.push_back({coefficient, exponent});
}
}
return result;
}
void display(vector<Term> a) {
for (int i = 0; i < a.size(); i++) {
cout << a[i].coefficient << "x^" << a[i].exponent;
if (i != a.size() - 1) {
cout << " + ";
}
}
cout << endl;
}
int main() {
vector<Term> a, b;
int coefficient, exponent;
// Input for first polynomial
cout ​`oaicite:{"index":0,"invalid_reason":"Malformed citation << \"Enter number of terms in the first polynomial: \";\n int n;\n cin >>"}`​ n;
cout << "Enter the terms of the first polynomial in the form of coefficient and exponent: ";
for (int i = 0; i < n; i++) {
cin >> coefficient >> exponent;
a.push_back({coefficient, exponent});
}
// Input for second polynomial
cout << "Enter number of terms in the second polynomial: ";
cin