2013-11-25 35 views
0

*注意:這是一個任務:我不想要一個解決方案,只是讓我思考正確方向的一些提示。添加構建爲鏈表C++的兩個多項式時遇到問題

我創建了多項式術語對象(PolyTerm)的鏈接列表。我應該爲+和 - 編寫操作符重載。我一直無法找到一種方法來完成超載。做研究,我相信我設置鏈接列表的方式可能無法完成重載。我想我應該爲節點創建一個結構並將這些節點放入一個類中。相反,我只是將它設置爲一個單獨的類,現在我正在努力嘗試用指針重載(根據我所知,這是你無法做到的)。

我已經創建了一個添加成員函數,並且想知道是否有任何方法來使用它來合併+運算符重載?我一直在讀的東西似乎表明不是,但我只想要最終驗證。

這是我爲我的類的頭:

class PolyTerm 
{ 
public: 

/************************** CLASS CONSTRUCTORS *********************************/ 
    PolyTerm();         // Default constructor 
    PolyTerm(int constant);      // Constant term constructor 
    PolyTerm(int newExp, int newCoeff);   // Unlinked term constructor 
    PolyTerm(int newExp, int newCoeff, 
      PolyTerm* next, PolyTerm* prev); // Full constructor 
    PolyTerm(PolyTerm* original);     // Copy Constructor 
/*******************************************************************************/ 

/**************************** CLASS DESTRUCTOR *********************************/ 
    ~PolyTerm(); 

/************************** ACCESSOR FUNCTIONS *********************************/ 
    int getCoeff() const;   // Returns coefficient of this term 
    int getExp() const;   // Returns the exponent of this term 
    PolyTerm* getNext() const; // Returns the address of the next term 
    PolyTerm* getPrev() const; // Returns the address of the previous term 
/*******************************************************************************/ 

/*************************** MUTATOR FUNCTIONS *********************************/ 
    void setCoeff(int newCoeff);  // Sets the value of this term's coefficient 
    void setExp(int newExp);   // Sets the value of this term's coefficient 
    void setNext(PolyTerm* newNext); // Sets the value of this term's next term 
    void setPrev(PolyTerm* newPrev); // Sets the value of this term's prev term 
/*******************************************************************************/ 

/**************************** MEMBER FUNCTIONS *********************************/ 
    int evalTerm(int value);  // Evaluates the term for using 'value' 
    void printTerm();    // Prints this term. ex '4x^3' 
    void printPoly();    // Prints the whole polynomial 
    void insertTerm(PolyTerm* afterMe, int exp, int coeff); // Node insertion 
    PolyTerm* addTogether(PolyTerm* p2); // adds this and p2 together. 
    PolyTerm* subtractThis(PolyTerm* p2); // subtracts this - p2. 

/*******************************************************************************/ 


/************************** OPERATOR OVERLOADS *********************************/ 
const PolyTerm& operator+(const PolyTerm &other) const; // <----Cant figure this out 

protected: 
/**************************** MEMBER VARIABLES *********************************/ 
    int exp;      // The exponent of this term 
    int coeff;      // The coefficient of this term 
    PolyTerm *next;     // The location of the next term 
    PolyTerm *prev;     // The location of the previous term 
/*******************************************************************************/ 

private: 

}; 

一切都在班上別的工作正常。這裏也是AddTogether函數定義。它運作良好,只是不完全是任務需要的。

PolyTerm* PolyTerm::addTogether(PolyTerm* p2) 
{ 
    PolyTerm* bigHead;   // This pointer has an Ego Problem. 
    PolyTerm* big; 
    PolyTerm* small; 

    int bigDegree, smallDegree; 

    // Sets the bigger degree and smaller degree polynomial. 
    if(this->getExp() >= p2->getExp()) 
    { 
     big = new PolyTerm(this); 
     small = p2; 
    } 
    else 
    { 
     big = new PolyTerm(p2); 
     small = this; 
    } 

    //Assign a head pointer for big polynomial (resultant of sums) 
    bigHead = big; 
    bigDegree = big->getExp(); 
    smallDegree = small->getExp(); 

    // Step through the members of the big polynomial that don't 
    // don't have a corresponding term in small one. 
    for (int i = 0; i < bigDegree - smallDegree; i++) 
    { 
     big = big->getNext(); 
    } 

    // For each term that they have in common, add the coefficients 
    // and create a new term. 
    for (int i = 0; i <= smallDegree; i++) 
    { 
     big->setCoeff(big->getCoeff() + small->getCoeff()); 
     big = big->getNext(); 
     small = small->getNext(); 
    } 

    return bigHead; 
} 

我說得對,我無法爲這個類設置一個運算符重載嗎?

+0

「我覺得我應該做的節點結構,把節點爲類」是正確的。 –

回答

0

希望這會給一些想法

class Test 
{ 
public: 
int x; 
Test(int val) 
{ 
    x = val; 
} 

Test() 
{ 
    x = 0; 
} 
Test* operator+(const Test& testRhs)const 
{ 
    Test* test = new Test(); 
    test->x = this->x + testRhs.x; 
    return test; 
} 

Test* operator+(const Test* testRhs)const 
{ 
    Test* test = new Test(); 
    test->x = this->x + testRhs->x; 
    return test; 
} 

}; 

using namespace std; 

int main() 
{ 

    Test* pTestOut = NULL; 
    Test test0(5); 
    Test test1(3); 

    pTestOut = test0 + test1; //calling Test* operator+(Test& testRhs)const 

    cout << pTestOut->x << endl; 

    Test* pTestOut2 = NULL; 
    Test* pTest3 = new Test(2); 
    Test* pTest4 = new Test(8); 
    pTestOut2 = (*pTest3) + pTest4; //Calling Test* operator+(Test* testRhs)const 
       //First parameter should be object reference. So looks ugly 
    cout << pTestOut2->x << endl; 

    return 0; 
} 

請參考Operator overloading : cannot add two pointers寫好看功能

相關問題