多项式加法和乘法(链表实现)

数据结构树上的代码~~练习留着以后复习

//Polynomial.h

#include <iostream>
using namespace std;

struct PNode
{
    int di;
    int ci;
    PNode *next;
    PNode(PNode *n=NULL){next=n;}
    PNode(int& d,int& c,PNode *n=NULL)
    {
        di=d;
        ci=c;
        next=n;
    }
};
class Polynomial{
protected:
    PNode *first;
public:
    Polynomial()  {first=new PNode;}
    Polynomial(int& d,int & c) 
    {
        first=new PNode(d,c);
    }  
    bool insert();                        //插入
    PNode* gethead()
    {
        return first;
    }   
    Polynomial(Polynomial &P1,Polynomial &P2);   //多项式1 2相加放入3中
    int getmax();              //返回最大阶数
    friend Polynomial operator +(Polynomial &P1,Polynomial &P2);        //多项式加法
    friend Polynomial operator *(Polynomial &P1,Polynomial &P2);        //多项式乘法
    friend ostream& operator<<(ostream & out,Polynomial &P)            //输出
    {
        PNode *p=P.gethead()->next;
        while(p->next!=NULL)
        {
            if (p->ci==0)
            {
                out<<"x+";
            }
            else if(p->ci==1)
            {
                out<<p->di<<"x+";
            }
            else
                out<<p->di<<"x^"<<p->ci<<"+";
            p=p->next;
        }
        if (p->next==NULL)
        {
            if (p->ci==0)
            {
                out<<"1"<<endl;
            }
            else if(p->ci==1)
            {
                out<<p->di<<"x"<<endl;
            }
            else
                out<<p->di<<"x^"<<p->ci<<endl;
            p=p->next;
        }
        return out;
    }   
};

bool Polynomial::insert()
{
    PNode *p=gethead();
    while(p->next=NULL)
        p=p->next;
    cout<<"依次输入底数和指数(以底数0结束)"<<endl;
    int d,c;
    cout<<"d=";
    cin>>d;
    cout<<"c=";
    cin>>c;
    while (d!=0)
    {
        PNode *q=new PNode(d,c);
        p->next=q;
        p=q;
        cout<<"d=";
        cin>>d;
        cout<<"c=";
        cin>>c;
    }
    return true;
};

int Polynomial::getmax()
{
    int i=0;
    PNode *p=gethead()->next;
    while (p!=NULL)
    {
        if (p->ci>i)
        {
            i=p->ci;
        }
        p=p->next;
    }
    return i;
};

Polynomial::Polynomial(Polynomial &P1,Polynomial &P2)
{
    int i=P1.getmax();
    if(P2.getmax()>i)
        i=P2.getmax();
    int *num=new int[i+1];
    for (int j=0;j<=i;j++)
    {
        num[j]=0;
    }
    PNode *p=P1.gethead()->next;
    while (p!=NULL)
    {
        num[p->ci]=num[p->ci]+p->di;
        p=p->next;
    }
    p=P2.gethead()->next;
    while (p!=NULL)
    {
        num[p->ci]+=p->di;
        p=p->next;
    }
    p=first=new PNode;
    for (int j=0;j<=i;j++)
    {
        if (num[j]==0)
            continue;
        PNode *q=new PNode(num[j],j);
        p->next=q;
        p=q;
    }
    p->next=NULL;
};

Polynomial operator+(Polynomial &P1,Polynomial &P2)
{
    int i=P1.getmax();
    if(P2.getmax()>i)
        i=P2.getmax();
    int *num=new int[i+1];
    for (int j=0;j<=i;j++)
    {
        num[j]=0;
    }
    PNode *p=P1.gethead()->next;
    while (p!=NULL)
    {
        num[p->ci]=num[p->ci]+p->di;
        p=p->next;
    }
    p=P2.gethead()->next;
    while (p!=NULL)
    {
        num[p->ci]+=p->di;
        p=p->next;
    }
    Polynomial P3;
    p=P3.gethead();
    for (int j=0;j<=i;j++)
    {
        if (num[j]==0)
            continue;
        PNode *q=new PNode(num[j],j);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return P3;
};

Polynomial operator*(Polynomial &P1,Polynomial &P2)
{
    int i=P1.getmax();
    int j=P2.getmax();
    i=i*j;
    int *num=new int[i+1];
    for (j=0;j<=i;j++)
    {
        num[j]=0;
    }
    PNode *p=P1.gethead()->next;
    PNode *q=P2.gethead()->next;
    while(p!=NULL)
    {
        while(q!=NULL)
        {
            int m=p->ci+q->ci;
            int n=(p->di)*(q->di);
            num[m]=num[m]+n;
            q=q->next;
        }
        p=p->next;
        q=P2.gethead()->next;
    }
    Polynomial P3;
    p=P3.gethead();
    for (int j=0;j<=i;j++)
    {
        if (num[j]==0)
            continue;
        PNode *q=new PNode(num[j],j);
        p->next=q;
        p=q;
    }
    p->next=NULL;
    return P3;
}



//main.cpp

#include <iostream>
#include "Polynomial.h"
using namespace std;

int main()
{
    Polynomial poly1;
    poly1.insert();
    PNode *p=poly1.gethead();
    if(p->next==NULL)
    {
        cout<<"多项式为空"<<endl;
        return -1;
    }
    cout<<poly1<<endl;

    Polynomial poly2;
    poly2.insert();
    p=poly2.gethead();
    if(p->next==NULL)
    {
        cout<<"多项式为空"<<endl;
        return -1;
    }
    cout<<poly2<<endl;
//    Polynomial poly3(poly1,poly2);   //多项式加法的一种
//    cout<<poly3;
    cout<<"多项式加法"<<endl;
    Polynomial poly3;
    poly3=poly1+poly2;
    cout<<poly3<<endl;
    cout<<"多项式乘法"<<endl;
    Polynomial poly4;
    poly4=poly1*poly2;
    cout<<poly4<<endl;
    return 0;
}
View Code

 

posted @ 2013-11-16 23:57  兔子爱吃小白菜  阅读(407)  评论(0编辑  收藏  举报