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

📄 poly.h

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 H
字号:
#ifndef _POLY_H
#define _POLY_H

#include <string>
using namespace std;
#include "clist.h"

// polynomials in 'x' (can be easily modified for
// polys in any variable, and templated for polys of ..)
//
// coefficients are doubles
// Poly() or Poly::ZERO represent 0, otherwise
// Poly(a,b) represents ax^b
//
// polynomials of more than one term are constructed using +=, e.g.,
// Poly a = Poly(5,3) + Poly(4,2) + Poly(3,1) + Poly(2,0)
// then a = 5x^3 + 4x^2 + 3x + 2
//
// Head() returns the leading term, Tail() returns all but Head()
// both return Poly objects [and return non-poly on error]
// IsPoly() returns true if object is a "good" polynomial, e.g.,
// Poly().Tail().IsPoly() == false
//
// accessors include
// leadingCoeff(), degree() for first term
// at(double x) to evaulate a polynomial at x
// tostring() -- standard helper function
//

class Poly
{
  public:
    Poly();
    Poly(double coeff, int exp);
    
    const Poly& operator += (const Poly& rhs);
    const Poly& operator *= (double c);
    
    string tostring()      const;
    double at(double x)    const;
    int    degree()        const;
    double leadingCoeff()  const;
    
    Poly Tail()            const;
    Poly Head()            const;
    bool IsPoly()          const;
    
    static Poly ZERO;
    static int TermsAllocated();
    
  private:
    struct Pair         // this is the (a,b) in ax^b
    {   double coeff;
        int expo;
        Pair()
          : coeff(0.0),expo(0) { }
        Pair(double c, int e) : coeff(c), expo(e) { }
    };
    typedef CList<Pair> Polist; 
    typedef CListIterator<Pair> PolistIterator;
    static bool ourInitialized;
    
    Poly(Polist p);     // make poly from list of terms, helper
    Polist myPoly;      // the list of terms
};

Poly operator + (const Poly& lhs, const Poly& rhs);
Poly operator * (double c, const Poly& p);
Poly operator * (const Poly& p, double c);
ostream& operator << (ostream& out, const Poly& p);
#endif

⌨️ 快捷键说明

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