重载链表的 I/O 运算符
Overloading the I/O operators for a linked list
我正在尝试通过重载输入和输出运算符来读取和打印表示为链表的多项式。我必须重载它们两次:一次在 class 节点中(用于读取和打印单个节点),另一次在 class 多项式中(使用已经实现的 I/O 重载节点)。
但是,我用来为多项式重载 I/O 的友元函数无法访问 class 节点的私有成员(class 是 class 多项式的友元).
函数"inserare"只是向多项式添加一个节点。
我的代码:
using namespace std;
class nod
{
friend class polinom;
int exp,coef;
nod *urm;
friend ostream &operator<<( ostream &output, const nod &X );
friend istream &operator>>( istream &input, nod &X );
};
ostream &operator<<( ostream &output, const nod &X )
{
if(X.coef==0) output<<0;
else if(X.exp==0)
{
if(X.coef>0) output<<"+"<<X.coef;
else output<<X.coef;
}
else if(X.coef>0)
{
if(X.coef==1&&X.exp==1) output<<"+"<<"x";
if(X.coef==1&&X.exp!=1) output<<"+"<<"x^"<<X.exp;
if(X.coef!=1&&X.exp==1) output<<"+"<<X.coef<<"*x";
if(X.coef!=1&&X.exp!=1) output<<"+"<<X.coef<<"*x^"<<X.exp;
}
else
{
if(X.coef==-1&&X.exp==1) output<<"-"<<"x";
if(X.coef==-1&&X.exp!=1) output<<"-"<<"x^"<<X.exp;
if(X.coef!=-1&&X.exp==1) output<<X.coef<<"*x";
if(X.coef!=-1&&X.exp!=1) output<<X.coef<<"*x^"<<X.exp;
}
return output;
}
istream &operator>>( istream &input, nod &X )
{
input>>X.coef;
input>>X.exp;
X.urm=NULL;
return input;
}
class polinom
{
nod *prim,*curent;
public:
polinom(); //constructor
~polinom(); //destructor
polinom(const polinom& p); //copy constructor
void inserare(nod *aux);
friend ostream &operator<<( ostream &output, const polinom &X );
friend istream &operator>>( istream &input, polinom &X );
};
ostream &operator<<( ostream &output, const polinom &X )
{
for(nod* temp = X.prim; temp!= NULL; temp = temp->urm)
output<<temp;
return output;
}
void polinom::inserare(nod *aux)
{
if(prim==NULL&&aux->coef==0)
{
prim=aux;
curent=prim;
}
if(aux->coef)
{
if(prim==NULL||prim->exp>aux->exp)
{
aux->urm=prim;
prim=aux;
curent=prim;
}
else
{
curent=prim;
while(curent->urm&&curent->urm->exp<=aux->exp)
{
curent=curent->urm;
}
if(curent->exp==aux->exp)
{
curent->coef=curent->coef+aux->coef;
return;
}
if(curent->urm==NULL) curent->urm=aux;
else
{
aux->urm=curent->urm;
curent->urm=aux;
return;
}
}
}
}
istream &operator>>( istream &input, polinom &X )
{
nod temp;
do
{
input>>temp;
X.inserare(&temp);
}
while(temp.coef);
return input;
}
您需要另一组 friend
声明,以便 polynom
的 extractor/inserter 可以访问节点的数据成员:
class nod
{
friend class polinom;
int exp,coef;
nod *urm;
friend ostream &operator<<( ostream &output, const nod &X );
friend istream &operator>>( istream &input, nod &X );
friend ostream &operator<<( ostream &output, const class polinom &X );
friend istream &operator>>( istream &input, class polinom &X );
};
我正在尝试通过重载输入和输出运算符来读取和打印表示为链表的多项式。我必须重载它们两次:一次在 class 节点中(用于读取和打印单个节点),另一次在 class 多项式中(使用已经实现的 I/O 重载节点)。 但是,我用来为多项式重载 I/O 的友元函数无法访问 class 节点的私有成员(class 是 class 多项式的友元).
函数"inserare"只是向多项式添加一个节点。 我的代码:
using namespace std;
class nod
{
friend class polinom;
int exp,coef;
nod *urm;
friend ostream &operator<<( ostream &output, const nod &X );
friend istream &operator>>( istream &input, nod &X );
};
ostream &operator<<( ostream &output, const nod &X )
{
if(X.coef==0) output<<0;
else if(X.exp==0)
{
if(X.coef>0) output<<"+"<<X.coef;
else output<<X.coef;
}
else if(X.coef>0)
{
if(X.coef==1&&X.exp==1) output<<"+"<<"x";
if(X.coef==1&&X.exp!=1) output<<"+"<<"x^"<<X.exp;
if(X.coef!=1&&X.exp==1) output<<"+"<<X.coef<<"*x";
if(X.coef!=1&&X.exp!=1) output<<"+"<<X.coef<<"*x^"<<X.exp;
}
else
{
if(X.coef==-1&&X.exp==1) output<<"-"<<"x";
if(X.coef==-1&&X.exp!=1) output<<"-"<<"x^"<<X.exp;
if(X.coef!=-1&&X.exp==1) output<<X.coef<<"*x";
if(X.coef!=-1&&X.exp!=1) output<<X.coef<<"*x^"<<X.exp;
}
return output;
}
istream &operator>>( istream &input, nod &X )
{
input>>X.coef;
input>>X.exp;
X.urm=NULL;
return input;
}
class polinom
{
nod *prim,*curent;
public:
polinom(); //constructor
~polinom(); //destructor
polinom(const polinom& p); //copy constructor
void inserare(nod *aux);
friend ostream &operator<<( ostream &output, const polinom &X );
friend istream &operator>>( istream &input, polinom &X );
};
ostream &operator<<( ostream &output, const polinom &X )
{
for(nod* temp = X.prim; temp!= NULL; temp = temp->urm)
output<<temp;
return output;
}
void polinom::inserare(nod *aux)
{
if(prim==NULL&&aux->coef==0)
{
prim=aux;
curent=prim;
}
if(aux->coef)
{
if(prim==NULL||prim->exp>aux->exp)
{
aux->urm=prim;
prim=aux;
curent=prim;
}
else
{
curent=prim;
while(curent->urm&&curent->urm->exp<=aux->exp)
{
curent=curent->urm;
}
if(curent->exp==aux->exp)
{
curent->coef=curent->coef+aux->coef;
return;
}
if(curent->urm==NULL) curent->urm=aux;
else
{
aux->urm=curent->urm;
curent->urm=aux;
return;
}
}
}
}
istream &operator>>( istream &input, polinom &X )
{
nod temp;
do
{
input>>temp;
X.inserare(&temp);
}
while(temp.coef);
return input;
}
您需要另一组 friend
声明,以便 polynom
的 extractor/inserter 可以访问节点的数据成员:
class nod
{
friend class polinom;
int exp,coef;
nod *urm;
friend ostream &operator<<( ostream &output, const nod &X );
friend istream &operator>>( istream &input, nod &X );
friend ostream &operator<<( ostream &output, const class polinom &X );
friend istream &operator>>( istream &input, class polinom &X );
};