私のメインでは、私は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
[mcve] – Barry
を提供してください。「findSpot」が重要だと思うなら、あなたは正しいです。それを含めると、あなたのコードをエディタにコピー/ペーストし、コンパイルして、問題を再現するために、** us **に必要なものを含めてください。他の何かを強調してください*。 – WhozCraig
findSpot関数を追加しました。私はすべてのコンストラクタとすべてを使ってコード全体をコピーして貼り付けることができたと思いますが、読みにくいと感じましたか? – user2444400