程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c++-稀疏多項式C++鏈表的問題

c++-稀疏多項式C++鏈表的問題

編輯:編程綜合問答
稀疏多項式C++鏈表的問題
 #include<iostream>

using namespace std;
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
template<class T>class Polynomial;
template<class T>ostream& operator<<(ostream& out, Polynomial<T>&a );

template<class T>
class Polynomial{
private:
    class Term{
    public:
        T xishu;
        unsigned mi;
    };
    class Node{
    public:
        Term data;
        Node* next;
        Node(T x = 0, unsigned y = 0, Node* p = 0){
            data.xishu = x;
            data.mi = y;
            Node* next = p;
        }
    };
public:
    Polynomial();
    ~Polynomial();
    Polynomial<T> operator+(const Polynomial<T>&);
    Polynomial<T> operator*(const Polynomial<T>&);
    void initial();
    friend ostream& operator<< <T> (ostream& out, Polynomial<T>& a);

private:
    int highestdigit;
    Node* first;
    int num;//用來判斷多項式有幾項,即鏈表節點數,在乘法運算裡也要用到
};
template<class T>
Polynomial<T>::Polynomial(){
    first = new Node();
    first->next = new Node();
    num = 0;
    highestdigit = 0;
}
template<class T>
Polynomial<T>::~Polynomial(){
    Node* p = first;
    while (p){
        Node* q = p->next;
        delete p;
        p = q;
    }
}

template<class T>
Polynomial<T> Polynomial<T>::operator+(const Polynomial<T>& second){
    Polynomial<T> sum;
    Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
    int digit = 0, num = 0;
    while (pa&&pb){
        if (pa->data.mi == pb->data.mi)  // 指數相等時  
        {
            T x = pa->data.xishu + pb->data.xishu;
            if (x)     // 相加完的系數不為0時  
            {
                pc->next = new Node(x, pa->data.mi);
                pc = pc->next;
                ++num;
                digit = pa->data.mi;
            }
            pa = pa->next;
            pb = pb->next;
        }
        else if (pa->data.mi < pb->data.mi){
            pc->next = new Node(pa->data.xishu, pa->data.mi);
            pc = pc->next;
            pa = pa->next;
            ++num;
            digit = pa->data.mi;
        }
        else{
            pc->next = new Node(pb->data.xishu, pb->data.mi);
            pc = pc->next;
            pb = pb->next;
            ++num;
            digit = pb->data.mi;
        }
    }
    while (pa){
        pc->next = new Node(pa->data.xishu, pa->data.mi);
        pc = pc->next;
        pa = pa->next;
        ++num;
        digit = pa->data.mi;
    }
    while (pb){
        pc->next = new Node(pb->data.xishu, pb->data.mi);
        pc = pc->next;
        pb = pb->next;
        ++num;
        digit = pb->data.mi;
    }       
    sum.num = num;
    sum.highestdigit = digit;
    return sum;

}

template<class T>
Polynomial<T> Polynomial<T>::operator*(const Polynomial<T>& second){
    Polynomial<T> multi;
    Node* pa = first->next, *pb = second.first->next;
    if (num >=second.num){
        Polynomial<T>* p1 = new Polynomial<T>[second.num];  
        Node **p2 = new Node*[second.num];
        for (int i = 0; i < second.num; ++i){
            while (pa){
                if (pa->next == 0){
                    p1[i].highestdigit = pa->data.mi*pb->data.mi;
                    p1[i].num = num;
                }
                p2[i] = p1[i].first;
                p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
                p2[i] = p2[i]->next;
                pa = pa->next;
            }
            pb = pb->next;
            multi = multi + p1[i];
        }
        return multi;
    }
    else{
        Polynomial<T>* p1 = new Polynomial<T>[num];
        Node **p2 = new Node*[num];
        for (int i = 0; i < num; ++i){
            while (pb){
                if(pb->next == 0){
                    p1[i].highestdigit = pa->data.mi*pb->data.mi;
                    p1[i].num = num;
                }
                p2[i] = p1[i].first;
                p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
                p2[i] = p2[i]->next;
                pb = pb->next;
            }
            pa = pa->next;
            multi = multi + p1[i];
        }
        return multi;
    }
}

template<class T>
void Polynomial<T>::initial(){
    T a;
    Node* p = first;
    unsigned b, high=0;
    while (cin >> a&&a!=0&&cin>>b){   //輸入a=0時結束輸入
        if (b >= high)high= b;
        ++num;
        p->next = new Node(a, b);
        p = p->next;
    }
    highestdigit = high;
}
template<class T>
ostream& operator<<(ostream& out, Polynomial<T>& a){
        Polynomial<T>::Node *p = a.first->next;
        if (p->data.xishu == 0)cout << 0 << endl;
        else{
            if (p->data.xishu != 1)
                out << p->data.xishu << "x^" << p->data.mi;
            else    out << "x^" << p->data.mi;          
        }
        p = p->next;
        while (p){
            out << '+';
            if (p->data.xishu != 1)
                out << p->data.xishu << "x^" << p->data.mi;
            else    out << "x^" << p->data.mi;
            p = p->next;
        }
        return out;
    }
    template<class T>
Polynomial<T> Polynomial<T>::operator*(const Polynomial<T>& second){
    Polynomial<T> multi;
    Node* pa = first->next, *pb = second.first->next;
    if (num >=second.num){
        Polynomial<T>* p1 = new Polynomial<T>[second.num];  
        Node **p2 = new Node*[second.num];
        for (int i = 0; i < second.num; ++i){
            while (pa){
                if (pa->next == 0){
                    p1[i].highestdigit = pa->data.mi*pb->data.mi;
                    p1[i].num = num;
                }
                p2[i] = p1[i].first;
                p2[i]->next = new Node(pa->data.xishu*pb->data.xishu, pa->data.mi*pb->data.mi);
                p2[i] = p2[i]->next;
                pa = pa->next;
            }
            pb = pb->next;
            multi = multi + p1[i];
        }
        return multi;
    }
    else{
        Polynomial<T>* p1 = new Polynomial<T>[num];
        Node **p2 = new Node*[num];
        for (int i = 0; i < num; ++i){
            while (pb){
                if(pb->next == 0){
                    p1[i].highestdigit = pa->data.mi*pb->data.mi;
                    p1[i].num = num;
                }
                p2[i] = p1[i].first;
                p2[i]->next = new Node(pb->data.xishu*pa->data.xishu, pb->data.mi*pa->data.mi);
                p2[i] = p2[i]->next;
                pb = pb->next;
            }
            pa = pa->next;
            multi = multi + p1[i];
        }
        return multi;
    }
}
#endif

測試代碼如下:
#include "Polynomial.h"
#include<iostream>
using namespace std;
int main(){
    Polynomial<int> a, b, c;
    a.initial();    
    cout << a <<endl;
    b.initial();
    cout << b << endl;
    cout << a + b << endl;
    return 0;
}
看了好久好久了啊,每次輸出a+b都是0

最佳回答:


刪除了你的類析構函數,鏈表節點的釋放改為成員函數myfree()來實現。
這樣在+重載函數返回時不會自動釋放鏈表。

 #include<iostream>
using namespace std;

//約定稀疏多項式輸入降冪排列
template<class T>class Polynomial;
template<class T>ostream& operator<<(ostream& out, Polynomial<T>&a );

template<class T>
class Polynomial
{
private:
    class Term
    {
    public:
        T xishu;
        unsigned mi;
    };
    class Node
    {
    public:
        Term data;
        Node* next;
        Node(T x = 0, unsigned y = 0, Node* p = 0)
        {
            data.xishu = x;
            data.mi = y;
            next = p;//修改
        }
    };
public:
    Polynomial();
    void myfree();//釋放鏈表各節點
    Polynomial<T>& operator=(const Polynomial<T>& second);
    Polynomial<T> operator+(const Polynomial<T>&);
    void initial();
    friend ostream& operator<< <T> (ostream& out, Polynomial<T>& a);

private:
    int highestdigit;
    Node* first;
    int num;//用來判斷多項式有幾項,即鏈表節點數,在乘法運算裡也要用到
};
template<class T>
Polynomial<T>::Polynomial()
{
    first = new Node;
    num = 0;
    highestdigit = 0;
}

template<class T>
void Polynomial<T>::myfree()    //釋放鏈表各節點
{
    Node* p = first,* q;
    while (p)
    {
        q = p->next;
        delete p;
        p = q;
    }
}

template<class T>
Polynomial<T>& Polynomial<T>::operator=(const Polynomial<T>& second)
{
    highestdigit = second.highestdigit;
    num=second.num;
    Node* p1=first,*p2=second.first;

    while (p2->next)
    {
        p1->next = new Node();
        p1 = p1->next;
        p2 = p2->next;
        *p1=*p2;
    }       
    return *this;
}

template<class T>
Polynomial<T> Polynomial<T>::operator+(const Polynomial<T>& second)
{
    Polynomial<T> sum;
    Node* pa = first->next, *pb = second.first->next, *pc = sum.first;
    while (pa&&pb)
    {
        if (pa->data.mi == pb->data.mi)  // 指數相等時  
        {
            T x = pa->data.xishu + pb->data.xishu;
            if (x)     // 相加完的系數不為0時  
            {
                pc->next = new Node(x, pa->data.mi);
                pc = pc->next;
                ++sum.num;
                sum.highestdigit = pa->data.mi;
            }
            pa = pa->next;
            pb = pb->next;
        }
        else if (pa->data.mi > pb->data.mi){ 
            pc->next = new Node(pa->data.xishu, pa->data.mi);
            pc = pc->next;
            pa = pa->next;
            ++sum.num;
            sum.highestdigit = pa->data.mi;
        }
        else{
            pc->next = new Node(pb->data.xishu, pb->data.mi);
            pc = pc->next;
            pb = pb->next;
            ++sum.num;
            sum.highestdigit = pb->data.mi;
        }
    }
    while (pa){
        pc->next = new Node(pa->data.xishu, pa->data.mi);
        pc = pc->next;
        pa = pa->next;
        ++sum.num;
        sum.highestdigit = pa->data.mi;
    }
    while (pb){
        pc->next = new Node(pb->data.xishu, pb->data.mi);
        pc = pc->next;
        pb = pb->next;
        ++sum.num;
        sum.highestdigit = pb->data.mi;
    }       
    return sum;
}

template<class T>
void Polynomial<T>::initial()
{
    //釋放鏈表,保留頭節點
    Node* p = first->next,* q;
    while (p)
    {
        q = p->next;
        delete p;
        p = q;
    }
    p = first;

    T a;
    int b;
    num=0; highestdigit=0;//修改
    while (cin >> a&&a!=0&&cin>>b){   //輸入a=0時結束輸入
        if (b >= highestdigit) highestdigit= b;
        ++num;
        p->next = new Node(a, b);
        p =p->next;
    }
    fflush(stdin);//修改
}

template<class T>
ostream& operator<<(ostream& out, Polynomial<T>& a)
{
    Polynomial<T>::Node *p = a.first->next;

    if( p==NULL ) return out;//修改

    if (p->data.xishu == 0)cout << endl;//修改
    else{
        if (p->data.xishu != 1)
            out << p->data.xishu << "x^" << p->data.mi;
        else    out << "x^" << p->data.mi;          
    }
    p = p->next;
    while (p)
    {
        out << '+';
        if (p->data.xishu != 1)
            out << p->data.xishu << "x^" << p->data.mi;
        else    out << "x^" << p->data.mi;
        p = p->next;
    }
    return out;
}


int main(){
    Polynomial<int> a,b, c;

    a.initial();    
    cout <<"a= "<< a <<endl;

    b.initial();
    cout <<"b="<< b << endl;

    c=a+b;
    cout << "c="<< c << endl;

    a.myfree();
    b.myfree();
    c.myfree();

    return 0;
}

運行結果:
圖片說明

fanluping123
fanluping123
fanluping123
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved