无法添加两个构建为链表c ++的多项式

时间:2013-11-25 01:07:54

标签: c++ linked-list operator-overloading

*注意:这是一个作业:我不想要一个解决方案,只是一些提示让我思考正确的方向。

我已经创建了多项式术语对象(PolyTerm)的链接列表。我应该为+和 - 编写opperator重载。我无法找到一种方法来完成重载。做研究,我相信我设置链接列表的方式,我可能无法做过载。我想我应该为节点制作一个结构并将节点放入一个类中。相反,我只是将它设置为一个单独的类,现在我正在努力尝试使用指针重载(据我所知,你不能这样做)。

我已经创建了一个附加成员函数,我想知道是否有任何方法可以使用它来破坏+运算符重载?我一直在阅读的所有内容似乎都表明没有,但我只想进行最终验证。

这是我班上的标题:

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;
}

我是否正确,我无法为此类设置运算符重载?

1 个答案:

答案 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撰写看起来不错的功能