mercredi 15 juin 2016

Linked List Addition/Subtraction Functions are changing the values of arguments?

So in my main, I have two polynomials A and B, and I'm trying to add the polynomials, output the new polynomial and then subtract the polynomial B from A then output. But the problem is that after the addition function, when I call the subtraction function it's performing the subtraction on the summed polynomials instead of a fresh polynomial A and B.

The calls in main

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

This is what my program outputs:

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)

But it SHOULD print:

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)

For some reason the addition function is making it act like it exists so when it's trying to do the subtraction it finds that the nodes currently exist, where it shouldn't.

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

I tried messing around and changing arguments being passed into by reference, as a pointer, having the return changed to pointer instead of value, but when I try to run it, it just says run failed.

edit: Adding findSpot function

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

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

  return spot;  
}

And a pastebin link to the whole code if it's needed - http://pastebin.com/yL85Xif2

Aucun commentaire:

Enregistrer un commentaire