多项式的构造和加减

场景说明:在这里实现了多项式的的构建,例如2x(2)+4x(4),因为水平有限,因此使用的括号代表的是指数。最有意思的应该是在一个完整的多项式中插入一个多项式,其中有相同指数的合并,还有插入。通过分类讨论:

1)没有节点,直接添加

2)只有一个节点,合并或者在前驱插入,或者在后继添加

3)两个节点的情况下,使用两两组合的方式,查找合适的位置。

typedef struct SPolynomial
{
    float coef;
    int expn;
    SPolynomial* pNextSPolynomial;
}SPolynomial;
class CPolyn
{
private:
    SPolynomial* m_pSPolynomialHeader;
public:
    CPolyn()
    {
        m_pSPolynomialHeader=NULL;
    }
    ~CPolyn()
    {
        if(NULL==m_pSPolynomialHeader) return;
        SPolynomial* pTmp=m_pSPolynomialHeader;
        while(NULL!=pTmp)
        {
            free(pTmp);
            m_pSPolynomialHeader=pTmp->pNextSPolynomial;
        }
    }
    int AddPolyn(float coef,int expn)
    {
        //2.create insert node
        SPolynomial* p=new SPolynomial;
        //SPolynomial* p=(SPolynomial*)malloc(sizeof(SPolynomial));
        p->coef=coef;
        p->expn=expn;
        p->pNextSPolynomial=NULL;
        //3.if polynheader is null,add and return
        if(NULL==m_pSPolynomialHeader)
        {
            m_pSPolynomialHeader=p;
            return 0;
        }
        //4.if polynheader has only one node
        if(m_pSPolynomialHeader->expn>p->expn)
        {
            p->pNextSPolynomial=m_pSPolynomialHeader;
            m_pSPolynomialHeader=p;
            return 0;
        }
        if(m_pSPolynomialHeader->expn==p->expn)
        {
            m_pSPolynomialHeader->coef+=p->coef;
            delete p;
            return 0;
        }
        //1.assing polynheader to tmp
        SPolynomial* pTmpSPolynomial=m_pSPolynomialHeader;
        //4.if polynheader only have two node,if a1.expn<p->expn<a2.expn,insert between
        if(NULL!=m_pSPolynomialHeader->pNextSPolynomial)
        {
            if((m_pSPolynomialHeader->expn<p->expn)&&(p->expn<m_pSPolynomialHeader->pNextSPolynomial->expn))
            {
                p->pNextSPolynomial=m_pSPolynomialHeader->pNextSPolynomial;
                m_pSPolynomialHeader->pNextSPolynomial=p;
                return 0;
            }
        }
        //5 transever all node
        while((NULL!=pTmpSPolynomial->pNextSPolynomial))
        {
            if(pTmpSPolynomial->expn==p->expn)
            {
                pTmpSPolynomial->coef=pTmpSPolynomial->coef+p->coef;
                delete p;
                return 0;
            }
            if(pTmpSPolynomial->pNextSPolynomial->expn==p->expn)
            {
                pTmpSPolynomial->pNextSPolynomial->coef+=p->coef;
                delete p;
                return 0;
            }
            if((pTmpSPolynomial->expn<p->expn)&&(pTmpSPolynomial->pNextSPolynomial->expn<p->expn))
            {
                pTmpSPolynomial=pTmpSPolynomial->pNextSPolynomial;
            }
            else if((pTmpSPolynomial->expn<p->expn)&&(pTmpSPolynomial->pNextSPolynomial->expn>p->expn))
            {
                p->pNextSPolynomial=pTmpSPolynomial->pNextSPolynomial;
                pTmpSPolynomial->pNextSPolynomial=p;
                return 0;
            }
        }
        pTmpSPolynomial->pNextSPolynomial=p;
        return 0;
    }
    void PrintPolyn()
    {
        SPolynomial* pTmp=m_pSPolynomialHeader;
        while(NULL!=pTmp)
        {
            cout<<"coef:"<<pTmp->coef<<"  "<<"expn:"<<pTmp->expn<<endl;
            pTmp=pTmp->pNextSPolynomial;
        }
    }
    int PolyLength()
    {
        SPolynomial* pTmp=m_pSPolynomialHeader;
        int nLen=0;
        while(NULL!=pTmp)
        {
            nLen++;
            pTmp=pTmp->pNextSPolynomial;
        }
        return nLen;
    }
    int AddOnePolynToPoly(CPolyn* p)
    {
        //of course we can use addPolyn function to finish it but not effective
        //first step we should sketch one SPolynomial coef and expn and then use
        //AddPolyn(coef,expn)finish it ,however every time we should traserval every link
        //list
        float coef=0;
        int expn=0;
        while((-1)!=p->PopPolyn(coef,expn))
        {
            AddPolyn(coef,expn);
        }
        return 0;
    }
    int PopPolyn(float& coef,int& expn)
    {
        if(NULL==m_pSPolynomialHeader) return -1;
        coef=m_pSPolynomialHeader->coef;
        expn=m_pSPolynomialHeader->expn;
        SPolynomial* pTmp=m_pSPolynomialHeader;
        m_pSPolynomialHeader=pTmp->pNextSPolynomial;
        delete  pTmp;
        return 0;
    }
};
int main(int argc, char *argv [])
{
    QCoreApplication a(argc, argv);
    {
    CPolyn b;
    b.AddPolyn(1,2);
    b.AddPolyn(3,3);
    b.AddPolyn(2,4);
    b.AddPolyn(4,4);
    b.AddPolyn(5,6);
    b.AddPolyn(6,5);
    b.AddPolyn(6,5);
    b.AddPolyn(7,7);
    int size=b.PolyLength();
    cout<<"size="<<size<<endl;
    b.PrintPolyn();
    cout<<"Finish it"<<endl;
    }
    return a.exec();
}

你可能感兴趣的:(多项式)