实现多项式的基本运算

#include<iostream>
#include<stdlib.h>
using namespace std;
class LinkNode
{
    public:
        int data;
        int exp;
        LinkNode *link;
        LinkNode() {}//
        LinkNode(int x,int y)
        {
            data = x;
            exp = y;
            link = NULL;
        }
    };
class List
{
public:
    friend LinkNode;
    LinkNode * first;
    List ()
    {
        first = new LinkNode(0,-1);
    }
    ~List ()
    {
        delete first;
    }
    void lian(int &x,int &y);
    void output1(int i);
    void output2();
    void paixu();
    void jiafa(List &a);
    void qiudao();
    void qiuzhi(int);
};

void List::lian(int &x, int &y)//xishu and zhishu
{
    LinkNode *current=first;
    while(current->link!=NULL)
    {
        current=current->link;
    }
    LinkNode *a = new LinkNode(x,y);
    current->link=a;
}

void List::paixu()//系数从小到大排序
{
    int l = 0;
    LinkNode *current =first;
    while(current->link!=NULL)
    {
        l++;
        current=current->link;
    }
    int i = 0;
    LinkNode *p =first;
    LinkNode *q;
    for(i=0; i<l; i++)
    {
        p=first;
        q=NULL;
        while(p->link->link!=NULL)
        {
            if(p->link->exp > p->link->link->exp)
            {
                q=p->link;
                p->link=q->link;
                q->link=p->link->link;
                p->link->link=q;
            }
            p=p->link;
        }
    }
}

void List ::jiafa(List  &b)//多项式假发
{
    List  c;
    LinkNode *k = first->link;
    while (k != NULL)
    {
        c.lian(k->data, k->exp);
        k = k->link;
    }
    int flag = 1;
    LinkNode *q = c.first->link;
    LinkNode *p = b.first->link;
    while (p != NULL)
    {
        q = c.first->link;
        flag = 1;
        while (q != NULL)
        {
            if (p->exp == q->exp)
            {
                q->data = q->data + p->data;
                flag = 0;
            }
            q = q->link;
        }
        if (flag)
        {
            c.lian(p->data, p->exp);
        }
        p = p->link;
    }
    c.paixu();
    c.output2();
}

void List::qiudao()//多项式求导,exp-1乘data
{
    List a;
    LinkNode *k =first->link;
    while(k!=NULL)
    {
        a.lian(k->data,k->exp);
        k=k->link;
    }
    k=a.first->link;
    while(k!=NULL)
    {
        k->data=(k->data)*(k->exp);
        k->exp=k->exp-1;
        k=k->link;
    }
    a.output2();
}


void List::qiuzhi(int x)
{
    int s = 0;
    int q = 1;
    LinkNode *p =first->link;
    while(p!= NULL)
    {
        q=1;
        for (int i=0; i<p->exp; i++)
        {
            q=q*x;
        }
        s=s+p->data*q;
        p=p->link;
    }
    cout<<s<<endl;
}

void List::output1(int i)
{
    if(first->link==NULL)
    {
        cerr<<"该链表为空!!!!!"<<endl;
        exit(1);
    }
    LinkNode *current = first->link;
    for(int k=1; k <i; k++)
    {
        if(current->link == NULL)
        {
            cerr<<"是无效的位置!!!!!!!!!!!"<<endl;
            exit(1);
        }
        else
        {
            current=current->link;
        }
    }


    cout<<current->data<<endl;
}

void List::output2()
{
    if(first->link == NULL)
    {
        cerr<<"链表为空!!!!!!"<<endl;
        exit(1);
    }
    LinkNode *current =first->link;
    int i = 1;
    while(current != NULL)
    {
        if(current->data==0)
        {
            current=current->link;
            continue;
        }


        if (i != 1)
        {
            if (current->data == 1)
            {
                cout << "+";
            }
            else if (current->data > 1)
            {
                cout << "+" << current->data;
            }
            else{
                cout << current->data;
            }
        }
        else
        {
            if (current->data == 1) {  }
            else{
                cout << current->data;
            }
        }


        if (current->exp != 1)
        {
            cout << "x^";
            cout << current->exp;
        }
        else
        {
            cout << "x";
        }
        i++;
        current = current->link;
    }
    cout<<endl;
}
int main()
{
    int i,a,b,e,f,g;
    List la;
    List lb;
    cin>>a;
    for (i=0;i<a;i++)
    {
        cin>>e>>f;
        la.lian(e, f);
    }
    cin>>b;
    for(i=0;i<b;i++)
    {
        cin>>e>>f;
        lb.lian(e,f);
    }
    cin>>g;
    cout<<"A(x)+B(x)=";
    la.jiafa(lb);
    cout<<"A'(x)=";
    la.qiudao();
    cout<<"A("<<g<<")=";
    la.qiuzhi(g);
    return 0;
}
阅读更多

没有更多推荐了,返回首页