C++多項式的乘法

2020-10-15 14:00:41

寫程式碼半小時,找bug兩小時
這波終於沒bug啦!
耶耶耶

/*
*改編:蘇格扯底
*
*
*
*
*
*
*/


#include<iostream>
using namespace std;
typedef struct pnode {
    int coef;
    int exp;
    pnode* next;
}pnode;
class poly {
private:
    pnode* head;
public:
    poly() { head = new pnode; head->next = NULL; }
    void create(int n);
    void print();
    int comp(int a, int b);
    void addpoly(poly& ha, poly hb);
    //在ha中加項x,
    void addpnode(poly& ha, pnode* x);
    void timespoly(poly& hc, poly ha, poly hb);



};
void poly::create(int n) {
    pnode* rear = head, * s;
    for (int i = 0; i < n; i++) {
        s = new pnode;
        cin >> s->coef >> s->exp;
        rear->next = s;
        rear = s;
    }
    rear->next = NULL;
}
void poly::print() {
    pnode* p = head->next;
    while (p) {
        cout << p->coef << ' ' << p->exp << ',';
        p = p->next;
    }
    cout << endl;
}
int poly::comp(int a, int b) {
    if (a > b)return -1;
    else if (a == b)return 0;
    else return 1;
}

//把hb多項式加到ha多項式上去
void poly::addpoly(poly& ha, poly hb) {
    pnode* pa = ha.head->next, * q = ha.head, * pb = hb.head->next, * r;
    while (pa && pb)
        switch (comp(pa->exp, pb->exp)) {
        case -1:q = pa; pa = pa->next; break;
        case 1:r = new pnode;
            r->coef = pb->coef;
            r->exp = pb->exp;
            r->next = pa;
            q->next = r;
            q = r;
            pb = pb->next;
            break;
        case 0:pa->coef += pb->coef;
            if (pa->coef == 0) {
                q->next = pa->next;
                delete pa;
                pa = q;
            }
            else q = pa;
            pa = pa->next;
            pb = pb->next;
            break;
        }
    while (pb) {
        r = new pnode;
        r->coef = pb->coef;
        r->exp = pb->exp;
        r->next = NULL;
        q->next = r;
        q - r;
        pb = pb->next;
    }
}

void poly::addpnode(poly& ha, pnode* x)
{
    pnode* pa = ha.head->next, * q = ha.head ,*r;
    if (pa == NULL)
    {
        r = new pnode;
        r->coef = x->coef;
        r->exp = x->exp;
        r->next = pa;
        q->next = r;
        q = r;
        return;
    }
    while (pa)
        switch (comp(pa->exp, x->exp)) {
        case -1: q = pa; pa = pa->next;
            if (pa == NULL)
            {
                r = new pnode;
                r->coef = x->coef;
                r->exp = x->exp;
                r->next = pa;
                q->next = r;
                q = r;
                return;
            }break;
        case 1:r = new pnode;
            r->coef = x->coef;
            r->exp = x->exp;
            r->next = pa;
            q->next = r;
            q = r;
            break;
        case 0:pa->coef += x->coef;
            if (pa->coef == 0) {
                q->next = pa->next;
                delete pa;
                pa = q;
            }//係數為0的話就刪除該節點
            else q = pa;
            pa = pa->next;
            break;
            
        }
    
}


//把ha,hb相乘得到hc
void poly::timespoly(poly& hc, poly ha, poly hb)
{
    pnode* pa = ha.head->next, * pb = hb.head->next;
    pnode* r;
    while (pa) {
        while (pb)
        {
            r = new pnode;
            r->coef = pa->coef * pb->coef;//係數相乘
            r->exp = pa->exp + pb->exp;//指數相加
            addpnode(hc, r);
            pb = pb->next;
        }
        pb = hb.head->next;
        pa = pa->next;


   }
}



int main() {
    poly ha, hb,hc;
    ha.create(2);
    hb.create(3);
    ha.print();
    hb.print();
    ha.timespoly(hc,ha,hb);
    hc.print();

    return 0;
}