Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

c++ polynomial I keep getting error when complie. please help. I use codeblock.

ID: 3565550 • Letter: C

Question

c++ polynomial

I keep getting error when complie. please help. I use codeblock.

polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


#include <iostream>
using namespace std;

class polynomial
{
public:
   friend ostream& operator<<(ostream& outfile, const polynomial&);
   friend polynomial operator+(int, const polynomial&);
   friend polynomial operator*(int, const polynomial&);
   friend polynomial operator-(int, const polynomial&);

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

   polynomial operator*(const polynomial&);
   polynomial operator*(int);
   polynomial operator+(const polynomial&);
   polynomial operator+(int);
   void operator=(const polynomial&);
   void operator=(int);
   polynomial operator-();
   polynomial operator-(const polynomial&);
   polynomial operator-(int);

private:
   int * expr;
   int degree;
};

#endif // POLYNOMIAL_H

___________________________________________________

polynomial.cpp

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

ostream& operator<<(ostream& outfile, const polynomial& p)
{
   for (int i = p.degree; i >= 0; i--)
   {
       if (i == p.degree && p.expr[i] < 0)
           outfile << "-";
       else if (i != p.degree)
       {
           if (p.expr[i] > 0)
               outfile << " + ";
           if (p.expr[i] < 0)
               outfile << " - ";
       }
       if (p.expr[i] != 0)
       {
           outfile << abs(p.expr[i]);
           if (i > 0)
               outfile << "x";
           if (i > 1)
               outfile << "^" << i;
       }
   }
   return outfile;
}

polynomial operator+(int n, const polynomial& p)
{

}

polynomial operator*(int n, const polynomial& p)
{

}

polynomial operator-(int n, const polynomial& p)
{

}

polynomial::polynomial()
{
   degree = 0;
   expr = NULL;
}

polynomial::polynomial(const polynomial& p)
{
expr = new int;
*expr = *(p.expr);
}


/**< Constructor will set the degree field and coefficient */
polynomial::polynomial(int* poly_expr, int d)
{
expr = new int [d];
for (int i = 0; i < d; i++)
expr[i] = *poly_expr++;
expr[d] = *poly_expr;
degree = d;
}

/**< Deconstruction */
polynomial::~polynomial()
{
cout << "deconstruction" << endl;
/*if (expr != NULL)
(delete [] expr);
//expr = NULL
*/
}

polynomial polynomial::operator*(const polynomial& p)
{

int newDeg = degree + p.degree();
int resultCoeff[degree];
for(int i = 0; i<degree; i++)
{
resultCoeff[i] = 0;
}
//cout<<resultCoeff[5]<<endl;

for(int i = 0; i<=degree; i++)
{
for(int j = 0; j<=p.degree(); j++)
{
resultCoeff[i+j] += (p.expr[j]*expr[i]);
//cout<<i+j<<endl;
//cout<<p.getCoeff(j)<<endl;
//cout<<coefficient[i]<<endl;
//cout<<resultCoeff[i+j]<<endl;
}
}
return polynomial(newDeg,resultCoeff);

}
/*
polynomial polynomial::operator*(int n)
{

}

polynomial polynomial::operator+(const polynomial& p)
{

}

polynomial polynomial::operator+(int n)
{

}
*/
void polynomial::operator=(const polynomial& p)
{

}
/*
void polynomial::operator=(int n)
{

}

polynomial polynomial::operator-()
{

}

polynomial polynomial::operator-(const polynomial& p)
{

}

polynomial polynomial::operator-(int n)
{

}

*/

______________________________________________

main.cpp

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

int main()
{
   int poly1[] = {-2,4,6};
   int poly2[] = {1,3,-5};
   int poly3[] = {3, 0, 4, 0, 5};

   polynomial p1(poly1, 2);
   polynomial p2(poly2, 2);
   polynomial p3(poly3, 4);
   polynomial result;


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

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

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

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

   result = p1 * p3;
   cout << "p1 * p3 = " << result << endl;

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

   result = p2 * 6;
   cout << "p2 * 6 = " << result << endl;

   result = 4 + p3;
   cout << "4 + p3 = " << result << endl;

   result = 3 * p1;
   cout << "3 * p1 = " << result << endl;

   result = 5 - p2;
   cout << "5 - p2 = " << result << endl;

   result = 10;
   cout << "result = 10: " << result << endl << endl;

*/
   return 0;
}

Explanation / Answer

#include #include //using namespace std; //A polynomial represented as a singly linked list struct term { int exponent; double coefficient; term* next; term(int e, double c, term* n = 0) : exponent(e), coefficient(c), next(n) { } void print() { cout next; } reverse(); } void polynomial::add_term(term*& a, term*& b) { term* c; if (a -> exponent > b -> exponent) { //add a c = new term(a -> exponent, a -> coefficient) ; assert(c != 0); a = a -> next; prepend(c); } else if (a -> exponent exponent){ //add b c = new term(b -> exponent, b -> coefficient); assert(c != 0); b = b -> next; prepend(c); } else { //check on cancellation if (a -> coefficient + b -> coefficient != 0) { c = new term( a -> exponent, a -> coefficient + b -> coefficient); assert(c != 0); prepend(c); } a = a -> next; b = b -> next; } } void polynomial::rest_of(term* rest) { term* temp; while (rest) { temp = new term(rest -> exponent, rest -> coefficient); assert(temp != 0); prepend(temp); rest = rest -> next; } } void polynomial::print() const { term* temp = h; if (h == 0) { cout print(); temp = temp -> next; } cout h; degree = temp -> degree; } return *this; } int main() { double coef[4] = {1, 2, 3, 4}; double coef2[4] = {-1,-2, -3, -4}; double coef3[4] = {7, 3, -4, 7}; int expo[4] = {0, 4, 14, 45}; int expo2[4] = { 5, 6, 14, 45}; polynomial p(4, coef2, expo), q(4, coef, expo), r(4, coef3, expo2); cout
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote