天天看點

連結清單實作多項式的加減乘求導

#include <iostream>
#include <math.h>
#include <stdlib.h>
using namespace std;
struct data
{
    double xiShu;
    int zhiShu;
    data *next;
};
class List
{
    data *head;
public:
    List()
    {
        head = new data;
        head->next=NULL;
    }
    data *getHead()
    {
        return head;
    }
    void createList()
    {
        cout<<"請輸入多項式的項數"<<endl;
        int n;
        cin>>n;
        for(int i = 0; i<n; i++)
        {
            data *a = new data;
            cout<<"請依次輸入多項式的系數,指數"<<endl;
            cin>>a->xiShu>>a->zhiShu;
            a->next = head->next;
            head->next = a;
        }
    }
    void sortData()
    {
        if(head->next==NULL)
        {
            cout<<"多項式為空"<<endl;
            return;
        }
        if(head->next->next==NULL)
        {
            cout<<"排序成功"<<endl;
            return;
        }
        data *a;
        data *b;
        for(a = head->next; a!=NULL; a = a->next)
        {
            for(b = a->next; b!=NULL; b = b->next)
            {
                if(a->zhiShu < b->zhiShu)
                {
                    int zhiShu,xiShu;
                    zhiShu = a->zhiShu;
                    a->zhiShu = b->zhiShu;
                    b->zhiShu = zhiShu;
                    xiShu = a->xiShu;
                    a->xiShu = b->xiShu;
                    b->xiShu = xiShu;
                }
            }
        }

    }
    void showData()
    {
        if(head->next==NULL)
        {
            cout<<"多項式為空"<<endl;
            return;
        }
        data *temp;
        temp = head->next;
        cout<<temp->xiShu<<'X'<<temp->zhiShu;
        temp = temp->next;
        while(temp!=NULL)
        {
            if(temp->xiShu>0)cout<<'+'<<temp->xiShu<<'X'<<temp->zhiShu;
            else if(temp->xiShu<0)cout<<temp->xiShu<<'X'<<temp->zhiShu;
            else ;
            temp = temp->next;
        }
        cout<<endl;
    }
    void add(data *head1)
    {
        List *c = new List;
        data *temp = c->getHead();
        data *temp1 = head->next;
        data *temp2 = head1->next;
        while(temp1!=NULL && temp2!=NULL)
        {
            data *temp3 = new data;
            if(temp1->zhiShu>temp2->zhiShu)
            {
                temp3->xiShu = temp1->xiShu;
                temp3->zhiShu = temp1->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp1 = temp1->next;
            }
            else if(temp1->zhiShu <temp2->zhiShu)
            {
                temp3->xiShu = temp2->xiShu;
                temp3->zhiShu = temp2->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp2 = temp2->next;
            }
            else
            {
                temp3->xiShu = temp1->xiShu + temp2->xiShu;
                temp3->zhiShu = temp1->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp1 = temp1->next;
                temp2 = temp2->next;
            }
        }
        if(temp1!=NULL)
        {
            temp->next = temp1;
        }
        else
        {
            temp->next = temp2;
        }
        c->showData();
    }
    void evaluate()
    {
        if(head->next==NULL)
        {
            cout<<"這是空連結清單"<<endl;
        }
        else
        {
            cout<<"請輸入X的值"<<endl;
            double x,sum=0;
            cin>>x;
            data *temp = head->next;
            while(temp!=NULL)
            {
                sum += temp->xiShu * pow(x,temp->zhiShu);
                temp = temp->next;
            }
            cout<<"多項式的值為"<<sum<<endl;
        }
    }
    void sub(data *head1)
    {
        List *c = new List;
        data *temp = c->getHead();
        data *temp1 = head->next;
        data *temp2 = head1->next;
        while(temp1!=NULL && temp2!=NULL)
        {
            data *temp3 = new data;
            if(temp1->zhiShu>temp2->zhiShu)
            {
                temp3->xiShu = temp1->xiShu;
                temp3->zhiShu = temp1->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp1 = temp1->next;
            }
            else if(temp1->zhiShu <temp2->zhiShu)
            {
                temp3->xiShu = temp2->xiShu *(-1);
                temp3->zhiShu = temp2->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp2 = temp2->next;
            }
            else
            {
                temp3->xiShu = temp1->xiShu - temp2->xiShu;
                temp3->zhiShu = temp1->zhiShu;
                temp3->next = NULL;
                temp->next = temp3;
                temp = temp3;
                temp1 = temp1->next;
                temp2 = temp2->next;
            }
        }
        if(temp1!=NULL)
        {
            temp->next = temp1;
        }
        else
        {
            temp->xiShu = temp->xiShu *(-1);
            temp->next = temp2;
        }
        c->showData();
    }
    void merging()
    {
        if(head ->next!=NULL)
        {
            data *temp = head->next;
            data *temp1;
            if(temp->next!=NULL)
            {
                while(temp->next!=NULL)
                {
                    if(temp->zhiShu != temp->next->zhiShu)
                    {
                        temp = temp->next;
                    }
                    else
                    {
                        temp1 = temp->next;
                        temp->next= temp1->next;
                        temp->xiShu = temp1->xiShu+temp->xiShu;
                        delete temp1;
                    }
                }
            }

        }
    }
    void qiuDao()
    {
        List *c = new List;
        data *newHead = c->getHead();
        data *temp = newHead;
        data *temp1 = head->next;
        while(temp1 !=NULL)
        {
            data *temp2 = new data;
            temp2->xiShu = temp1->xiShu * temp1->zhiShu;
            temp2->zhiShu = temp1->zhiShu-1;
            temp2->next = NULL;
            temp1 = temp1->next;
            temp->next = temp2;
            temp = temp2;
        }
        c->showData();
    }
    void  multiply(data *head1)
    {
        List *c = new List;
        data *temp = c->getHead();
        data *temp1 = head->next;
        data *temp2 = head1->next;
        while(temp1!=NULL)
        {
            while(temp2!=NULL)
            {
                data *temp3 = new data;
                temp3->xiShu = temp1->xiShu *temp2->xiShu;
                temp3->zhiShu = temp1->zhiShu +temp2->zhiShu;
                temp3->next = NULL;
                temp2 = temp2->next;
                temp->next = temp3;
                temp = temp3;
            }
            temp1 = temp1->next;
            temp2 = head1->next;
        }
        c->sortData();
        c->merging();
        c->showData();
    }

};
void ini(List *first,List *second)
{
    cout<<"\n\n"<<endl;
    cout<<"[0]給多項式指派"<<endl;
    cout<<"[1]給多項式求導"<<endl;
    cout<<"[2]多項式加法"<<endl;
    cout<<"[3]多項式減法"<<endl;
    cout<<"[4]多項式乘法"<<endl;
    cout<<"[5]退出"<<endl;
    cout<<"請輸入一個要操作的數字"<<endl;
    int i,num;
    cin>>i;
    cout<<"多項式1為:"<<endl;
    first->showData();
    cout<<"多項式2為:"<<endl;
    second->showData();
    switch(i)
    {
    case 0:

        cout<<"請選擇要操作的多項式,1或2"<<endl;
        cin>>num;
        if(num==1) first->evaluate();
        else if(num==2)second->evaluate();
        break;
    case 1:
        cout<<"請選擇要操作的多項式,1或2"<<endl;
        cin>>num;
        if(num==1) first->qiuDao();
        else if(num==2)second->qiuDao();
        break;
    case 2:
        first->add(second->getHead());
        break;
    case 3:
        cout<<"請選擇被減多項式,1或2"<<endl;
        cin>>num;
        if(num==1) first->sub(second->getHead());
        else if(num==2)second->sub(first->getHead());
        break;
    case 4:
        first->multiply(second->getHead());
        break;
    case 5:
        exit(0);
        break;
    default:
        return;

    }
}
int main()
{
    List *e = new List;
    List *e1 = new List;
    cout<<"建立第一個多項式"<<endl;
    e->createList();
    e->sortData();
    e->showData();
    cout<<"建立第二個多項式"<<endl;
    e1->createList();
    e1->sortData();
    e1->showData();
    while(1)
    {
        ini(e,e1);
    }

    return 0;
}
           

繼續閱讀