C++ 如何重载加法运算符以处理链表?
我正在建立一个程序,该程序使用链表来存储多项式。基本上,用户以.txt文件的名称输入,打开、读取该文件,并将系数和指数插入到每个节点的数据字段中。节点表示多项式中的项。我有两个类,外部类是多项式,嵌套类是项。我已经开始编写重载加法运算符的定义。到目前为止,我的基本想法是将一个for循环嵌套在另一个for循环中。我只是不知道如何将结果存储在我创建的新多项式对象中C++ 如何重载加法运算符以处理链表?,c++,linked-list,shared-ptr,C++,Linked List,Shared Ptr,我正在建立一个程序,该程序使用链表来存储多项式。基本上,用户以.txt文件的名称输入,打开、读取该文件,并将系数和指数插入到每个节点的数据字段中。节点表示多项式中的项。我有两个类,外部类是多项式,嵌套类是项。我已经开始编写重载加法运算符的定义。到目前为止,我的基本想法是将一个for循环嵌套在另一个for循环中。我只是不知道如何将结果存储在我创建的新多项式对象中 #include "polynomial.h" #include <sstream> #include <cmath&
#include "polynomial.h"
#include <sstream>
#include <cmath>
double Polynomial::TOL = .000000000001; // tolerance for floating pt. equality
Polynomial::Term::Term( double c, int e, shared_ptr<Term> n )
{
coeff = c; exponent = e; next = n;
}
Polynomial::Polynomial()
{
head = nullptr;
}
Polynomial::Polynomial( string & str )
{
stringstream ss( str ); // stringstream lets us extract items separated by
// whitespace simply by using the >> operator
double coefficient; // to hold the coefficient
int exp; // to hold the exponent
head = nullptr; // initialize head to null
// read in coefficient-exponent pairs and add each term to the list
// ----------------------------------------------------------------
while (ss >> coefficient >> exp)
if (coefficient != 0) // don't make a 0 term
head = shared_ptr<Term>(new Term( coefficient, exp, head ));
}
#包括“polynomy.h”
#包括
#包括
双多项式::TOL=.000000000001;//浮动pt的公差。平等
多项式::项::项(双c,整数e,共享)
{
系数=c;指数=e;下一个=n;
}
多项式::多项式()
{
水头=零PTR;
}
多项式::多项式(字符串和str)
{
stringstream ss(str);//stringstream允许我们提取由
//只需使用>>运算符即可使用空格
双系数;//保存系数
int exp;//保存指数
head=nullptr;//将head初始化为null
//读入系数指数对,并将每个项添加到列表中
// ----------------------------------------------------------------
而(ss>>系数>>经验)
如果(系数!=0)//不要做0项
head=共享的ptr(新术语(系数、经验、head));
}
迄今为止重载的加法函数
Polynomial Polynomial::operator+( const Polynomial &other ) const
{
shared_ptr<Polynomial> result = shared_ptr<Polynomial>(new Polynomial()); // where you are going to store your results
shared_ptr<Polynomial::Term> a = this->head;
shared_ptr<Polynomial::Term> b = other.head;
while(a != nullptr && b != nullptr)
{
for(a; a != nullptr; a->next)
{
for(b; b!=nullptr; b->next)
{
if(a->exponent == b->exponent)
{
sum = a->coeff+b->coeff;
result = shared_ptr<Term> newTerm (new Term( sum, exp, head ));
}
}
return newTerm;
}
}
多项式::运算符+(常数多项式和其他)常数
{
shared_ptr result=shared_ptr(新多项式());//将结果存储在何处
共享\u ptr a=此->头部;
共享_ptr b=其他头;
while(a!=nullptr&&b!=nullptr)
{
对于(a;a!=nullptr;a->next)
{
对于(b;b!=nullptr;b->next)
{
如果(a->指数==b->指数)
{
总和=a->coeff+b->coeff;
结果=共享的新项(新项(总和、经验、总分));
}
}
返回新项;
}
}
Advice-首先重载操作符+=
。然后根据+=
实现+
。Advice 2:我已经开始编写重载加法操作符的定义了
不要在不知道最终结果的情况下编写代码。你会把自己弄到一个角落。首先规划你的设计。哟你有一个三重嵌套循环来实现两个多项式的加法。听起来不对。还有,你为什么不使用std::list
?。这使得代码更简单,更不容易出错。+
函数中的逻辑是错误的;它在a
的第一次迭代后返回。我想你是在要求n将两个多项式相加的算法。你有一个正确的想法,你需要迭代它们,但这需要某种拼接操作。我认为在你通过共享ptr
管理的术语
中包含下一步
是一个设计缺陷。它使得共享ptr
毫无意义,因为e如果在不同的多项式中有相同的项(指数/系数),那么它们必须使用不同的实例,因为它们的下一个实例将不同。相反,多边形可以存储一个std::forward_list
。但是,很可能sizeof(shared_ptr)>sizeof(项)
所以在我看来,在这里只使用std::forward\u list
更合适。使用shared\u ptr会使代码复杂化,我看不到这一阶段的好处。