Hello I had to do a Polynomial class code, But for some reason my code crash can
ID: 3537106 • Letter: H
Question
Hello I had to do a Polynomial class code, But for some reason my code crash can somebody helps me also multiplication does not work please help
#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H
class Polynomial
{
private:
int *coefficient;
int maxExponent;
public:
Polynomial();//default constructor
Polynomial(int *); //dummy constructor
~Polynomial();//destructor
Polynomial(const Polynomial &obj);//copy constructor
int getMaxExponent();
void setPolynomial();//?should I pass something
void printPolynomial()const;
Polynomial Polynomial::operator+(const Polynomial &obj);
Polynomial Polynomial::operator-(const Polynomial &obj);
Polynomial Polynomial::operator*(const Polynomial &obj);
const Polynomial Polynomial::operator=(const Polynomial &obj);
};
#endif
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include "Polynomial.h"
using namespace std;
Polynomial::Polynomial()//default constructor
{
coefficient = new int[1];
*coefficient = NULL;
maxExponent = 0;
}
Polynomial::~Polynomial()//destructor, free memory
{
delete [] coefficient;
}
Polynomial::Polynomial(const Polynomial &obj)//copy constructor
{
//delete [] coefficient;
maxExponent = obj.maxExponent;
coefficient = new int [maxExponent + 1];//coefficient = new int [maxExponent+1];
for (int i=0; i <= maxExponent; i++)// <=maxExponent
*(coefficient + i) = obj.coefficient[i];//*(obj.coefficient + i);
}
int Polynomial::getMaxExponent()
{
return maxExponent;
}
void Polynomial::setPolynomial()
{
cout << "Enter highest power for x: ";
cin >> maxExponent;
coefficient = new int[maxExponent+1];
for(int i=maxExponent; i>=0; i--)
{
cout << "Enter Co-efficient for " << i << ": ";
cin >> *(coefficient + i);
//coefficient[i];
}
}
//Do I need a getPolynomial
/**
void getPolynomial() const
{
for (int i = 0; i < maxExponent; i++)
{
cout << coefficient[i] << "x^" << i;
if (i != maxExponent - 1)
cout << " + ";
}
}
**/
void Polynomial::printPolynomial()const
{
bool termPrinted = false;
for (int i=0; i<=maxExponent; i++)//<=
{
if (coefficient[i]==0);
else if(!termPrinted)
{
if(coefficient[i]!=1)
cout << coefficient[i];
cout <<"x^"<< i;
termPrinted = true;
}
else if(coefficient[i]<0)
cout << " - " << abs(coefficient[i]) << "x^" << i;
else if(coefficient[i]==1)
cout << " + x^" << i;
else
cout << " + " << coefficient[i] << "x^" << i;
}
cout << endl;
}
const Polynomial Polynomial::operator=(const Polynomial &obj)
{;
delete [] coefficient;
maxExponent = obj.maxExponent;
coefficient = new int [maxExponent + 1];
for (int i=0; i<=maxExponent; i++)
*(coefficient + i) = obj.coefficient[i];//*(obj.coefficient + i);
return *this;
}
Polynomial Polynomial::operator+(const Polynomial &obj)
{
Polynomial temp;
int smallest, largest;
smallest = (maxExponent > obj.maxExponent) ? obj.maxExponent : maxExponent;
largest = (maxExponent > obj.maxExponent) ? maxExponent : obj.maxExponent;
//if (maxExponent == obj.maxExponent)
// largest = maxExponent;
//make coefficient array
temp.maxExponent = largest;
temp.coefficient = new int [largest];
for (int i = 0; i <= smallest; i++)
temp.coefficient[i] = coefficient[i] + obj.coefficient[i];
for (int j = smallest+1; j <= largest; j++)
{
if (maxExponent > obj.maxExponent)
temp.coefficient[j] = coefficient[j];
else
temp.coefficient[j] = obj.coefficient[j];
}
return temp;
}
Polynomial Polynomial::operator-(const Polynomial &obj)
{
Polynomial temp;
int smallest, largest;
smallest = (maxExponent > obj.maxExponent) ? obj.maxExponent : maxExponent;
largest = (maxExponent > obj.maxExponent) ? maxExponent : obj.maxExponent;
//if (maxExponent == obj.maxExponent)
// largest = maxExponent;
//make coefficient array
temp.maxExponent = largest;
temp.coefficient = new int [largest];
for (int i = 0; i <= smallest; i++)
temp.coefficient[i] = coefficient[i] - obj.coefficient[i];
for (int j = smallest+1; j < largest; j++)
{
if (maxExponent > obj.maxExponent)
temp.coefficient[j] = coefficient[j];
else
temp.coefficient[j] = obj.coefficient[j];
}
return temp;
}
Polynomial Polynomial::operator*(const Polynomial &obj)
{
Polynomial temp;
//make coefficient array
int count = (maxExponent + obj.maxExponent) - 1;
temp.maxExponent = count;
temp.coefficient = new int [count];
for(int i = 0; i <= maxExponent; i++)
{
for(int j = 0; j < obj.maxExponent; j++)
temp.coefficient[i+j] += coefficient[i] * obj.coefficient[j];
}
return temp;
}
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include "Polynomial.h"
using namespace std;
int main()
{
//int maxExponent;
//Polynomial one, two;
cout << "Please enter first Polynomial: ";
Polynomial one;
one.setPolynomial();
one.printPolynomial();
cout << "Please enter second Polynomial: ";
Polynomial two;
two.setPolynomial();
two.printPolynomial();
int choice;
do
{
cout << "1. Addition 2. Subtraction 3. Multiplication";
cout << " 4. Assignment 5. Exit Enter your choice: ";
cin >> choice;
switch (choice)
{
case 1:
{
cout << "Addition ";
Polynomial add;
add = one + two;
cout << " ";
one.printPolynomial();
cout << " + ";
two.printPolynomial();
cout << " = ";
add.printPolynomial();
cout << endl;
}
break;
case 2:
{
cout << "Subtraction ";
Polynomial subtract = one - two;
cout << " ";
one.printPolynomial();
cout << " - ";
two.printPolynomial();
cout << " = ";
subtract.printPolynomial();
cout << endl;
}
break;
case 3:
{
cout << "Multiplication ";
Polynomial multiply = one * two;
one.printPolynomial();
cout << " * (";
two.printPolynomial();
cout << ") = ";
multiply.printPolynomial();
cout << endl;
}
break;
case 4:
cout << "Assignment ";
cout << "First polynomial: ";
one.printPolynomial();
cout << " Second polynomial: ";
two.printPolynomial();
cout << " First = Second First polynomial is now: ";
> one.printPolynomial();
cout << endl;
break;
case 5:
cout << "Goodbye ";
break;
default:
cout << "Invalid choice ";
break;
}
}while (choice != 5);
system("pause");
return 0;
}
Explanation / Answer
too long homework dude increase the points
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.