2015-12-01 13 views
0

我正在寫一個程序,實現稀疏多項式,與定義了兩個全球性結構如下:C++多項式輸出爲50%,準確和50%的內存地址

//Global structs to represent a polynomial 
struct Term { 
    int coeff; 
    int degree; 
}; 

struct Node { 
    Term *term; 
    Node *next; 
}; 

類有一個私人Node *poly;

我超載運算符*以獲得兩個多項式的乘積。

該頭文件包括定義:

//Returns a polynomial that is the product of polynomial a and b 
friend const Polynomial operator *(const Polynomial &a, const Polynomial &b); 

和功能,我通過一個表示多項式(一個& b)中,直到達到nullptr每個鏈表迭代,加入度,並將係數相乘,創建一個新項,將其插入臨時多項式,並使用我的重載加法運算符將臨時多項式添加到正在返回的多項式中。我測試了重載的加法運算符,並沒有收到這個問題,所以我不認爲這是造成這個問題。問題是有些時候我回來了正確的答案,例如輸出,第一次運行是正確的,其他運行產生一些方面和一些地址:

>PolynomialTester 
Enter numbebr of terms of the polynomial: 3 
    coefficient degree 
Enter term 1: 3 0 
Enter term 2: 2 1 
Enter term 3: 1 2 

3 + 2*x^1 + 1*x^2 

Enter numbebr of terms of the polynomial: 3 
    coefficient degree 
Enter term 1: 3 0 
Enter term 2: 2 1 
Enter term 3: 1 2 

3 + 2*x^1 + 1*x^2 
9 + 12*x^1 + 7*x^2 + 6*x^3 + 1*x^4 //Correct 
>Exit code: 0 Time: 33.22 
>PolynomialTester 
Enter numbebr of terms of the polynomial: 3 
    coefficient degree 
Enter term 1: 3 0 
Enter term 2: 2 1 
Enter term 3: 1 2 

3 + 2*x^1 + 1*x^2 

Enter numbebr of terms of the polynomial: 3 
    coefficient degree 
Enter term 1: 3 0 
Enter term 2: 2 1 
Enter term 3: 1 2 

3 + 2*x^1 + 1*x^2 
4109104 + 9 + 12*x^1 + 3*x^2 + 2*x^3 + 1*x^4 + 4108992*x^4108944 + 4109392*x^4109136 //Nope 
>Exit code: 0 Time: 19.54 

的功能,我使用了重載*是:

const Polynomial operator *(const Polynomial &a, const Polynomial &b) { 
//Computes the sum of two polynomials a + b 
//Overloaded + operator 

    //The new polynomial that will be returned 
    Polynomial polyProduct; 

    Polynomial tempPoly; 
    //Temporary nodes to process 
    Node *tempA = a.poly; 
    Node *tempB; 

    while(tempA != nullptr) { 
     tempB = b.poly; 
     while(tempB != nullptr) { 

     int degree = tempA->term->degree + tempB->term->degree; 
     int coeff = tempA->term->coeff * tempB->term->coeff; 
     tempPoly.insert(Polynomial::newTerm(coeff, degree)); 
     tempB = tempB->next; 
     } 
     polyProduct = polyProduct + tempPoly; 
     tempPoly.deletePoly(); 
     tempA = tempA->next; 
    } 

    return polyProduct; 
} 

PS。 deletePoly()通過多項式並首先刪除該項,然後刪除該節點,直到它成爲nullptr。

默認構造函數:

Term* Polynomial::newTerm(int c, int d) { 
//Creates a new term 
    Term *newTerm = new Term; 
    newTerm->coeff = c; 
    newTerm->degree = d; 
    return newTerm; 
} 

Node* Polynomial::cons(Term *t, Node *p) { 
//Creates a new node 
    Node *newNode = new Node; 
    newNode->term = t; 
    newNode->next = p; 
    return newNode; 
} 

Polynomial::Polynomial() { 
//Creates the zero polynomial 
    poly = cons(newTerm(0, 0), nullptr); 
} 

PolynomialTester:

int main() { 
    Polynomial newPoly; 
    Polynomial newPoly2; 
    Polynomial newPoly3; 

    newPoly.readPoly(); 
    cout << "\n"; 
    newPoly.printPoly(); 

    cout << "\n"; 

    newPoly2.readPoly(); 
    cout << "\n"; 
    newPoly2.printPoly(); 

    newPoly3 = newPoly * newPoly2; 
    newPoly3.printPoly(); 
    newPoly3.deletePoly(); 
    newPoly2.deletePoly(); 
    newPoly.deletePoly(); 
} 

運營商+

const Polynomial operator +(const Polynomial &a, const Polynomial &b) { 
//Computes the sum of two polynomials a + b 
//Overloaded + operator 

    //The new polynomial that will be returned 
    Polynomial polySum; 
    //Temporary nodes to process 
    Node *tempA = a.poly; 
    Node *tempB = b.poly; 

    //While neither node A or B is equal to the nullptr 
    while(tempA != nullptr && tempB != nullptr) { 

     int degreeA = tempA->term->degree; 
     int degreeB = tempB->term->degree; 

     if(degreeA < degreeB) { 
     polySum.insert(tempA->term); 
     tempA = tempA->next; 
     } 

     else if(degreeA > degreeB) { 
     polySum.insert(tempB->term); 
     tempB = tempB->next; 
     } 

     else { 
     int coeff = tempA->term->coeff + tempB->term->coeff; 
     int degree = degreeA; 
     polySum.insert(Polynomial::newTerm(coeff, degree)); 
     tempA = tempA->next; 
     tempB = tempB->next; 
     } 
    } 
    //Incase one of the polynomials still has remaining terms 
    while(tempA != nullptr) { 
     polySum.insert(tempA->term); 
     tempA = tempA->next; 
    } 
    //Incase one of the polynomials still has remaining terms 
    while(tempB != nullptr) { 
     polySum.insert(tempB->term); 
     tempB = tempB->next; 
    } 
    //Removes any zero coeffiecient terms 
    //Possibly due to a positive and negative coefficient being added 
    polySum.remZeroCoeff(); 
    return polySum; 
} 

拷貝構造函數:

Polynomial::Polynomial(const Polynomial& oP) { 
//Constructs a deep copy of the Polynomial being passed 

    poly = nullptr; 
    //The list to copy 
    Node *toCopy = oP.poly; 

    while(toCopy != nullptr) { 
     insert(toCopy->term); 
     poly->next = toCopy->next; 
     toCopy = toCopy->next; 
    } 
} 

重載賦值運算符,但是這是在我的代碼當中,因爲它只是似乎使事情更糟

Polynomial& Polynomial::operator =(const Polynomial &a) { 

    //Check to see if it's passing itself 
    if(this == &a) { 
     return *this; 
    } 

    //Delete current polynomial 
    deletePoly(); 

    //The poly to copy 
    Node *toCopy = a.poly; 

    while(toCopy != nullptr) { 
     poly = cons(toCopy->term, poly); 
     toCopy = toCopy->next; 
    } 

    //Return a reference to itself 
    return *this; 
} 

誰能幫我明白爲什麼我得到地址作爲結果有一定的時間呢?

+0

'poly-> next = toCopy-> next'呵呵?這是做什麼的?並且仍然沒有'operator ='。你有嗎? –

+0

@Dmitriy由於最初一個循環之後的兩個while循環,不會被照顧嗎? – coopwatts

+0

@ n.m。沒有重載的賦值運算符 – coopwatts

回答

0

The line tempPoly.deletePoly();造成了這個問題,所以爲了提高效率並解決問題,我在插入函數中添加了一個條件,用於檢查傳遞的項的度數是否等於當前多項式的項度數,如果是,則只需添加該術語的係數並在導致不需要之後刪除該術語。這消除了在重載*功能中進行任何刪除或重載添加的需要,並且提高了效率。

+0

解釋這是如何引入內存泄漏?這個答案中沒有任何內容泄漏,事實上,答案完全消除了對臨時免費存儲內存的需求。刪除poly仍然在主函數的多項式上被調用,但是它沒有在重載乘法函數中調用,因爲沒有東西需要刪除。就像我在答案中所說的那樣,如果在插入方法中創建了一個不需要的術語,它將被刪除。除此之外,這個析構函數,複製構造函數和重載賦值運算符都已到位。 – coopwatts

+0

我很抱歉,我誤解了你的解釋。出於某種原因,我想你已經從析構函數中刪除了deletePoly()。沒有內存泄漏。 Downvote刪除。 –

+0

不用等待,在析構函數中沒有deletePoly(),對吧?這是錯的,析構函數是調用它的正確位置,這就是析構函數設計的目的。無論如何,如果你想聽到建設性的批評,發佈代碼codereview。 stackexchange.com。 –

相關問題