Reputation: 1
hope you're all doing well im working on an assignment and i've been stuck here at this point for ages, and there are no errors or warnings in my code i dunno whats wrong. I'd appreciate the help i checked all the pointers and null related things and still i couldnt find out what was wrong
#pragma once
#include<iostream>
using namespace std;
struct Node {
public:
int coefficient;
int power;
Node* Next;
int getcoefficient()
{
return this->coefficient;
}
int getpower()
{
return this->power;
}
};
class Polynomial {
private:
Node* Head;
Node* Tail;
public:
Polynomial();
int getco();
int getpow();
Polynomial(int arraycoefficients[], int arraypower[], int n);
~Polynomial();
void insert(int coefficient1, int power1);
void remove(int power1);
double Evaluate(double value);
Polynomial operator+(Polynomial O2);
/*Polynomial& operator=(Polynomial& p);*/
int degree()const;
void merge(Polynomial p);
void removeAll();
friend istream& operator>>(istream& in, Polynomial& P);
friend ostream& operator<<(ostream& out, const Polynomial& P);
};
and this is the cpp for the header file
#include "Polynomial.h"
#include<math.h>
#include <iostream>
using namespace std;
Polynomial::Polynomial()
{
Head = Tail = NULL;
}
int Polynomial::getco()
{
return Head->coefficient;
}
int Polynomial::getpow()
{
return Head->power;
}
Polynomial::Polynomial(int arraycoefficients[], int arraypower[], int n)
{
Head = NULL;
Tail = NULL;
for (int i = 0; i < n; i++) {
insert(arraycoefficients[i], arraypower[i]);
}
}
Polynomial::~Polynomial()
{
removeAll();
}
void Polynomial::insert(int coefficient1, int power1)
{
Node* node = new Node;
node->coefficient = coefficient1;
node->power = power1;
node->Next = NULL;
if (Head == NULL) {
Head = Tail = node;
return;
}
//addition at head
if (power1 == Head->power) {
Head->coefficient = Head->coefficient + coefficient1;
return;
}
//insert at head
if (power1 < Head->power) {
node->Next = Head;
Head = node;
return;
}
Node* temporary = Head;
while (temporary->Next != NULL) {
if (temporary->power == power1) {
temporary->coefficient = temporary->coefficient + coefficient1;
return;
}
if (power1 > temporary->power && power1 < temporary->Next->power) {
node->Next = temporary->Next;
temporary->Next = node;
return;
}
temporary = temporary->Next;
}
//insert at last
if (temporary->power == power1) {
temporary->coefficient = temporary->coefficient + coefficient1;
return;
}
temporary->Next = node;
Tail = node;
}
void Polynomial::remove(int pw)
{
if (Head == NULL)
return;
//only one node
if (Head->power == pw && Head == Tail) {
delete Head;
Head = Tail = NULL;
return;
}
Node* temporary = Head;
while (temporary->Next != NULL) {
if (pw == temporary->Next->power)
{
Node* node = temporary->Next;
temporary->Next = temporary->Next->Next;
if (temporary->Next == NULL)
Tail = temporary;
delete node;
return;
}
temporary = temporary->Next;
}
}
double Polynomial::Evaluate(double value)
{
Node* temporary = Head;
double result = 0.0;
while (temporary != NULL) {
result =result+ (temporary->coefficient * pow(value, temporary->power));
temporary = temporary->Next;
}
return result;
}
/*Polynomial& Polynomial::operator=(Polynomial& p)
{
Node* temporary = p.Head;
int pp;
int cc;
removeAll();
while (temporary != nullptr) {
pp = temporary->getpower();
cc = temporary->getcoefficient();
insert(cc,pp);
temporary = temporary->Next;
}
return *this;
}*/
int Polynomial::degree() const
{
if (Head == NULL)
{
cout << "Empty polynomial\n";
return -1;
}
return Tail->power;
}
void Polynomial::merge(Polynomial p)
{
int count = 0;
Node* temporary = p.Head;
while (temporary != NULL) {
insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
p.removeAll();
return;
}
void Polynomial::removeAll()
{
if (Head == NULL)
return;
Node* CurrentNode = Head;
Node* NextNode = NULL;
while (CurrentNode != NULL)
{
NextNode = CurrentNode->Next;
delete CurrentNode;
CurrentNode = NextNode;
}
Head = Tail = NULL;
}
istream& operator>>(istream& in, Polynomial& P)
{
int num;
cout << "Please enter number of nodes\n";
cin >> num;
int c, p;
for (int i = 0; i < num; i++)
{
cout << "Please enter the pair of (coefficient,power)\n";
in >> c >> p;
P.insert(c, p);
}
return in;
}
ostream& operator<<(ostream& out, const Polynomial& P)
{
Node* temporary = P.Head;
if (temporary == NULL)
{
out << "Empty polynomial\n";
return out;
}
else {
out << "Polynomial is:\n";
while (temporary != NULL) {
if (temporary->power == 0)
out << temporary->coefficient << " + ";
else if (temporary->Next == NULL)
out << temporary->coefficient << "x^" << temporary->power;
else
out << temporary->coefficient << "x^" << temporary->power << " + ";
temporary = temporary->Next;
}
}
out << endl;
return out;
}
//Polynomial operator+(Polynomial& p1, Polynomial& p2)
Polynomial Polynomial::operator+(Polynomial O2)
{
Polynomial SumOfPolies;
Node* temporary = Head;
while (temporary != NULL) {
SumOfPolies.insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
temporary = O2.Head;
while (temporary != NULL) {
SumOfPolies.insert(temporary->coefficient, temporary->power);
temporary = temporary->Next;
}
return SumOfPolies;
}
and this is the main
#include <iostream>
#include "Polynomial.h"
using namespace std;
int main()
{
int X[] = { 2,4,1,3,5 };
int Z[] = { 1,2,0,3,4 };
Polynomial F1(X, Z, 5);
cout << F1 << endl;
cout << F1.degree() << endl;
Polynomial F2;
F2.insert(3, 2);
F2.insert(1, 6);
F2.insert(2, 7);
cout << F2 << endl;
Polynomial F3;
cout << "Here\n";
F3 = F1 + F2;
cout << "Here\n";
cout << F3;
return 0;
}
Upvotes: 0
Views: 83
Reputation: 9571
IMHO it's the time you should learn using debugger.
But if I had to guess I would say the most suspicious part I see in your code so far is this instruction:
return SumOfPolies;
in Polynomial Polynomial::operator+(Polynomial O2)
.
With this return
you cause the program to make a copy of a local variable SumOfPolies
. However, you did not provide a copy constructor to the Polynomial
class, so compiler creates a default copy member-by-member. This means a copied object, which shall be returned, shares the same list of Node
s with SumOfPolies
. But SumOfPolies
will be destroyed immediately after, because this is just return from the method – and in the destructor if will delete all its Node
s thus invalidating the Head
and Tail
pointers in the returned copy.
There are other mistakes and imperfections, unrelated to what you experience now.
For example, you create a new Node
object in the insert
method even though if may be unnecessary if power1 == Head->power
– but you do not delete it, and the abandoned Node
object remains in memory for ever (or rather until your program terminates).
You mix NULL
and nullptr
.
You also declared some variables where they're not necessary. For example in removeAll()
the NextNode
is not necessary at the function's scope, it may well be defined within the loop. You can drop the CurrentNode
as well:
void Polynomial::removeAll()
{
while (Head != NULL)
{
Node* NextNode = Head->Next;
delete Head;
Head = NextNode;
}
Tail = NULL;
}
You may also drop using the Tail
member. As far as I can see you need to initialize or modify it in seven places of your program, but you use it just in two—and you can easily get rid of it in both of them!
remove(int pw)
where you compare Tail
to Head
to check if the list is a single node only. Note this can be accomplished just by testing if Head->Next == NULL
.degree()
, where you need to access the highest power node. But you could test the Head
node instead if only you reverse the order in which powers are stored.<
and two >
operators.Upvotes: 2
Reputation: 16318
You need a copy constructor that looks like this:
Polynomial::Polynomial(Polynomial const& other)
{
Node* head = other.Head;
while (head != nullptr)
{
insert(head->coefficient, head->power);
head = head->Next;
}
}
When you provide a user-defined implementation of it, you should also implement the copy assignment operator, the move constructor and the move assignment operator. See the Rule of Five and the Rule of Zero: https://en.cppreference.com/w/cpp/language/rule_of_three.
Upvotes: 0