. 用单链表表示一元多项式,并实现输入、输出、加、减运算。使用C++实现代码
时间: 2024-11-07 10:13:37 浏览: 9
单链表实现一元多项式相乘.rar
5星 · 资源好评率100%
在C++中,我们可以使用结构体或类来定义一个表示一元多项式的节点,每个节点包含系数和下一个节点的指针。对于加法和减法,我们需要遍历两个多项式并合并相同次数项的系数。这里是一个简单的示例:
```cpp
#include <iostream>
// 定义多项式节点
class Term {
public:
double coefficient; // 系数
int exponent; // 次数
// 构造函数
Term(double c = 0, int e = 0) : coefficient(c), exponent(e) {}
};
// 单链表节点
class PolynomialNode {
public:
Term term;
PolynomialNode* next;
// 构造函数
PolynomialNode(Term t = Term(), PolynomialNode* n = nullptr)
: term(t), next(n) {}
};
// 链表表示的一元多项式类
class UnivariatePolynomial {
private:
PolynomialNode* head;
public:
// 添加新项到多项式末尾
void addTerm(double coefficient, int exponent) {
PolynomialNode* newNode = new PolynomialNode(Term(coefficient, exponent));
if (head == nullptr) {
head = newNode;
} else {
PolynomialNode* current = head;
while (current->next != nullptr && current->term.exponent >= exponent) {
current = current->next;
}
current->next = newNode;
}
}
// 输出多项式
void print() const {
PolynomialNode* current = head;
while (current != nullptr) {
std::cout << current->term.coefficient << "x^" << current->term.exponent << (current->next ? " + " : "\n");
current = current->next;
}
}
// 加法操作(假设两多项式系数相同次项相加)
friend UnivariatePolynomial operator+(const UnivariatePolynomial& p1, const UnivariatePolynomial& p2);
// 减法操作(假设两多项式系数相同次项相减)
friend UnivariatePolynomial operator-(const UnivariatePolynomial& p1, const UnivariatePolynomial& p2);
};
// 实现加法和减法运算符重载
UnivariatePolynomial operator+(const UnivariatePolynomial& p1, const UnivariatePolynomial& p2) {
UnivariatePolynomial result;
result.head = nullptr;
PolynomialNode* p1_current = p1.head;
PolynomialNode* p2_current = p2.head;
while (p1_current != nullptr || p2_current != nullptr) {
if (p1_current == nullptr) {
result.addTerm(p2_current->term.coefficient, p2_current->term.exponent);
p2_current = p2_current->next;
} else if (p2_current == nullptr) {
result.addTerm(p1_current->term.coefficient, p1_current->term.exponent);
p1_current = p1_current->next;
} else {
double combined_coefficient = p1_current->term.coefficient + p2_current->term.coefficient;
if (combined_coefficient != 0) {
result.addTerm(combined_coefficient, std::max(p1_current->term.exponent, p2_current->term.exponent));
}
if (p1_current->term.exponent > p2_current->term.exponent) {
p1_current = p1_current->next;
} else {
p2_current = p2_current->next;
}
}
}
return result;
}
UnivariatePolynomial operator-(const UnivariatePolynomial& p1, const UnivariatePolynomial& p2) {
UnivariatePolynomial result;
result.head = nullptr;
PolynomialNode* p1_current = p1.head;
PolynomialNode* p2_current = p2.head;
while (p1_current != nullptr || p2_current != nullptr) {
if (p1_current == nullptr) {
result.addTerm(-p2_current->term.coefficient, p2_current->term.exponent);
p2_current = p2_current->next;
} else if (p2_current == nullptr) {
result.addTerm(p1_current->term.coefficient, p1_current->term.exponent);
p1_current = p1_current->next;
} else {
double combined_coefficient = p1_current->term.coefficient - p2_current->term.coefficient;
if (combined_coefficient != 0) {
result.addTerm(combined_coefficient, std::max(p1_current->term.exponent, p2_current->term.exponent));
}
if (p1_current->term.exponent > p2_current->term.exponent) {
p1_current = p1_current->next;
} else {
p2_current = p2_current->next;
}
}
}
return result;
}
```
这个例子展示了如何创建单链表表示的一元多项式以及基本的加减运算。请注意,这里没有处理不同多项式长度不匹配的情况,实际应用中可能需要添加错误检查。
阅读全文