数据结构之多项式(C++)

      数据结构多项式,运用到类的复制构造函数,静态成员等基础知识,简单实现了加法和乘法运算,但是对乘法(复杂度比较高)暂时做这样处理。

      对于多项式的构成这些不做多余介绍。本代码纯手工制作,难免有不足之处。

       头文件: Polynomial .h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
#include <iostream>
using namespace std;
class Polynomial; // forward declaration

class Term
{
    friend Polynomial;
private:
    float coef;   // coefficient
    int   exp;    // exponent
public:
    int getExp() const {return exp;}
    float getCoef() const { return coef; }
    void setExp(int nExp) { exp = nExp; }
    void setCoef(float fCoeff) { coef = fCoeff; }
};
class Polynomial
{
private:
    Term * termArray;    // array of nonzero terms
    int    capacity;     // size of termArray
    int    terms;        // number of nonzero terms
    static int free;     // After adding the terms
    static int multfree; // After Multing the terms
public:
    Polynomial(int initValue = 1)
        : terms(initValue), capacity(initValue)
    {
        termArray = new Term[capacity];
        if (terms >= capacity)
        {
            // double capacity of termArray
            capacity *= 2;
            Term * temp = new Term[capacity]; // new array
            copy(termArray, termArray + terms, temp);
            delete[] termArray;
            termArray = temp;
        }
        memset(termArray, 0, sizeof(Term) * capacity);
    }

    Polynomial(const Polynomial& rhs)
        : termArray(NULL)
    {
        operator= (rhs);
    }

    Polynomial& testData(bool isRight);

    const Polynomial& operator= (const Polynomial& rhs);
    ~Polynomial() { delete[] termArray; }
    Polynomial Add(Polynomial &b); // 多项式加法
    Polynomial Mult(const Polynomial &rhs);  // 多项式乘法
  //  Polynomial Merge(Polynomial &rhs);       // 多项式每项相加表达式
    void Remove(int nIndex); 
    friend ostream & operator<< (ostream & out, const Polynomial& rhs);
    friend istream & operator>> (istream & ins,  Polynomial& rhs);
};

#endif


 

       源文件: Polynomial.cpp

#include "Polynomial.h"
#include <assert.h>

const Polynomial& Polynomial::operator= (const Polynomial& rhs)
{
    if (this != &rhs) // alias test
    {
        delete[] termArray;
        terms = rhs.terms;
        capacity = rhs.capacity;
        termArray = new Term[capacity];
        for (int k = 0; k < terms; ++k)
            termArray[k] = rhs.termArray[k];
    }
    return *this;
}

Polynomial Polynomial::Add(Polynomial &rhs)
{
    Polynomial c(terms + rhs.terms);
    int apos = 0;
    int bpos = 0;
    while (apos < terms || bpos < rhs.terms)
    {
        if (termArray[apos].exp == rhs.termArray[bpos].exp)
        {
            float tmp = termArray[apos].coef + rhs.termArray[bpos].coef;
            if (tmp) 
            {
                c.termArray[free].exp  = termArray[apos].exp;
                c.termArray[free].coef = tmp;
                ++apos;
                ++bpos;
            }
        }
        else if (termArray[apos].exp > rhs.termArray[bpos].exp)
        {
            c.termArray[free].exp  = termArray[apos].exp;
            c.termArray[free].coef = termArray[apos].coef;
            ++apos;
        }
        else
        {
            c.termArray[free].exp  = rhs.termArray[bpos].exp;
            c.termArray[free].coef = rhs.termArray[bpos].coef;
            ++bpos;
        }
        ++free;
    }
    return c;
}

void Polynomial::Remove(int nIndex) 
{        
	assert(nIndex >= 0 && nIndex < terms);      
	// First create a new array one element smaller than the old array     
	Term *pnData = new Term[terms - 1];      
	// Copy all of the elements up to the index     
	for (int nBefore=0; nBefore < nIndex; ++nBefore)        
	    pnData[nBefore] = termArray[nBefore];      
	// Copy all of the values after the inserted element     
	for (int nAfter=nIndex+1; nAfter < terms; ++nAfter)        
	   pnData[nAfter-1] = termArray[nAfter];      
	// Finally, delete the old array, and use the new array instead    
	delete[] termArray;    
	termArray = pnData;    
	terms -= 1;
}  

Polynomial Polynomial::Mult(const Polynomial &rhs)
{
    Polynomial cTemp(terms * rhs.terms);
	int theExp = 0;          // temp argument for store exp
	float theCoeff = 0.0f;
	free = 0;
	int nIndex = 0;
    for (int i = 0; i < terms; ++i)
    {
        Polynomial cTempRhs(rhs);
        for (int j = 0; j < rhs.terms; ++j)
        { 
		theExp   = termArray[i].exp + rhs.termArray[j].exp;
		theCoeff = termArray[i].coef * rhs.termArray[j].coef;
		cTemp.termArray[free].exp  = theExp;
		cTemp.termArray[free].coef = theCoeff;
		++nIndex;
		++free;
        }

    }

	Term* pTerm = new Term[nIndex];
	// check the Polynomial's exp is equal
	for (int k = 0; k < nIndex; ++k)
	{
		int temp = cTemp.termArray[k].exp;
		for (int n = k + 1; n <  nIndex; ++n)
		{
 			if (temp == cTemp.termArray[n].exp)
 			{
 				// the coefficient merge when exp is equal
				cTemp.termArray[k].coef += cTemp.termArray[n].coef;
 				// delete the same term, keep the front
 				cTemp.Remove(n);
 				if (free)
 				--free;
 			}
		}

	}
	cTemp.terms = free;
    return cTemp;
}


Polynomial& Polynomial::testData(bool isRight)
{
    if (isRight) // data 1
    {
        termArray[0].exp = 1000;  termArray[0].coef = 3.14f;
        termArray[1].exp = 200;   termArray[1].coef = 6.28f;
        termArray[2].exp = 100;   termArray[2].coef = 9.42f;
        termArray[3].exp = 50;    termArray[3].coef = 12.56f;
    }
    else
    {
        termArray[0].exp = 900;  termArray[0].coef = 3.14f;
        termArray[1].exp = 200;   termArray[1].coef = 6.28f;
        termArray[2].exp = 120;   termArray[2].coef = 9.42f;
        termArray[3].exp = 50;    termArray[3].coef = 12.56f;
    }
    return *this;
}

ostream & operator<< (ostream & out, const Polynomial& rhs)
{
    for (int k = 0; k < rhs.free; ++k)
	out << "theExp: " << rhs.termArray[k].getExp()
	<< "\t" << "theCoeff: " << rhs.termArray[k].getCoef() << endl;
    out << endl;
    return out;
}

istream & operator>> (istream & ins,  Polynomial& rhs)
{
    int   tmpExp   = 0;
    float tmpCoeff = 0.0f;
    for (int k = 0; k < rhs.terms; ++k)
    {    
        ins >> tmpExp >> tmpCoeff;
        rhs.termArray[k].setExp(tmpExp);
        rhs.termArray[k].setCoef(tmpCoeff);
    }
    return ins;
}
int Polynomial::free = 0;
/*int Polynomial::multfree = 0;*/


 

 

     

       测试文件: testmain.cpp 

#include "Polynomial.h"

int main()
{
    Polynomial tmp(4);
    Polynomial x1 = tmp.testData(true);

    Polynomial x2 = tmp.testData(false);
    std::cout << x2.Add(x1);
    std::cout << x2.Mult(x1);
    std::cout << std::endl;
    return 0;
}

      测试结果:

数据结构之多项式(C++)_第1张图片


 

 

 

 

 

 

 

 

 

 

 

你可能感兴趣的:(数据结构之多项式(C++))