C++ 如何使用包含单项式C+的链表实现多项式类的构造和istrem \ostrem和运算符+;
我需要创建两个类,一个用于单项式。另一个用于多项式(使用单项式链接列表构建)。 我创建了单项式类,它还在工作,但我一直坚持使用多项式类的实现 接线员:= +,-(多项式之间以及多项式与多项式之间) =+,=-(带右多项式或右多项式) -不羁的 ==, =! I/O操作员 ()重估操作员。也就是说,p(r)返回x=r的多项式值p []系数检索和定位运算符。 我需要一个操作员的指导,以及如何构建istrem\ostrem\constructors,从那里我将独自继续 这是主要问题:C++ 如何使用包含单项式C+的链表实现多项式类的构造和istrem \ostrem和运算符+;,c++,oop,C++,Oop,我需要创建两个类,一个用于单项式。另一个用于多项式(使用单项式链接列表构建)。 我创建了单项式类,它还在工作,但我一直坚持使用多项式类的实现 接线员:= +,-(多项式之间以及多项式与多项式之间) =+,=-(带右多项式或右多项式) -不羁的 ==, =! I/O操作员 ()重估操作员。也就是说,p(r)返回x=r的多项式值p []系数检索和定位运算符。 我需要一个操作员的指导,以及如何构建istrem\ostrem\constructors,从那里我将独自继续 这是主要问题: #include
#include <iostream>
#include <iomanip>
#include "Monomial.h"
#include "Polynomial.h"
using namespace std;
void testMonomial() {
Monomial m1, m2;
do {
cout << "Enter two monomials m1 and m2: " << endl;
cin >> m1 >> m2;
cout << "m1= " << m1 << ",m2= " << m2 << endl;
cout << "m1+m2= " << m1 + m2 << endl;
cout << "m1-m2= " << m1 - m2 << endl;
cout << "m1*m2= " << m1 * m2 << endl;
m1 *= 2; //m1=m1*2
cout << "m1 *= 2, m1= " << m1 << endl;
m2 += m1; // m2 = m2 + m1
cout << "m2 += m1, m1= " << m1 << ",m2=" << m2 << endl;
}
while (m1 != 0 || m2 != 0);
}
void testPolynomial() {
Polynomial p;
cout << "p=" << p << endl;
cout << "Adding one to p" << endl;
p += Monomial(1);
cout << "p=" << p << endl;
cout << "Adding x^2 to p" << endl;
p += Monomial(1, 2);
cout << "p=" << p << endl;
cout << "Adding -x^2 to p" << endl;
p += Monomial(-1, 2);
cout << "p=" << p << endl;
cout << "Number of Monomials=" << Monomial::getNumberOfMonomials() << endl;
do {
cout << "Enter a polynomial: " << endl;
cin >> p;
cout << p << endl;
cout << "p(0)=" << p(0) << ", p(1)=" << p(1) << ", p(2)=" << p(2) << endl;
cout << "p[0]=" << p[0] << ", p[1]=" << p[1] << ", p[2]=" << p[2] << ", p[4]=" << p[4] << endl;
cout << "p+p=" << p + p << endl;
} while (p != Monomial(0));
}
//Enter a polynomial :
1
p(0) = 1, p(1) = 1, p(2) = 1
p[0] = 1, p[1] = 0, p[2] = 0, p[4] = 0
p + p = 2
int main() {
cout << boolalpha;
testMonomial();
cout << "Number of Monomials=" << Monomial::getNumberOfMonomials() << endl;
testPolynomial();
cout << "Number of Monomials=" << Monomial::getNumberOfMonomials() << endl;
return 0;
}
enter code here
#包括
#包括
#包括“单项式.h”
#包括“多项式.h”
使用名称空间std;
单项式(){
单项式m1,m2;
做{
cout m1>>m2;
cout这个网站不是一个代码编写服务。请解释一下你当前的代码出了什么问题,并提出一个简单的问题,我们可以回答。另外,在拼写上花点功夫也是值得的。我知道你不是一个以英语为母语的人,但是修复诸如“constructor”和“includind”的“constructure”这样的错误并不难“为了‘包括’。我写了我需要帮助如何实现istrem\ostrem,以及polinom类中的一个操作符,我将独自继续3x^2+4
=7x^4
”,这对我来说似乎很奇怪。
friend ostream & operator<< (ostream & out, const Monomial & mono);
friend istream & operator>>(istream &, Monomial &);
public:
Monomial(void);
Monomial(int coefficient, int degree);
Monomial(const Monomial &other);
Monomial(int a);
~Monomial();
void setCoff(int);
void setExpo(int);
bool operator != (const Monomial &other);
bool operator != (int other);
const Monomial& operator = (const Monomial&);
Monomial operator + (const Monomial&) const;
Monomial operator - (const Monomial&) const;
Monomial operator * (const Monomial&);
const Monomial operator*=(const Monomial&);
const Monomial operator*=(int num);
const Monomial operator += (const Monomial&);
const Monomial operator+=(int num);
// Monomial operator- (const Monomial& right) const;
int getCoefficient() const;
int getDegree() const;
void print() const;
static int getNumberOfMonomials();
private:
int coefficient;
int degree;
static int numberOfMonomials;
};
int Monomial::numberOfMonomials = 0;
Monomial::Monomial(void)
{
numberOfMonomials++;
}
Monomial::Monomial(int a)
{
setCoff(a);
setExpo(0);
numberOfMonomials++;
}
Monomial::Monomial(int a, int b)
{
setCoff(a);
setExpo(b);
numberOfMonomials++;
}
Monomial::Monomial(const Monomial& other) : degree(other.degree)
{
this->coefficient = other.coefficient;
numberOfMonomials++;
}
Monomial::~Monomial()
{
numberOfMonomials--;
}
int Monomial::getCoefficient() const
{
return coefficient;
}
int Monomial::getDegree() const
{
return degree;
}
void Monomial::setCoff(int x)
{
coefficient = x;
}
void Monomial::setExpo(int x)
{
degree = x;
}
const Monomial& Monomial:: operator = (const Monomial& right)
{
coefficient = right.coefficient;
degree = right.degree;
return (*this);
}
Monomial Monomial:: operator + (const Monomial& right) const
{
Monomial res;
if (right.degree == degree)
{
if (right.degree != 0)
{
res.setCoff(this->getCoefficient() + right.getCoefficient());
res.setExpo(this->getDegree() + res.getDegree());
return (res);
}
res.setCoff(this->getCoefficient() + right.getCoefficient());
res.setExpo(0);
return (res);
}
res.setCoff(this->getCoefficient());
res.setExpo(this->getDegree());
return (res);
}
Monomial Monomial:: operator - (const Monomial& right) const
{
Monomial res;
if (right.degree == degree)
{
res.setCoff(this->getCoefficient() - right.getCoefficient());
res.setExpo(this->getDegree());
return (res);
}
res.setCoff(this->getCoefficient());
res.setExpo(this->getDegree());
return (res);
}
Monomial Monomial:: operator * (const Monomial& right)
{
Monomial res;
res.setCoff(coefficient * right.getCoefficient());
res.setExpo(degree + right.getDegree());
return (res);
}
const Monomial Monomial:: operator*=(const Monomial& right)
{
Monomial res;
if (right.getCoefficient() == 0)
{
this->coefficient *= 0;
this->degree *= 0;
return (*this);
/* res.coefficient *= 0;
res.degree *= 0;
return Monomial(coefficient, degree);*/
}
this->coefficient *= right.getCoefficient();
this->degree += right.getDegree();
return (*this);
}
const Monomial Monomial:: operator*=(int num)
{
this->coefficient *= num;
this->degree /*+= this->getDegree()*/;
return (*this);
}
const Monomial Monomial:: operator += (const Monomial& right)
{
if (right.degree == degree)
{
return Monomial(this->coefficient += right.coefficient, degree);
}
return (*this);
}
const Monomial Monomial:: operator+=(int num)
{
this->coefficient += num;
this->degree += this->getDegree();
return (*this);
}
bool Monomial:: operator != (const Monomial& other)
{
if (this->getCoefficient() == other.getCoefficient() && this->getDegree() == other.getDegree())
{
return true;
}
return false;
}
bool Monomial:: operator != (int other)
{
if (this->getDegree() == 0)
{
if (this->getCoefficient() == other)
{
return true;
}
}
return false;
}
void Monomial::print() const {
if (coefficient != 1 || degree == 0) cout << coefficient;
if (coefficient != 1 && degree > 0) cout << "*";
if (degree > 0) cout << "x";
if (degree > 1) cout << "^" << degree;
}
int Monomial::getNumberOfMonomials() {
return numberOfMonomials;
}
ostream &operator<<(ostream &out, const Monomial &r)
{
if (r.coefficient == 0 || r.coefficient != 0)
{
if (r.coefficient == 1 && r.degree == 1)
out << "x";
else if (r.coefficient == 0 && r.degree == 0)
out << "0";
else if (r.coefficient == (-1) && r.degree == 1)
out << r.coefficient << " * x";
else if (r.degree == 1)
out << r.coefficient << " * x";
else if (r.degree == 0 && r.coefficient != 0)
out << r.coefficient;
else if (r.coefficient == 1 && r.degree)
out << " x ^ " << r.degree;
else
out << r.coefficient << " * x ^ " << r.degree;
}
return out;
}
istream &operator>>(istream &in, Monomial &r)
{
in >> r.coefficient;
in >> r.degree;
return in;
}