lundi 27 novembre 2017

Poly output error - linked list

My current output is giving me an error and I do not understand why. If someone could guide me as to why it does it would be greatly appreciated. I am able to add two polynomials together but when I get the output I get a segmentation fault after removing a space from the output operator. I do not know why this is. I am also using codeblocks if that helps.

main.cpp

#include <iostream>
#include "poly.h"
using namespace std;
int main ()
{

int x1[] = {1 , 0 , 3 , 4 , 5};
int x2[] = {3 , 2};

polynomial p1(x1 , 4);
polynomial p2(x2 , 1);
polynomial p3(5);
polynomial p4;
polynomial result;

result = 6;


cout << " p1 = " << p1 << endl ;
cout << " p2 = " << p2 << endl ;
cout << " p3 = " << p3 << endl ;
cout << " p4 = " << p4 << endl ;
cout << " result = " << result << endl << endl ;

result = p1 + p2 ;
cout << " p1 + p2 = " << result << endl ;

poly.h

#include <iostream>
using namespace std;

class polynomial
{

    struct node
    {
        int coefficient ;
        node * link ;
    };

public:
polynomial();
polynomial(const polynomial&);
polynomial(int* ,int);
polynomial(int);
~polynomial();


polynomial operator+(const polynomial&) const;
polynomial operator+(int) const;
const polynomial& operator=(const polynomial &);
const polynomial& operator=(int);

friend ostream& operator<<(ostream& outfile , const polynomial&);
friend polynomial operator+(int ,const polynomial&);

private:
node* head;
int degree;
};

poly.cpp

#include <iostream>
#include "poly.h"
using namespace std;

polynomial::polynomial()
{
    head = new node;
    head->coefficient = 0;
    head->link = NULL;
    degree = -1;
};

polynomial::polynomial(const polynomial& copy)
{
    if(this != &copy)
    {
        delete[] head;
        head = copy.head;
    }
};

polynomial::polynomial(int * p, int degree)
{
    this->degree = degree;
    head = new node;
    head->coefficient = p[0];
    head->link = NULL;

    for(int x=1;x<degree;x++)
    {
        node* temp;
        temp = new node;
        temp->coefficient = p[x];
        temp->link = head;
        head = temp;
    }

    node* temp;
    temp = new node;
    temp->coefficient = p[degree];
    temp->link = head;
    head = temp;
};

polynomial::polynomial(int s)
{
    degree = 0;
    head = new node;
    head->coefficient = s;
    head->link = NULL;
};

polynomial::~polynomial()
{
    node* temp = head;
    node* current = head;
    while(current != NULL)
    {
        current = current->link;
        delete temp;
        temp = current;
        if (current == NULL || current == NULL)
            break;
    }
};

polynomial polynomial::operator+(const polynomial& rhs) const
{
    polynomial hold;
    polynomial tempLhs;
    polynomial tempRhs = rhs;

    tempLhs.degree = degree;
    tempRhs.degree = rhs.degree;
    hold.degree;
    int tempDegree;

    tempLhs.head = new node;
    tempRhs.head = new node;
    hold.head = new node;

    for(int x=0;x<tempDegree+1;x++)
    {
        node* temp;
        temp = new node;
        temp->coefficient = 0;
        temp->link = hold.head;
        hold.head = temp;
    }

    tempLhs.head = head;
    tempRhs.head = rhs.head;

    if(tempLhs.degree < tempRhs.degree)
    {
        tempDegree = tempLhs.degree;
        hold.degree = tempDegree;
        for(int x = (tempDegree-tempLhs.degree-1);x<tempDegree+1;x++)
        {
            node* temp;
            temp = new node;
            temp->coefficient = 0;
            temp->link = tempLhs.head;
            tempLhs.head = temp;
        }

    }
    else if(tempLhs.degree > tempRhs.degree)
    {
        tempDegree = tempLhs.degree;
        hold.degree = tempDegree;
        for(int x = (tempDegree-tempRhs.degree-1);x<tempDegree+1;x++)
        {
            node* temp;
            temp = new node;
            temp->coefficient = 0;
            temp->link = tempRhs.head;
            tempRhs.head = temp;
        }
    }
    else
    {
        tempDegree = tempRhs.degree = tempLhs.degree;
        hold.degree = tempDegree;
    }


    node* lhsCurrent = tempLhs.head;
    node* rhsCurrent = tempRhs.head;
    int tempArr[tempDegree];

    while(lhsCurrent != NULL && rhsCurrent != NULL)
    {
        for(int x=tempDegree;x>-1;x--)
        {
            tempArr[x]= lhsCurrent->coefficient + rhsCurrent->coefficient;
            lhsCurrent = lhsCurrent->link;
            rhsCurrent = rhsCurrent->link;
        }
    }

    polynomial use(tempArr, tempDegree);
    return use;
};

polynomial polynomial::operator+(int rhs) const
{
    polynomial temp = *this;

    return rhs+temp;
};

const polynomial& polynomial::operator=(const polynomial& rhs)
{
    cout << "doing = operator" << endl;
    degree = rhs.degree;

    if(this != &rhs)
    {
        delete[] head;
        head = rhs.head;
    }

    return *this;
};

const polynomial& polynomial::operator=(int rhs)
{
    degree = 0;
    head = new node;
    head->coefficient = rhs;
    head->link = NULL;
};

ostream& operator<<(ostream& out, const polynomial& rhs)
{
    out << "operator ";

    polynomial::node* temp = new polynomial::node;
    temp = rhs.head;

    while(temp != NULL)
    {
        out << temp->coefficient << " ";
        temp = temp->link;
        if(temp == NULL)
            break;
    }
    out << " ";
};

The output should be this

p1 = 5 x ^4 + x ^2 + 5 x + 4
p2 = 3 x + 2
p3 = 5
p4 = 0
result = 6
p1 + p2 = 5 x ^4 + x ^2 + 8 x + 6

I am getting this result but I just have to format it so that the degrees are represented correctly but my addition it coming out correctly I just need to adjust the output operator which is not the issue.

Whenever I run the program without

out << " ";

which is the second to last line of poly.cpp I get an error.

It says I have segmentation fault after line 215 which happens to be the last line of poly.cpp when the out<< is deleted from the code.

Aucun commentaire:

Enregistrer un commentaire