Also in meinem Haupt habe ich zwei Polynome A und B, und ich versuche, die Polynome hinzuzufügen, das neue Polynom auszugeben und dann das Polynom B von A subtrahieren und dann ausgeben. Aber das Problem ist, dass nach der Zugabe Funktion, wenn ich die Funktion Subtraktion nennen es die Subtraktion auf den summierten Polynome Durchführung anstelle eines frischen Polynom A und B.Verknüpfte Liste Addition/Subtraktion Funktionen ändern die Werte von Argumenten?
Die Anrufe in Haupt
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);
Dies ist was mein Programm Ausgänge:
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)
aber es sollte drucken:
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)
Aus irgendeinem Grund verhält sich die Additionsfunktion so, als ob sie existiert. Wenn sie versucht, die Subtraktion durchzuführen, findet sie heraus, dass die Knoten momentan existieren, wo sie nicht existieren sollten.
//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;
}
Ich habe versucht, Herumspielen und Ändern Argumente als Referenz übergeben in ist, als Zeiger, änderte sich die Rückkehr mit, anstatt Wert auf Zeiger, aber wenn ich versuche, es zu laufen, es scheiterte nur sagt laufen.
bearbeiten: Hinzufügen Fundort Funktion
Node* findSpot (int coefficient, int exponent)
{
Node *spot = listHead;
while(spot->next != NULL && spot->next->exponent >= exponent)
{
spot = spot->next;
}
return spot;
}
Und ein Pastebin Link auf den gesamten Code, wenn es gebraucht wird - http://pastebin.com/yL85Xif2
Bitte geben Sie eine [mcve] – Barry
Wenn Sie denken, dass "findSpot" wichtig ist, haben Sie Recht. Fügen Sie es und alles, was für ** us ** erforderlich ist, ein, um Ihren Code in einen Editor zu kopieren, ihn zu kompilieren und Ihr Problem zu reproduzieren. Betonen Sie das * alles andere *. – WhozCraig
Ich habe die Funktion findSpot hinzugefügt. Ich denke, ich hätte den ganzen Code kopieren und einfügen können, mit all den Konstruktoren und allem, aber ich fühlte mich, als wäre es zu unhandlich zu lesen? – user2444400