財神娛樂首存即享優惠回饋唷~詳情請進👉

【數據布局】1-3 多項式相吃角子老虎 玩法加

六合彩坐車

實在這個仍是有點成績的,在偶見環境下會打印出0*x,現在無解唔。。。

道理便是借用線性表,然后做運算間接先拔出到前面。

然后遍歷一下,只需指數雷同就歸并在后面一個內里,前面的回0(不刪除)。

打印的時辰加一個判定是否為0的前提就行了。

上面是源碼:

#include<iostream>
#include<cstring>
using namespace std;
struct Node
{
    double coe;//系數
    int index;//指數
    Node *link;
};
class polynomial
{
private:
    Node *head,*tail;        //界說頭指針,尾指針
public:
    polynomial();//無參組織函數
    polynomial(double 玩運彩a[],int n[],int number);    //有參組織函數
    ~polynomial();            //析構函數
    void setup();            //求值函數
    void add(double a[],int number);
    void subtract(double a[],int number);
    void print();
};
polynomial::polynomial()
{
    head = NULL;
    tail = NULL;
}
polynomial::polynomial(double a[],int number)
{
    head = 妞妞算牌new Node;
    head->inde捕魚達人apkx = 0;
    head->coe = 0;
    tail = head;
    for (int i = 0; i < number; i++)
    {
        Node *p = new Node;
        p->coe = a[i];            //存儲系數
        p->index = n[i];        //存儲指數
        p->link = NULL;
        tail->link = p;
        tail = p;
    }
    setup();
}
polynomial::~polynomial()
{

    if (head != NULL)
    {
        head = tail = NULL;
    }
}
void polynomial::add(double a[],int number)
{
    for (int i = 0; i < number; i++)
    {
        Node *add = new Node;
        add->coe = a[i];
        add->index = n[i];
        tail->link = add;
        tail = add;
    }
    tail->link = NULL;
    setup();
}
void polynomial::subtract(double a[],int number)
{
    for (int i = 0; i < number; i++)
    {
        Node *add = new Node;
        add->coe = -a[i];
        add->index = n[i];
        tail->link = add;
        tail = add;
    }
    tail->link = NULL;
    setup();
}
void polynomial::setup()
{
    Node *m = head->link;
    Node *p;
    while (m != NULL)
    {
        p = m->link;
        while (p != NULL)
        {
            if (m->index == p->index)
            {
                m->coe = m->coe + p->coe;
                p->coe = 0;
            }
            p = p->link;
        }
        m = m->link;
    }
    tail->link = NULL;
}
void polynomial::print()
{
    if (head == NULL)
    {
        cout << "大眾過錯,無數據!"大眾 << endl;
        exit(1);
    }
    Node *p = head;
    while (p != NULL)
    {
        p = p->link;
        if (p == NULL)
        {
            cout << endl;
            break;
        }
        if (p->coe == 0)
        {
            continue;
        }
        else
        {
            cout << p->coe << "大眾x^"大眾 << p->index;
            if (p->link!= NULL&&p->link->coe>0)
            {
                cout << 公眾+公眾;
            }
            
        }
        
        
    }
}
int main()
{
    int number;
    int *index;
    double *coe;
    cout << "大眾迎接使用多項式計算器,請輸出多項式的元素個數:"大眾 << endl;
    cin >> number;
    index = new int[number];
    coe = new double[number];
    for (int i = 0; i < number; i++)
    {
        cout << 公眾請輸出第"大眾 << i + 1 << "大眾個多項式的系數: 公眾;
        cin >>coe[i];
        cout << "大眾請輸出第公眾 << i + 1 << "大眾個多項式的指數: "大眾;
        cin >> index[i];
    }
    polynomial test1(coe,index,number);
    test1.print();
    cout << "大眾請輸出相加的多項式的元素個數:"大眾 << endl;
    cin >> number;
    delete []index;
    delete []coe;
    index = new int[number];
    coe = new double[number];
    for (int i = 0; i < number; i++)
    {
        cout << "大眾請輸出第"大眾 << i + 1 << "大眾個多項式的系數: "大眾;
        cin >> coe[i];
        cout << 公眾請輸出第公眾 << i + 1 << "大眾個多項式的指數: "大眾;
        cin >> index[i];
    }
    test1.add(coe,number);
    cout <&地下539玩法lt; 公眾相加勝利!"大眾 << endl;
    test1.print();
    cout << "大眾請輸出相減的多項式的元素個數:公眾 << endl;
    cin >> number;
    delete[]index;
    delete[]coe;
    index = new int[number];
    coe = new double[number];
    for (int i = 0; i < number; i++)
    {
        cout << "大眾請輸出第"大眾 << i + 1 << "大眾個多項式的系數: 公眾;
        cin >> coe[i];
        cout << 公眾請輸出第公眾 << i + 1 << "大眾個多項式的指數: "大眾;
        cin >> index[i];
    }
    test1.subtract(coe,number);
    cout << "大眾相減勝利!"大眾 << endl;
    test1.print();

    system(公眾pause"大眾);
    return 0;
}

【免責聲明】本站內容轉載自互聯網,其相關談吐僅代表作者小我私家概念盡非權勢巨子,不代表本站態度。如您發明內容存在版權成績,請提交相關鏈接至郵箱:,咱們將實時予以處置。