| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1024 人关注过本帖
标题:请问大家如何用链表实现多项式的乘法?
只看楼主 加入收藏
张小旭
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-11-4
结帖率:0
收藏
已结贴  问题点数:20 回复次数:5 
请问大家如何用链表实现多项式的乘法?
试了很多天了,都不行。。
搜索更多相关主题的帖子: 乘法 链表 多项式 
2009-11-12 12:15
l01w29
Rank: 2
等 级:论坛游民
帖 子:4
专家分:27
注 册:2009-11-7
收藏
得分:6 
你的链表的结点是什么样的?换句话说你的多项式存储格式是怎样的?
2009-11-12 13:44
张小旭
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-11-4
收藏
得分:0 
回复 2楼 l01w29
#include<iostream.h>

class Term
{
public:
    Term(int c,int e);
    Term(int c,int e,Term *nxt);
    Term* InsertAfter(int c,int e);
private:
    int coef;
    int exp;
    Term *link;
    friend ostream &operator<<(ostream &,const Term &);
    friend class Polynominal;
};
Term::Term(int c,int e):coef(c),exp(e)
{
    link=0;
}
Term::Term(int c,int e,Term *nxt):coef(c),exp(e)
{
    link=nxt;
}
Term* Term::InsertAfter(int c,int e)
{
    link=new Term(c,e,link);
    return link;
}
ostream &operator<<(ostream &out,const Term &val)
{
    if(val.coef==0)return out;
    out<<val.coef;
    switch(val.exp)
    {
    case 0:break;
    case 1:out<<"X";break;
    default:out<<"X^"<<val.exp;break;
    }
    return out;
}
class Polynominal
{
public:
    Polynominal();
    ~Polynominal();
    void AddTerms(istream& in);
    void Output(ostream& out)const;
    void PolyAdd(Polynominal& r);
    void PolyMultiply(Polynominal& r);
private:
    Term* theList;
    friend ostream& operator<<(ostream&,const Polynominal&);
    friend istream& operator>>(istream&,Polynominal&);
    friend Polynominal& operator +(Polynominal&,Polynominal&);
};
Polynominal::Polynominal()
{
    theList=new Term(0,-1);
    theList->link=theList;
}
Polynominal::~Polynominal()
{
    Term* p=theList->link;
    while(p!=theList)
    {
        theList->link=p->link;
        delete p;
        p=theList->link;
    }
    delete theList;
}
void Polynominal::AddTerms(istream &in)
{
    Term* q=theList;
    int e,c;
    for(;;)
    {
        cout<<"Input a term(ceof,exp):\n"<<endl;
        cin>>c>>e;
        if(e<0) break;
        q=q->InsertAfter(c,e);
    }
}
void Polynominal::Output(ostream& out)const
{
    int first=1;Term *p=theList->link;
    cout<<"The polynominal is:\n"<<endl;
    for(;p!=theList;p=p->link)
    {
        if(!first&&(p->coef>0)) out<<"+";
        first=0;
        out<<*p;
    }
    cout<<"\n"<<endl;
}
void Polynominal::PolyAdd(Polynominal& r)
{
    Term* q,*q1=theList,*p;
    p=r.theList->link;
    q=q1->link;
    while(p->exp=0)
    {
        while(p->exp<q->exp)
        {
            q1=q;
            q=q->link;
        }
        if(p->exp==q->exp)
        {
            q->coef=q->coef+p->coef;
            if(q->coef==0)
            {
                q1->link=q->link;
                delete(q);
                q=q1->link;
//                p=p->link;
            }
            else
            {
                q1=q;
                q=q->link;
//                p=p->link;
            }
        }
        else
            q1=q1->InsertAfter(p->coef,p->exp);
        p=p->link;
    }
}
void Polynominal::PolyMultiply(Polynominal& r)
{
    Term *q,*p,*q2,*p1,*p2;
    int coef,exp;
    p=r.theList->link;
    q=theList->link;
    p1=new Term(0,-1);
    p1->link=p1;
    p2=p1;
    while(q->exp>=0)
    {
        p=r.theList->link;
        while(p->exp>=0)
        {
            coef=q->coef*p->coef;
            exp=q->exp+p->exp;
            for(p2=p1->link;p2->exp>=0&&p2->exp!=exp;p2=p2->link);
            if(p2->exp==-1)
                p2->InsertAfter(coef,exp);
            else
            {
                p2->coef=p2->coef+coef;
                if(p2->coef==0)
                {
                    q2=p2->link;
                    p2->link=q2->link;
                    delete(q2);
                }
            }
            p=p->link;
        }
        q=q->link;
    }
    theList=p1;
}
ostream& operator<<(ostream &out,const Polynominal &x)
{
    x.Output(out);
    return out;
}
istream& operator>>(istream& in,Polynominal &x)
{
    x.AddTerms(in);
    return in;
}
Polynominal& operator+(Polynominal &a,Polynominal &b)
{
    a.PolyAdd(b);
    cout<<"after add"<<endl;
    return a;
}
Polynominal& operator*(Polynominal &a,Polynominal &b)
{
    a.PolyMultiply(b);
    cout<<"after mul"<<endl;
    return a;
}


int main()
{
    Polynominal p,q;
    cin>>p;cout<<p;
    cin>>q;cout<<q;
    q=p+q;cout<<q;
    q=p*q;cout<<q;
    return 0;
}



这是代码,可是执行时加法时而会出错。


2009-11-12 19:38
张小旭
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2009-11-4
收藏
得分:0 
呵呵,我做出来啦。哈哈~~
2009-11-12 20:09
flyingcloude
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
威 望:6
帖 子:598
专家分:1512
注 册:2008-1-13
收藏
得分:6 
回复 4楼 张小旭
呵呵,还是希望自己动动练习,在练习中才能不断提高。

你能学会你想学会的任何东西,这不是你能不能学会的问题,而是你想不想学的问题
2009-11-13 12:38
我爱好
Rank: 1
等 级:新手上路
帖 子:5
专家分:7
注 册:2009-10-25
收藏
得分:6 
把你做的发出来看看哈~
2009-11-13 22:09
快速回复:请问大家如何用链表实现多项式的乘法?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014079 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved