#include "Polynomial.h"
term::term(double c, int e)
{
coefficient = c;
exponent = e;
}
//重载“输出号”<<
ostream& operator<<(ostream& out, term a)
{
//正常的多项式若有常数项,必然是第一个
//若指数等于0,即常数项,一般只打印系数
if (a.exponent == 0)
out << a.coefficient;
else
{
//若系数等于1,一般不再打印该系数,只打印“+”
if (a.coefficient != 1)
{
out << showpos << a.coefficient;
}
else
out << '+';
out << 'x';
//若指数等于1,一般不再打印该指数
if (a.exponent != 1)
out << '^' <<noshowpos<< a.exponent;
}
out << ' ';
return out;
}
term term::operator+(term a)
{
term t(0, 0);
if (this->exponent == a.exponent)//只有系数相等才加,系数不等什么也不做
{
t.coefficient = this->coefficient + a.coefficient;
t.exponent = this->exponent;
}
return t;
}
//重载乘号
term term::operator*(term a)
{
term t;
t.coefficient = this->coefficient * a.coefficient;
t.exponent = this->exponent + a.exponent;
return t;
}
cmp_result term_cmp(term t1, term t2)
{
if (t1.exponent < t2.exponent) return cmp_result::less;
else if (t1.exponent == t2.exponent) return cmp_result::equal;
else return cmp_result::greater;
}
ostream& operator<<(ostream& out, Polynomail aPolynomail)
{
if (!(aPolynomail.theTermList.empty()))
{
uint16_t number = aPolynomail.theTermList.size();
out << "共有" << number << "项" << endl;
list<term>::iterator it = aPolynomail.theTermList.begin();
for (int i = 0; i < number; i++)
{
out << *it;
it++;
}
}
return out;
}
Polynomail::Polynomail()
{
}
Polynomail::Polynomail(term* const t, uint16_t const n)
{
for (int i = 0; i < n; i++)
{
this->theTermList.push_back(t[i]);
}
}
//完成多项式相加运算,即:Pc=Pa+Pb
Polynomail Polynomail::operator+(Polynomail& Pb)
{
Polynomail Pc;
list<term>::iterator ita = this->theTermList.begin();//自己就是Pa
list<term>::iterator itb = Pb.theTermList.begin();
double sum = 0;
while (ita != this->theTermList.end() && itb != Pb.theTermList.end())
{
switch (term_cmp(*ita, *itb))//比较多项式的指数
{
case cmp_result::less://如果ita的指数比itb的小
Pc.theTermList.push_back(*ita);//插入ita
ita++;
break;
case cmp_result::greater://如果ita的指数比itb的大
Pc.theTermList.push_back(*itb);//插入itb
itb++;
break;
case cmp_result::equal://如果两项指数相等
sum = ita->coefficient + itb->coefficient;//得到新的项的系数
if (sum != 0)//如果系数不为零
{
Pc.theTermList.push_back(term(sum, ita->exponent));//插入新的项
}
//系数相加为0,这一项就没了,不用插入什么
ita++;
itb++;
break;
default:
break;
}
}
//插入Pa中剩余结点
while (ita != this->theTermList.end())
{
Pc.theTermList.push_back(*ita);
ita++;
}
//插入Pb中剩余结点
while (itb != Pb.theTermList.end())
{
Pc.theTermList.push_back(*itb);
itb++;
}
return Pc;
}
//重载乘号实现多项式和一项相乘
Polynomail Polynomail::operator*(term& t)
{
Polynomail tem;
list<term>::iterator it = this->theTermList.begin();
uint16_t num = this->theTermList.size();
for (uint16_t i = 0; i < num; i++)
{
tem.theTermList.push_back((*it) * t);//新的多项式每项为,原多项式各项乘该项
it++;
}
return tem;
}
//两多项式相乘,即:Pa=Pa*Pb
Polynomail Polynomail::operator*(Polynomail& Pb)
{
Polynomail Pc;//保存最终结果
Polynomail tem;//用来进行多项式和某一项相乘
//link它begin就是第一个存储数据的结点,最多自增Pb.size()-1次,请注意
for (list<term>::iterator itb = Pb.theTermList.begin(); itb!=Pb.theTermList.end(); itb++)
{
tem = (*this) * (*itb);
Pc = Pc + tem;
}
return Pc;
}
cpp链表实现一元多项式表示,相加,相乘
需积分: 0 59 浏览量
更新于2024-06-18
1
收藏 3KB 7Z 举报
在计算机科学中,一元多项式是数学中的一个重要概念,它可以被用来表示一系列系数与变量的线性组合。在编程中,特别是在C++这样的语言中,我们常常需要以数据结构的形式来表示和操作这些多项式。在这个场景下,链表是一种非常适合的数据结构,因为它可以动态地存储不同长度和顺序的项。下面我们将详细讨论如何使用C++通过链表实现一元多项式,以及如何实现多项式的相加和相乘。
我们需要创建一个节点类来表示多项式的每一项。每个节点应包含系数(coefficient)和指数(exponent)两个属性。系数通常是整数或浮点数,而指数则是一个非负整数。例如:
```cpp
class Node {
public:
int coefficient;
int exponent;
Node* next;
};
```
接着,我们需要一个类来管理整个多项式链表。这个类应该包含一些基本操作,如构造函数、析构函数、添加项、相加和相乘等。在`Polynomial`类中,我们通常会维护一个头节点,用于指向链表的第一个元素:
```cpp
class Polynomial {
private:
Node* head;
public:
Polynomial() : head(nullptr) {}
~Polynomial() { // 注意释放内存 }
// 其他成员函数...
};
```
添加项到链表中可以通过创建新的节点并插入到正确的位置来完成。我们可以按照指数降序排列多项式项,以确保打印出的多项式形式上是正确的:
```cpp
void Polynomial::addTerm(int coefficient, int exponent) {
Node* newNode = new Node{coefficient, exponent, nullptr};
if (!head) {
head = newNode;
} else {
Node* current = head;
while (current->next && current->next->exponent > exponent) {
current = current->next;
}
newNode->next = current->next;
current->next = newNode;
}
}
```
接下来,我们讨论相加操作。两个多项式相加需要遍历两个链表,并将具有相同指数的项合并。如果一个多项式中没有某个指数的项,那么另一个多项式的相应项可以直接保留:
```cpp
Polynomial Polynomial::add(const Polynomial& other) const {
Polynomial result;
Node* thisCurrent = head;
Node* otherCurrent = other.head;
while (thisCurrent && otherCurrent) {
if (thisCurrent->exponent == otherCurrent->exponent) {
result.addTerm(thisCurrent->coefficient + otherCurrent->coefficient, thisCurrent->exponent);
thisCurrent = thisCurrent->next;
otherCurrent = otherCurrent->next;
} else if (thisCurrent->exponent > otherCurrent->exponent) {
result.addTerm(thisCurrent->coefficient, thisCurrent->exponent);
thisCurrent = thisCurrent->next;
} else {
result.addTerm(otherCurrent->coefficient, otherCurrent->exponent);
otherCurrent = otherCurrent->next;
}
}
while (thisCurrent) {
result.addTerm(thisCurrent->coefficient, thisCurrent->exponent);
thisCurrent = thisCurrent->next;
}
while (otherCurrent) {
result.addTerm(otherCurrent->coefficient, otherCurrent->exponent);
otherCurrent = otherCurrent->next;
}
return result;
}
```
我们考虑相乘操作。多项式相乘较为复杂,因为每个项都需要与其他项相乘,生成新的项。我们可以使用Karatsuba算法或Long Multiplication算法,但这里我们简单地展示一个基于迭代的直观方法:
```cpp
Polynomial Polynomial::multiply(const Polynomial& other) const {
Polynomial result;
for (Node* thisCurrent = head; thisCurrent; thisCurrent = thisCurrent->next) {
for (Node* otherCurrent = other.head; otherCurrent; otherCurrent = otherCurrent->next) {
int newExponent = thisCurrent->exponent + otherCurrent->exponent;
int newCoefficient = thisCurrent->coefficient * otherCurrent->coefficient;
result.addTerm(newCoefficient, newExponent);
}
}
return result;
}
```
在实际应用中,为了提高效率,我们可能还需要对上述算法进行优化,比如使用动态规划或者更高效的多项式乘法算法。同时,别忘了在类中实现其他必要的辅助函数,如打印多项式、比较多项式大小等,以便于测试和使用。
在提供的`start.cpp`文件中,你可能找到了一个主程序,用于创建多项式实例,执行加法和乘法操作,并打印结果。`Polynomial.cpp`和`Polynomial.h`分别包含了`Polynomial`类的实现和声明。通过理解并实现这些代码,你可以掌握链表在表示和操作一元多项式中的应用。

菜哥万岁万岁万万岁
- 粉丝: 210
最新资源
- 基于MATLAB的PCM仿真.doc
- 云计算毕业论文.docx
- 大数据驱动的区域卫生平台建设方案PPT课件.ppt
- 自动化立体仓库毕业论文.doc
- 基于JAVA的购物网站(毕业论文).doc
- 计算机基础笔试试卷.doc
- 基于Web的在线实时通讯系统.doc
- 基于单片机的万年历实习报告.docx
- 基于无线传感网络的路灯采集系统.doc
- 多项目管理体系论文:地产公司大规模多项目开发条件下的产品保障体系.doc
- 自动化生产线安装与调试毕业论文.doc
- 实验三-信号卷积的MATLAB实现.doc
- c语言期末复习试卷.doc
- 信息系统安全自查报告.doc
- 基于matlab的伪随机序列生成及相关函数仿真实验.doc
- PLC和变频器综合实验报告.doc