2016-06-13 8 views
0

私のメインでは、私は2つの多項式AとBを持っており、多項式を追加しようとしています。新しい多項式を出力し、Aから多項式Bを減算して出力します。しかし、問題は、私はそれが加算された代わりに、新鮮な多項式Aの多項式とB.リンクリスト加算/減算関数が引数の値を変更していますか?

メイン

outfile << "Printing addition of the first polynomial and the second polynomial" << endl << endl; 
linkedList polynomialD; 
polynomialD = polynomialD.polynomialAddition(polynomialA, polynomialB); 
polynomialD.printList(outfile); 

outfile << endl; 
outfile << "Printing subtraction of the second polynomial from the first polynomial" << endl << endl; 
linkedList polynomialE; 
polynomialE = polynomialE.polynomialSubtraction(polynomialA, polynomialB); 
polynomialE.printList(outfile); 

中のコールに減算を行っています減算機能を呼び出すとき加算関数の後に、これがあることですそれは印刷する必要があり

Polynomial in canonical form - 
(6x^6) + (9x^2) + (-5) 

Polynomial in canonical form - 
(7x^7) + (2x^5) + (7x^2) + (12) 

Printing addition of the first polynomial and the second polynomial 

Polynomial in canonical form - 
(7x^7) + (6x^6) + (2x^5) + (16x^2) + (7) 


Printing subtraction of the second polynomial from the first polynomial 

Polynomial in canonical form - 
(0x^7) + (6x^6) + (0x^5) + (9x^2) + (-5) 

:しかし何私のプログラムの出力

Polynomial in canonical form - 
(6x^6) + (9x^2) + (-5) 

Polynomial in canonical form - 
(7x^7) + (2x^5) + (7x^2) + (12) 

Printing addition of the first polynomial and the second polynomial 

Polynomial in canonical form - 
(7x^7) + (6x^6) + (2x^5) + (16x^2) + (7) 


Printing subtraction of the second polynomial from the first polynomial 

Polynomial in canonical form - 
(-7x^7) + (6x^6) + (-2x^5) + (2x^2) + (-17) 

何らかの理由で、加算関数が存在するように動作しているので、減算をしようとしているときに、ノードが現在存在していることが分かります。

//Function to insert into the linked list 
void listInsert(int coefficient, int exponent) 
{ 
    Node *spot = findSpot (coefficient, exponent); 

    if(spot->exponent == exponent) 
    { 
     int temp; 
     temp = spot->coefficient + coefficient; 
     spot->coefficient = temp; 
    } 
    else 
    { 
     Node* newNode = new Node(coefficient, exponent); 
     newNode->next = spot->next; 
     spot->next = newNode; 
    } 
} 

//Function used to insert into the linked list but subtracting like powers. 
void listInsertSubtraction(int coefficient, int exponent) 
{ 
    Node *spot = findSpot (coefficient, exponent); 

    if(spot->exponent == exponent) 
    { 
     int temp; 
     temp = spot->coefficient - coefficient; 
     spot->coefficient = temp;   
    } 
    else 
    { 
     int tempcoeff; 
     tempcoeff = -coefficient; 

     Node* newNode = new Node(coefficient, exponent); 
     newNode->next = spot->next; 
     spot->next = newNode; 
    } 
} 

//Function to add two polynomials. 
linkedList polynomialAddition (linkedList& polynomialA, linkedList& polynomialB) 
{ 
    linkedList newPolynomial = polynomialA; 

    //Temporary Nodes point to the first element of each linked list. 
    Node* tempNodeB = polynomialB.listHead->next; 

    while (tempNodeB != NULL) 
    { 
     newPolynomial.listInsert (tempNodeB->coefficient, tempNodeB->exponent); 
     tempNodeB = tempNodeB->next; 
    } 
    return newPolynomial; 
} 

//Function to subtract the 2nd polynomial from the first polynomial. 
linkedList polynomialSubtraction (linkedList& polynomialA, linkedList& polynomialB) 
{ 
    linkedList newPolynomial = polynomialA; 

    //Temporary Nodes point to the first element of each linked list. 
    Node* tempNodeB = polynomialB.listHead->next; 

    while (tempNodeB != NULL) 
    { 
     newPolynomial.listInsertSubtraction (tempNodeB->coefficient, tempNodeB->exponent); 
     tempNodeB = tempNodeB->next; 
    } 
    return newPolynomial; 
} 

私はいじり、参照によりに渡される引数を変更しようとした、ポインタとして、リターンを有する値ではなくポインタに変更しましたが、私はそれを実行しようとすると、それだけで実行が失敗したと言います。

編集:findSpot機能にコード全体に

Node* findSpot (int coefficient, int exponent) 
{ 
    Node *spot = listHead; 

    while(spot->next != NULL && spot->next->exponent >= exponent) 
    { 
     spot = spot->next; 
    } 

    return spot; 
} 

そしてペーストビンのリンクを追加することが必要になった場合 - http://pastebin.com/yL85Xif2

+2

[mcve] – Barry

+0

を提供してください。「findSpot」が重要だと思うなら、あなたは正しいです。それを含めると、あなたのコードをエディタにコピー/ペーストし、コンパイルして、問題を再現するために、** us **に必要なものを含めてください。他の何かを強調してください*。 – WhozCraig

+0

findSpot関数を追加しました。私はすべてのコンストラクタとすべてを使ってコード全体をコピーして貼り付けることができたと思いますが、読みにくいと感じましたか? – user2444400

答えて

2

を私のためにあなたがlinkedList newPolynomial = polynomialA;を行うとき、あなたは新しいリストを作成しますが、使用していませんpolynomialA list。

あなたが加えた後、あなたのpolynomialAを印刷する場合は、なぜあなたsubstraction後にあなたが0x^7のような何かを得ること、それが変更されていることがわかります。 アルゴリズムを維持するには、polynomialAのコピーを作成し、それをnewPolynomialとして使用する必要があります。

+0

私はここで私が混同していると思います。 linkedListを実行することによってnewPolynomial = polynomialA;私はすでにそれをコピーしていたと思ったが、そうは思わない。どのように私はそれの実際のコピーを作るのですか?そして、ええ、追加後にAのリストを印刷したとき、それは確かに変更されました。 – user2444400

+0

クラスのコピーコンストラクタと代入演算子を実装してコピーを作成します。いったん正しく実装されると、 '='は期待どおりに動作します。 – PaulMcKenzie

+0

ええ、私はそれについていくつかの読書をして、私はそれを働かせました。ありがとう。それは予想よりも長くかかりました。コピーコンストラクタと割り当てのオーバーロードのために、私はポインタの変更や、うまくいきませんでした他のいろいろなものを使いこなすのに長い時間を費やしました。 – user2444400

関連する問題