按照升幂排列的一元多项式P n (x)=p 1 x+p 2 x 2 +⋯+p n x n 可以用线性表来表示P=(p 1 ,p 2 ,…,p n ),对于一元多项式各种操作,实际上可以利用线性表来处理。若多项式的非零项指数很高并且非零项很少称之为稀疏多项式,此时使用链式存储结构较为方便。设计一个程序,实现一元稀疏多项式简单计算器。 基本要求 稀疏多项式简单计算器的基本功能是: (1)输入并建立多项式; (2)输出多项式,输出形式为整数序列:n,p 1 ,e 1 ,p 2 ,e 2 …,p n ,e n ,其中n是多项式的项数,p i 和e i 分别是第i项的系数和指数,序列按指数升幂排列; (3) 多项式a和b相加,建立多项式a+b; (4) 多项式a和c相减,建立多项式a-b; (5) 计算多项式在x处的值。
时间: 2024-03-22 11:40:26 浏览: 148
好的,我可以帮你实现这个程序。这里提供一种基于链式存储结构的实现方法:
1. 定义节点结构体PolyNode,包含系数和指数两个成员变量,以及指向下一个节点的指针next。
```
struct PolyNode {
int coef; // 系数
int expn; // 指数
PolyNode *next; // 下一个节点指针
};
```
2. 定义多项式类Polynomial,包含多项式的头节点指针head和项数n。提供以下成员函数:
- 构造函数Polynomial():初始化头节点指针为空,项数为0。
- 析构函数~Polynomial():释放所有节点内存。
- void createPoly():输入建立多项式。
- void printPoly():输出多项式。
- Polynomial operator+(const Polynomial &b):多项式加法。
- Polynomial operator-(const Polynomial &c):多项式减法。
- int calcPoly(int x):计算多项式在x处的值。
下面是具体实现:
```
class Polynomial {
private:
PolyNode *head; // 多项式头节点指针
int n; // 多项式项数
public:
Polynomial() {
head = nullptr;
n = 0;
}
~Polynomial() {
PolyNode *p = head, *q;
while (p) {
q = p->next;
delete p;
p = q;
}
head = nullptr;
n = 0;
}
void createPoly() {
cout << "请输入多项式的项数n:";
cin >> n;
PolyNode *p = head, *q;
for (int i = 1; i <= n; i++) {
int coef, expn;
cout << "请输入第" << i << "项的系数和指数:";
cin >> coef >> expn;
q = new PolyNode;
q->coef = coef;
q->expn = expn;
q->next = nullptr;
if (!head) {
head = q;
p = q;
} else {
p->next = q;
p = q;
}
}
}
void printPoly() {
PolyNode *p = head;
cout << "多项式的项数为:" << n << endl;
cout << "多项式各项系数和指数依次为:";
while (p) {
cout << p->coef << " " << p->expn << " ";
p = p->next;
}
cout << endl;
}
Polynomial operator+(const Polynomial &b) {
Polynomial c;
PolyNode *p = head, *q = b.head, *r = c.head, *s;
while (p && q) {
if (p->expn < q->expn) {
s = new PolyNode;
s->coef = p->coef;
s->expn = p->expn;
s->next = nullptr;
if (!c.head) {
c.head = s;
r = s;
} else {
r->next = s;
r = s;
}
c.n++;
p = p->next;
} else if (p->expn > q->expn) {
s = new PolyNode;
s->coef = q->coef;
s->expn = q->expn;
s->next = nullptr;
if (!c.head) {
c.head = s;
r = s;
} else {
r->next = s;
r = s;
}
c.n++;
q = q->next;
} else {
s = new PolyNode;
s->coef = p->coef + q->coef;
s->expn = p->expn;
s->next = nullptr;
if (!c.head) {
c.head = s;
r = s;
} else {
r->next = s;
r = s;
}
c.n++;
p = p->next;
q = q->next;
}
}
while (p) {
s = new PolyNode;
s->coef = p->coef;
s->expn = p->expn;
s->next = nullptr;
r->next = s;
r = s;
c.n++;
p = p->next;
}
while (q) {
s = new PolyNode;
s->coef = q->coef;
s->expn = q->expn;
s->next = nullptr;
r->next = s;
r = s;
c.n++;
q = q->next;
}
return c;
}
Polynomial operator-(const Polynomial &c) {
Polynomial d;
PolyNode *p = head, *q = c.head, *r = d.head, *s;
while (p && q) {
if (p->expn < q->expn) {
s = new PolyNode;
s->coef = p->coef;
s->expn = p->expn;
s->next = nullptr;
if (!d.head) {
d.head = s;
r = s;
} else {
r->next = s;
r = s;
}
d.n++;
p = p->next;
} else if (p->expn > q->expn) {
s = new PolyNode;
s->coef = -q->coef;
s->expn = q->expn;
s->next = nullptr;
if (!d.head) {
d.head = s;
r = s;
} else {
r->next = s;
r = s;
}
d.n++;
q = q->next;
} else {
s = new PolyNode;
s->coef = p->coef - q->coef;
s->expn = p->expn;
s->next = nullptr;
if (!d.head) {
d.head = s;
r = s;
} else {
r->next = s;
r = s;
}
d.n++;
p = p->next;
q = q->next;
}
}
while (p) {
s = new PolyNode;
s->coef = p->coef;
s->expn = p->expn;
s->next = nullptr;
r->next = s;
r = s;
d.n++;
p = p->next;
}
while (q) {
s = new PolyNode;
s->coef = -q->coef;
s->expn = q->expn;
s->next = nullptr;
r->next = s;
r = s;
d.n++;
q = q->next;
}
return d;
}
int calcPoly(int x) {
int result = 0;
PolyNode *p = head;
while (p) {
result += p->coef * pow(x, p->expn);
p = p->next;
}
return result;
}
};
```
这样,我们就可以通过链式存储结构实现一元稀疏多项式简单计算器了。
阅读全文