zoukankan      html  css  js  c++  java
  • 链表实现多项式相加和相乘

    【Polynominal.h】:

    #include<iostream>
    using namespace std;
    class Polynominal;
    class Term
    {
    public:
        Term(int c, int e);
        Term(int c, int e, Term* next);
        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* next) : coef(c), exp(e)
    {
        link = next;
    }
    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 Output(ostream& out)const;
        void AddTerms(istream& in);
        void PolyAdd(Polynominal& r);
        void PolyMult(Polynominal& r);
    private:
        Term* theList;
        friend ostream & operator <<(ostream &, const Polynominal &);
        friend istream & operator >>(istream &, Polynominal &);
        friend Polynominal& operator +(Polynominal &, 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 c, e;
        for (;;)
        {
            cout << "Input a term(coef,exp):
    " << 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:
    " << endl;
        for (; p != theList; p = p->link)
        {
            if (!first && (p->coef > 0))
                out << "+";
            first = 0;
            out << *p;
        }
        cout << "
    " << 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;
                }
                else
                {
                    q1 = q; q = q->link;
                }
            }
            else
                q1 = q1->InsertAfter(p->coef, p->exp); p = p->link;
        }
    }
    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);
        return a;
    }
    void Polynominal::PolyMult(Polynominal &r)
    {
        Term* q, *q1=theList, *p,*mult;
        p = r.theList->link;
        q = q1->link;
        Polynominal temp;
        int c = 0, e = 0;
        mult = temp.theList->link;
        for (q = r.theList->link; p->exp >= 0; p = p->link)
        {
            for (; q->exp >= 0; q = q->link)
            {
                c = p->coef*q->coef;
                e = p->exp*q->exp;
                mult=mult->InsertAfter(c, e);
            }
        }
        for (p = temp.theList->link,q=this->theList->link; p->exp >= 0; p = p->link)
        {
            if (q->exp >= 0)
            {
                q->coef = q->coef;
                q->exp = p->exp;
                q = q->link;
            }
            else
            {
                c = p->coef;
                e = p->exp;
                q->InsertAfter(c, e);
            }
        }
    }
    Polynominal& operator *(Polynominal &a, Polynominal &b)
    {
        a.PolyMult(b);
        return a;
    }

    【polyominal.cpp】:

     1 #include"polynominal.h"
     2 void main()
     3 {
     4     Polynominal p, q;
     5     cin >> p;
     6     cout << p;
     7     cin >> q;
     8     cout << q;
     9     q = q + p;
    10     cout << q;
    11     q = q*p;
    12     cout << q;
    13     system("pause");
    14 }
  • 相关阅读:
    args 、kwargs不定参数通过列表、元组、字典传递
    内置函数_eval
    python模块之beautifulSoup
    修改jupyter notebook的默认浏览器
    jupyter notebook自动补全功能实现
    在资源管理器中添加一个共享的网络位置
    在word2010中添加带滚动条的文本框
    Outlook 2010中263邮箱客户端设置
    跳跃游戏
    螺旋矩阵
  • 原文地址:https://www.cnblogs.com/zlgxzswjy/p/4804966.html
Copyright © 2011-2022 走看看