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

Node は、プライベート データ メンバー係数、電力、および次のノードへのポインターを持つ単なる私のクラスです。LinkedList は、プライベート Node* ヘッド メンバー、パブリック オペレーターのオーバーロード関数、およびいくつかのコンストラクターを含むメイン クラスです。ここで使用するコンストラクターは、head を NULL に設定したデフォルトのコンストラクターです。

2 番目の while ループの後にいくつかの cout ステートメントを配置し、2 つの多項式を乗算して乗算関数をテストしました。

したがって、この場合、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;

セグメンテーション違反が発生します。理由はわかりません。前述したように、最初の while ループ内に cout ステートメントを配置しました。これは、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

おい、私は投稿全体を読むのがちょっと面倒だった..しかし、これが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 に答える