1

我正在用 C++ 做一个多项式程序,我们应该用一个单链表来实现它。是的,这是一个家庭作业。我已经完成了大部分程序,但我只是坚持我的乘法运算符重载。这是我的 operator* 函数:

LinkedList operator*(const LinkedList& a, const LinkedList& b)
{
    LinkedList product;
    Node* nodeA = a.head;
    Node* nodeB = b.head;
    int coeff, powr;

    if (nodeA == NULL && nodeB == NULL)
        return product;
    else if (nodeA == NULL && nodeB != NULL)
        return b;
    else if (nodeA != NULL && nodeB == NULL)
        return a;
    else {
        while (nodeA != NULL) {
            while (nodeB != NULL) {
                coeff = nodeA->getCoeff() * nodeB->getCoeff();
                powr = nodeA->getPow() + nodeB->getPow();
                product.addElement(coeff, powr);
                nodeB = nodeB->getNext();
            }
            nodeB = b.head;
            nodeA = nodeA->getNext();
        }
    }
    return product;
}

作为参考,我现在只是在链接列表的末尾添加一个新元素。

这是我的 AddElement 函数:

void LinkedList::addElement(int coeff, int powr)
{
    Node *newNode = new Node();

    // Set the Node's data
    newNode->setPowAndCoefficient(coeff, powr);
    newNode->setNextNode(NULL);
    Node *temp = head;

    if (temp != NULL) {
        // Go to the last element of the list
        while (temp->getNext() != NULL) {
            temp = temp->getNext();
        }
        // temp is now the last element and its next element is null
        // Set temp's next node to be the newNode
        temp->setNextNode(newNode);
    }
    else
        head = newNode;
}

节点只是我的类,具有私有数据成员系数、功率和指向下一个节点的指针。LinkedList 是我的主类,它包括一个私有 Node* 头成员和公共运算符重载函数和几个构造函数。这里使用的构造函数是默认构造函数,我只是将 head 设置为 NULL。

我在第二个 while 循环之后放置了一些 cout 语句,并乘以两个多项式来测试我的乘法函数。

所以在这种情况下,我的 main.cpp 文件中有这段代码:

LinkedList poly1, poly2, result;
    // The first polynomial: 3x^3 + 7x^2 - 7
    poly1.addElement(3, 3);
    poly1.addElement(7, 2);
    poly1.addElement(-7, 0);
    cout << "Polynomial A: " << poly1 << endl;

    // The second polynomial: -5x^5 - 14x^3 + 7x^2 + 14
    poly2.addElement(-5, 14);
    poly2.addElement(-14, 3);
    poly2.addElement(7, 2);
    poly2.addElement(14, 0);
    cout << "Polynomial B: " << poly2 << endl;

此外, << 重载运算符可以正常工作并正常显示链接列表。问题是当我尝试这样做时:

result = poly1 * poly2;

我得到一个分段错误。我不知道为什么。正如我所说,我将 cout 语句放在第一个 while 循环中,这就是我在执行 poly1 * poly2 时得到的:

 -15x^17 - 42x^6 + 21x^5 + 42x^3 - 35x^16 - 98x^5 + 49x^4 + 98x^2 + 35x^14 + 98x^3 - 49x^2 - 98
[1]    39009 segmentation fault  ~/Desktop/run

是的,它很丑陋,但这是在我将所有这些东西加在一起之前。但无论如何,它基本上是正确的。在评估最后一个常数后,我只是得到一个分段错误。

我不知道它为什么这样做,它只对乘法运算符这样做。其他东西虽然很好。我可能在某个地方有一个错误,过去几个小时我一直在寻找它,但我不知道我做错了什么。有人可以帮忙吗?

谢谢。

我的节点类:

class Node {
private:
    int power;
    int coefficient;
    Node *next;
public:
    Node(); // in implementation: coeff = 0, power = 0, next = NULL;
    Node(const int coeff, const int powr = 1);
    void setPowAndCoefficient(const int coeff, const int powr);
    inline int getPow() const { return power; };

    inline int getCoeff() const { return coefficient; };

    inline void setNextNode(Node *aNode) { next = aNode; };

    inline Node *getNext() const { return next; };
};


Node::Node()
{
    coefficient = 0;
    power = 1;
    next = NULL;
}

Node::Node(const int coeff, const int powr)
{
    coefficient = coeff;
    power = powr;
    next = NULL;
}

void Node::setPowAndCoefficient(const int coeff, const int powr)
{
    coefficient = coeff;
    power = powr;
    next = NULL;
}
4

1 回答 1

2

伙计,我有点懒得通读整篇文章..但这就是我如何将两个多项式相乘..

    LinkedList operator*(const LinkedList& a, const LinkedList& b){
int coef,pow;
LinkedList temp = new LinkedList();
    for(node * a1 = a->head;a1!=NULL;a1=a1->next)
    for(node * b1 = b->head;b1!=NULL;b1=b1->next){
     coef = a1->getCoeff() * b1-> getCoeff();
     pow = a1->getPow()+b1->getPow();
node ab  = new node(coef,pow);//Writting it java style, cant remember if this is how u       //declare objects in c++ :(
temp.addNode(ab);
}
return temp;
}

如果对您没有帮助,我很抱歉.. 但我只是想向您介绍一个想法。

于 2012-10-14T17:13:25.347 回答