实现多项式的基本运算

#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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值