写一个一元稀疏多项式计算的C++源代码,利用链表实现加法减法的计算
时间: 2024-02-18 18:03:32 浏览: 146
好的,这是一份简单的一元稀疏多项式计算的C++源代码,利用链表实现加法减法的计算:
```c++
#include <iostream>
using namespace std;
// 多项式项
class PolyItem {
public:
int coef; // 系数
int expn; // 指数
PolyItem* next; // 下一个项
};
// 多项式
class Poly {
public:
Poly() {
head = new PolyItem();
head->next = nullptr;
}
~Poly() {
PolyItem* p = head;
while (p) {
PolyItem* temp = p;
p = p->next;
delete temp;
}
}
// 获取多项式项的个数
int GetLength() const {
int count = 0;
PolyItem* p = head->next;
while (p) {
count++;
p = p->next;
}
return count;
}
// 添加多项式项
void AddItem(int coef, int expn) {
PolyItem* p = head;
while (p->next && p->next->expn > expn) {
p = p->next;
}
if (p->next && p->next->expn == expn) {
// 合并同类项
p->next->coef += coef;
if (p->next->coef == 0) {
// 系数为0,删除该项
PolyItem* temp = p->next;
p->next = p->next->next;
delete temp;
}
} else {
PolyItem* item = new PolyItem();
item->coef = coef;
item->expn = expn;
item->next = p->next;
p->next = item;
}
}
// 加法运算
void Add(const Poly& b) {
PolyItem* pa = head->next;
PolyItem* pb = b.head->next;
PolyItem* pc = head;
while (pa && pb) {
if (pa->expn > pb->expn) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pa->coef;
pc->expn = pa->expn;
pa = pa->next;
} else if (pa->expn < pb->expn) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pb->coef;
pc->expn = pb->expn;
pb = pb->next;
} else {
int sum = pa->coef + pb->coef;
if (sum != 0) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = sum;
pc->expn = pa->expn;
}
pa = pa->next;
pb = pb->next;
}
}
while (pa) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pa->coef;
pc->expn = pa->expn;
pa = pa->next;
}
while (pb) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pb->coef;
pc->expn = pb->expn;
pb = pb->next;
}
pc->next = nullptr;
}
// 减法运算
void Subtract(const Poly& b) {
PolyItem* pa = head->next;
PolyItem* pb = b.head->next;
PolyItem* pc = head;
while (pa && pb) {
if (pa->expn > pb->expn) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pa->coef;
pc->expn = pa->expn;
pa = pa->next;
} else if (pa->expn < pb->expn) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = -pb->coef;
pc->expn = pb->expn;
pb = pb->next;
} else {
int diff = pa->coef - pb->coef;
if (diff != 0) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = diff;
pc->expn = pa->expn;
}
pa = pa->next;
pb = pb->next;
}
}
while (pa) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = pa->coef;
pc->expn = pa->expn;
pa = pa->next;
}
while (pb) {
pc->next = new PolyItem();
pc = pc->next;
pc->coef = -pb->coef;
pc->expn = pb->expn;
pb = pb->next;
}
pc->next = nullptr;
}
// 显示多项式
void Display() const {
PolyItem* p = head->next;
while (p) {
cout << p->coef << "x^" << p->expn;
if (p->next) {
cout << " + ";
}
p = p->next;
}
cout << endl;
}
private:
PolyItem* head;
};
int main() {
Poly a, b, c;
// 添加多项式项
a.AddItem(3, 2);
a.AddItem(4, 1);
a.AddItem(5, 0);
b.AddItem(1, 2);
b.AddItem(-3, 1);
b.AddItem(2, 0);
// 显示多项式
cout << "a = ";
a.Display();
cout << "b = ";
b.Display();
// 加法运算
c = a;
c.Add(b);
cout << "a + b = ";
c.Display();
// 减法运算
c = a;
c.Subtract(b);
cout << "a - b = ";
c.Display();
return 0;
}
```
在这个源代码中,我们定义了两个类:PolyItem和Poly。PolyItem表示多项式的一项,包括系数和指数。Poly表示多项式,包括多项式项的链表和一些操作,如添加多项式项、加法运算、减法运算和显示多项式。我们可以创建两个多项式a和b,添加多项式项,然后进行加法和减法运算。最后,我们可以显示结果多项式c。
阅读全文