作者:夏雨荷Cassiopeia | 来源:互联网 | 2023-01-09 16:28
Term.h
#ifndef TERM_H
#define
TERM_H
#include
<
iostream
>
using
namespace
std;
class
Polynominal;
class
Term
...
{
public:
Term(int c,int e);
Term(int c,int e,Term* nxt);
Term* InsertAfter(int c,int e);
private:
int coef;
int exp;
Term* link;
friend ostream & operator<<(ostream &,const Term & );
friend class Polynominal;
}
;
Term::Term(
int
c,
int
e):coef(c),exp(e)
...
{
link = NULL;
}
Term::Term(
int
c,
int
e,Term
*
nxt):coef(c),exp(e)
...
{
link = nxt;
}
Term
*
Term::InsertAfter(
int
c,
int
e)
...
{
link = new Term(c,e,link);
return link;
}
ostream
&
operator
<<
(ostream
&
out
,
const
Term
&
val)
...
{
if(val.coef == 0) return out;
out<<val.coef;
switch(val.exp)...{
case 0:break;
case 1:out<<"X";break;
default:out<<"X^"<<val.exp;break;
}
return out;
}
#endif
//
TERM_H
Polynominal.h
#ifndef POLYNOMINAL_H
#define
POLYNOMINAL_H
#include
<
iostream
>
#include
"
Term.h
"
using
namespace
std;
class
Polynominal
...
{
public:
Polynominal();
void AddTerms(istream& in);
void AddLast(int c,int e);
void Output(ostream& out) const;
void PolyAdd(Polynominal& r);
void MakeEmpty();
void PolyMultiply(Polynominal& );
private:
Term* theList;
friend ostream & operator<<(ostream &, const Polynominal &);
friend istream & operator>>(istream &, Polynominal &);
friend Polynominal& operator+(Polynominal &, Polynominal &);
friend Polynominal& operator*(Polynominal &, Polynominal &);
}
;
Polynominal::Polynominal()
...
{
theList = new Term(0,-1);
theList->link = theList;
}
void
Polynominal::AddTerms(istream
&
in
)
...
{
int c,e;
Term *q = theList;
while(true)...{
cout<<"Input a term(coef,exp): "<<endl;
in>>c>>e;
if(e < 0) break;
q = q->InsertAfter(c,e);
}
}
void
Polynominal::MakeEmpty()
...
{
Term *p = theList->link;
while(p->link != theList)...{
theList->link = p->link;
delete p;
p = theList->link;
}
theList->link = theList;
delete p;
}
void
Polynominal::AddLast(
int
c,
int
e)
...
{
Term *p = theList;
while(p->link != theList) p = p->link;
Term *temp = new Term(c,e,theList);
p->link = temp;
}
void
Polynominal::Output(ostream
&
out
)
const
...
{
int first = 1;
Term *p = theList->link;
while(p != theList)...{
if(!first &&(p->coef > 0)) out<<"+";
first = 0;
out<<*p;
p = p->link;
}
}
void
Polynominal::PolyAdd(Polynominal
&
r)
...
{
Term * p = r.theList->link;
Term *q1 = theList;
Term *q = theList->link;
while(p != r.theList)...{
while((q->exp > p->exp) && (q->link != theList))...{
q = q->link;
q1 = q1->link;
}
if(q->exp < p->exp) ...{
q1->InsertAfter(p->coef,p->exp);
q1 = q1->link;
}
else if(q->exp == p->exp)...{
q->coef += p->coef;
}
else...{
q->InsertAfter(p->coef,p->exp);
q1 = q;
q = q->link;
}
p = p->link;
}
}
void
Polynominal::PolyMultiply(Polynominal
&
b)
...
{
Term *p = theList->link;
Term *q = b.theList->link;
Polynominal result,temp;
if(p == theList || q == b.theList) return;
while(p != theList)...{
while( q != b.theList)...{
temp.AddLast(p->coef*q->coef,p->exp+q->exp);
q = q->link;
}
result.PolyAdd(temp);
temp.MakeEmpty();
p = p->link;
q = b.theList->link;
}
theList = result.theList;
}
ostream
&
operator
<<
(ostream
&
out
,
const
Polynominal
&
x)
...
{
x.Output(out);
return out;
}
istream
&
operator
>>
(istream
&
in
, Polynominal
&
x)
...
{
x.AddTerms(in);
return in;
}
Polynominal
&
operator
+
(Polynominal
&
a, Polynominal
&
b)
...
{
a.PolyAdd(b);
return a;
}
Polynominal
&
operator
*
(Polynominal
&
a,Polynominal
&
b)
...
{
a.PolyMultiply(b);
return a;
}
#endif
//
POLYNOMINAL_H
main.cpp
#include
<
cstdlib
>
#include
<
iostream
>
#include
"
Polynominal.h
"
using
namespace
std;
int
main(
int
argc,
char
*
argv[])
...
{
Polynominal x,y;
cout<<"Input x :"<<endl;
cin>>x;
cout<<"x: "<<x<<endl;
cout<<"Input y:"<<endl;
cin>>y;
cout<<"y: "<<y<<endl;
cout<<"x*y: "<<x*y<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
1.如果要把多项式a的值赋给多项式b:a.theList = b.theList,不能写成a.theList->link = b.theList->link
2.考虑如下两段代码段
Term *p = theList->link;
while( p != theList) {
cout<
coef< p = p->link;
}
把theList之后的每项系数都打印出来,最后p指向theList
Term *p = theList->link;
while(p->link != theList){
cout<coef< p = p->link;
}
把theList之后的每项(除了最后一项)系数都打印出来,最后p指向最后一项
3.这些代码不能在vc 6.0上编译通过,提示友元类不能访问私有成员的错误,据说这是vc 6.0的bug,但在dev c++可以顺利编译通过.