数据结构之多项式(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;
}

      测试结果:



 

 

 

 

 

 

 

 

 

 

 

符号多项式的操作,已经成为表处理的典型用例。在数学上,一个一元多项式Pn(x)可按升幂写 成: Pn(x) = p0+ p1x+ p2x2+….+ pnxn 它由n+1个系数唯一确定,因此,在计算机里,它可用一个线 性表P来表示: P = (p0 ,p1 ,p2 ,… pn)每一项的指数i隐含在其系数pi的序号里。 假设Qm(x)是一元m次多项式,同样可用线性表Q来表示:Q = (q0 ,q1 ,q2 ,… qm)。 不失一般性,设m<n,则两个多项式相加的结果 Rn(x) = Pn(x)+Qm(x)可用线性表R表示:R = (p0+q0 , p1+q1 , p2 +q2 , … , pm +qm , pm+1 ,… pn)。显然,我们可以对P、Q和R采用顺序存储结构, 使得多项式相加的算法定义十分简洁。至此,一元多项式的表示及相加问题似乎已经解决了。 然而在通常的应用中,多项式的次数可能很高且变化很大,使得顺序存储结构的最大长度很难 决定。特别是在处理形如:S(x) = 1+3x10000+2x20000的多项式时,就要用一长度为20001的线性表来 表示,表中仅有三个非零元素,这种对内存空间的浪费是应当避免的,但是如果只存储非零系数项 则显然必须同时存储相应的指数。 一般情况下的一元n次多项式可写成: Pn(x) = p1x e1 + p2x e2 + … + pmx em 其中 pi,是指数为 ei 的项的非零系数,且满足 0 ≤ e1 < e2 < …< em = n,若用一个长度为 m 且 每个元素有两个数据项(系数项和指数项)的线性表便可唯一确定多项式 Pn(x)。 ((p1 ,e1) , (p2 ,e2) , … ,(pm,em)) 在最坏情况下,n+1(=m)个系数都不为零,则比只存储每项系数的方案要多存储一倍的数据。但 是,对于 S(x)类的多项式,这种表示将大大节省空间。 本题要求选用线性表的一种合适的存储结构来表示一个一元多项式,并在此结构上实现一元多 项式的加法,减法和乘法操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值