⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 poly.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include "poly.h"
#include "strutils.h"
#include <cmath>

bool Poly::ourInitialized = false;
Poly Poly::ZERO = Poly();

Poly operator + (const Poly& lhs, const Poly& rhs)
{
    Poly copy(lhs);
    copy += rhs;
    return copy;
}

ostream& operator << (ostream& out, const Poly& p)
{   out << p.tostring();
    return out;
}

Poly::Poly(Polist p)
  : myPoly(p)
{

}

Poly::Poly()
{
    if(ourInitialized)
    {   myPoly = ZERO.myPoly;
    }
    else
    {   ourInitialized = true;
        myPoly = cons(Pair(0,0),Polist());
    }
}

Poly::Poly(double coeff, int exp)
{
   if (coeff == 0)
   {   myPoly = Poly::ZERO.myPoly;
   }
   else
   {   myPoly = cons(Pair(coeff,exp),Polist());
   }
}

double Poly::at(double x) const
{
    if (IsPoly())
    {   return leadingCoeff() * pow(x,degree()) + Tail().at(x);
    }
    return 0;
}

bool Poly::IsPoly() const
{
    return ! myPoly.IsEmpty();
}

Poly Poly::Tail() const
{
    if (IsPoly())
    {   return Poly(myPoly.Tail());
    }
    return Poly(Polist());
}

Poly Poly::Head() const
{
    if (IsPoly())
    {   return Poly(leadingCoeff(),degree());
    }
    return Poly(Polist());
}


string Poly::tostring() const
{
    if (myPoly.IsEmpty())
    {   return "";
    }
    string term = ::tostring(leadingCoeff());
    if (leadingCoeff() == 1)
    {  term="";
    }
    if (degree() > 0)
    {   term += "x";
    }
    if (degree() > 1)
    {   term += "^" + ::tostring(degree());
    }
    if (Tail().IsPoly())
    {   return term + " + " + Tail().tostring();
    }
    return term;
}

double Poly::leadingCoeff() const
{
    if (myPoly.IsEmpty())
    {   return 0.0;
    }
    return myPoly.First().coeff;
}

int Poly::degree() const
{
    if (myPoly.IsEmpty())
    {   return 0;
    }
    return myPoly.First().expo;
}

const Poly& Poly::operator += (const Poly& rhs)
{
    if (! rhs.IsPoly())
    {    return *this;
    }
    if (leadingCoeff() == 0 && degree() == 0)
    {   myPoly = rhs.myPoly;
        return *this;
    }
    if (rhs.leadingCoeff() == 0)
    {   this->operator += (rhs.Tail());
        return *this;
    }
    
    
    if (degree() > rhs.degree())
    {   myPoly = cons(myPoly.First(), (Tail() + rhs).myPoly);
    }
    if (rhs.degree() > degree())
    {   myPoly = cons(rhs.myPoly.First(), (Poly(myPoly) + rhs.Tail()).myPoly);
    }
    else if (rhs.degree() == degree())
    {   myPoly = cons(Pair(leadingCoeff() + rhs.leadingCoeff(),degree()),
                      (Tail() + rhs.Tail()).myPoly);
    }
    return *this;
}

Poly operator * (const Poly& p, double c)
{
    Poly copy(p);
    copy *= c;
    return copy;
}

Poly operator * (double c, const Poly& p)
{
    return p * c;
}

const Poly& Poly::operator *= (double c)
{
    if (IsPoly())
    {    myPoly = cons (Pair(c * leadingCoeff(),degree()), (Tail()*c).myPoly);
    }
    return *this;
}

int Poly::TermsAllocated()
{
    return Polist::ConsCalls();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -