设计一个一元稀疏多项式简单计算器。 【基本要求】 (1)输入并建立两个多项式; (2)多项式a与b相加,建立和多项式c; (3)多项式a与b相减,建立差多项式d; (3)输出多项式a,b,c,d。输出格式:比如多项式a为:A(x)=clxel+c2xe2+…+ cmxem,其中,ci和ei分别为第i项的系数和指数,且各项按指数的升幂 排列,即0<el<e2<…<em。 【测试数据】 (1)(1+x+x2+x3+x4+x5)+(-x3-x4)=(1+x+x2+x5) (2)(x+x100)+(x100+x200)=(x+2x100+x200) (3)(2x+5×8-3x11)+(7-5x8+11x9)=(7+2x+11x9-3x11)
时间: 2023-05-23 14:04:09 浏览: 164
以下是一个一元稀疏多项式计算器的完整代码:
#include <iostream>
#include <vector>
using namespace std;
// 多项式项的结构体
struct polyItem {
int coef; // 系数
int exp; // 指数
};
// 多项式类
class Polynomial {
public:
Polynomial() {
items.clear(); // 清空项
}
// 添加一项
void addItem(int coef, int exp) {
polyItem item = { coef, exp };
items.push_back(item);
}
// 获取当前多项式的字符串表示
string toString() const {
string str = "";
for (int i = 0; i < items.size(); i++) {
const polyItem &item = items[i];
char buf[32];
snprintf(buf, sizeof(buf), "%+dx^%d", item.coef, item.exp);
str += buf;
}
return str;
}
// 求和
static Polynomial add(const Polynomial &a, const Polynomial &b) {
Polynomial c;
int i = 0, j = 0;
while (i < a.items.size() && j < b.items.size()) {
const polyItem &item1 = a.items[i];
const polyItem &item2 = b.items[j];
if (item1.exp == item2.exp) {
int coef = item1.coef + item2.coef;
if (coef != 0) {
c.addItem(coef, item1.exp);
}
i++, j++;
} else if (item1.exp < item2.exp) {
c.addItem(item1.coef, item1.exp);
i++;
} else {
c.addItem(item2.coef, item2.exp);
j++;
}
}
for (; i < a.items.size(); i++) {
c.addItem(a.items[i].coef, a.items[i].exp);
}
for (; j < b.items.size(); j++) {
c.addItem(b.items[j].coef, b.items[j].exp);
}
return c;
}
// 求差
static Polynomial sub(const Polynomial &a, const Polynomial &b) {
Polynomial d;
int i = 0, j = 0;
while (i < a.items.size() && j < b.items.size()) {
const polyItem &item1 = a.items[i];
const polyItem &item2 = b.items[j];
if (item1.exp == item2.exp) {
int coef = item1.coef - item2.coef;
if (coef != 0) {
d.addItem(coef, item1.exp);
}
i++, j++;
} else if (item1.exp < item2.exp) {
d.addItem(item1.coef, item1.exp);
i++;
} else {
d.addItem(-item2.coef, item2.exp);
j++;
}
}
for (; i < a.items.size(); i++) {
d.addItem(a.items[i].coef, a.items[i].exp);
}
for (; j < b.items.size(); j++) {
d.addItem(-b.items[j].coef, b.items[j].exp);
}
return d;
}
private:
vector<polyItem> items; // 多项式项的数组
};
int main() {
// 读入多项式a
cout << "Input the polynomial a (e.g. 1x^2+2x+3): ";
string strA;
getline(cin, strA);
Polynomial a;
int pos = 0;
while (pos < strA.length()) {
int coef = 0, exp = 0;
if (strA[pos] == '+') {
pos++;
} else if (strA[pos] == '-') {
coef = -1;
pos++;
} else {
coef = 1;
}
while (pos < strA.length() && strA[pos] >= '0' && strA[pos] <= '9') {
coef = coef * 10 + (strA[pos] - '0');
pos++;
}
if (pos >= strA.length() || strA[pos] != 'x') {
cerr << "Invalid input format" << endl;
return 1;
}
pos++;
if (pos >= strA.length() || strA[pos] != '^') {
cerr << "Invalid input format" << endl;
return 1;
}
pos++;
while (pos < strA.length() && strA[pos] >= '0' && strA[pos] <= '9') {
exp = exp * 10 + (strA[pos] - '0');
pos++;
}
a.addItem(coef, exp);
}
// 读入多项式b
cout << "Input the polynomial b (e.g. 1x^2+2x+3): ";
string strB;
getline(cin, strB);
Polynomial b;
pos = 0;
while (pos < strB.length()) {
int coef = 0, exp = 0;
if (strB[pos] == '+') {
pos++;
} else if (strB[pos] == '-') {
coef = -1;
pos++;
} else {
coef = 1;
}
while (pos < strB.length() && strB[pos] >= '0' && strB[pos] <= '9') {
coef = coef * 10 + (strB[pos] - '0');
pos++;
}
if (pos >= strB.length() || strB[pos] != 'x') {
cerr << "Invalid input format" << endl;
return 1;
}
pos++;
if (pos >= strB.length() || strB[pos] != '^') {
cerr << "Invalid input format" << endl;
return 1;
}
pos++;
while (pos < strB.length() && strB[pos] >= '0' && strB[pos] <= '9') {
exp = exp * 10 + (strB[pos] - '0');
pos++;
}
b.addItem(coef, exp);
}
// 计算和、差并输出
Polynomial c = Polynomial::add(a, b);
Polynomial d = Polynomial::sub(a, b);
cout << "a(x) = " << a.toString() << endl;
cout << "b(x) = " << b.toString() << endl;
cout << "c(x) = " << c.toString() << endl;
cout << "d(x) = " << d.toString() << endl;
return 0;
}
关于中文加密,我需要更多的上下文信息才能回答。
阅读全文