c polynomial c polynomial I keep getting error when complie

c++ polynomial

c++ polynomial

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

polynomial.h

#ifndef POLYNOMIAL_H
#define POLYNOMIAL_H


#include
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
#include
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 {
resultCoeff[i] = 0;
}
//cout<

for(int i = 0; i<=degree; i++)
{
for(int j = 0; j<=p.degree(); j++)
{
resultCoeff[i+j] += (p.expr[j]*expr[i]);
//cout< //cout< //cout< //cout< }
}
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
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;
}

Solution

You may refer these files:

polynomial.hxx

#ifndef __POLYNOMIAL_HXX   // Control inclusion of header files
#define __POLYNOMIAL_HXX

/************ C++ Headers ************************************/
#include <iostream>   // Defines istream & ostream for IO
#include <vector>
using namespace std;

/************ Project Headers ********************************/
#include \"Fraction.hxx\"

/************ CLASS Declaration ******************************/
template<
   class T,   // Type of Value
   class U>   // Type of Coefficients
class Poly {

public:

   // CONSTRUCTOR
   // -----------
   Poly(unsigned int = 0);   // Uses default parameters. Overloads to
                           // Poly(...);

   // Copy Constructor
   Poly(const Poly&);   // Param cannot be changed (const)

   // DESTRUCTOR
   // ----------
   ~Poly() {}   // No virtual destructor needed

   // BASIC ASSIGNEMENT OPERATOR
   // --------------------------
   Poly& operator=(const Poly&);

   // UNARY ARITHMETIC OPERATORS
   // --------------------------
   Poly operator-();       // Operand \'this\' implicit
   Poly operator+();

   // BINARY ARITHMETIC OPERATORS
   // ---------------------------
   Poly operator+(const Poly&);
   Poly operator-(const Poly&);

   // ADVANCED ASSIGNEMENT OPERATORS
   // ------------------------------
   Poly& operator+=(const Poly&);
   Poly& operator-=(const Poly&);

   // BASIC I/O using FRIEND FUNCTIONS
   // --------------------------------
   template<class T, class U>
   friend ostream& operator<<(ostream& os, const Poly<T, U>& p);

   template<class T, class U>
   friend istream& operator>>(istream& is, Poly<T, U>& p);

   // METHODS
   // -------

   T Evaluate(const T&); // Evaluates the polynomial - use Horner\'s Rule

private:

   // DATA MEMBERS
   // ------------
   unsigned int   degree_;
   vector<U>       coefficients_;
};

#include \"Polynomial.inl\"

#endif // __POLYNOMIAL_HXX

************************************************************************************************

polynomial.inl

#include <iostream>
#include<math.h>

#include \"Polynomial.hxx\"
#include \"Fraction.hxx\"

template<class T, class U>
Poly<T,U>::Poly(unsigned int a):
degree_(a) {
   this->coefficients_.resize(a + 1);
}

template<class T, class U>
Poly<T,U>::Poly(const Poly& p)
{
   degree_ = p.degree_;
   this->coefficients_.resize(degree_ + 1);
   for (int i = 0; i <= (int)degree_;i++)
   this->coefficients_[i] = p.coefficients_[i];

}
template<class T, class U>
Poly<T,U>& Poly<T, U>::operator=(const Poly& p){
   int a;
   this->degree_ = p.degree_;
   a = (int)this->degree_;
   this->coefficients_.clear();
   this->coefficients_.resize(a + 1);
   for (int i = 0; i <= (int)degree_; i++)
           this->coefficients_[i] = p.coefficients_[i];
   return *this;

}

template<class T, class U>
Poly<T, U> Poly<T, U>::operator-(){
   unsigned int a = this->degree_;
   Poly<T,U> p(a);
  
   for (int i = 0; i <= (int)degree_; i++)
   {
       p.coefficients_[i] = (-1)*this->coefficients_[i];
      
   }
  
   return p;
  

}

template<class T, class U>
Poly<T, U> Poly<T, U>::operator+(){
   return *this;
  
}

template<class T, class U>
Poly<T, U> Poly<T, U>::operator+(const Poly& p){
   int d1, d2, degree, max, min;
   d1 = this->degree_;
   d2 = p.degree_;
   max = degree = d1 > d2 ? d1:d2;
   min = d1 < d2 ? d1:d2;
   Poly<T, U> pans(this->degree_);
   for (int i = 0; i <= degree; i++){
       if (i < max - min)
       {
           if (max == d1)
               pans.coefficients_[i] = this->coefficients_[i];
           else
               pans.coefficients_[i] = p.coefficients_[i];
       }
       else{
           if (max == d1)
               pans.coefficients_[i] = this->coefficients_[i] + p.coefficients_[i - max + min];
           else
               pans.coefficients_[i] = p.coefficients_[i] + this->coefficients_[i - max + min];

       }

   }
   return pans;
}

   template<class T, class U>
   Poly<T, U> Poly<T, U>::operator-(const Poly& p){
       int d1, d2, degree, max, min;
       d1 = this->degree_;
       d2 = p.degree_;
       degree = d1 > d2 ?d1:d2;
       max = degree;
       min = d1 < d2 ? d1:d2;
       Poly<T, U> pans(this->degree_);
       for (int i = 0; i <= degree; i++){
           if (i < max - min)
           {
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i];
               else
                   pans.coefficients_[i] = (-1)*p.coefficients_[i];
           }
           else{
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i] - p.coefficients_[i - max + min];
               else
                   pans.coefficients_[i] = this->coefficients_[i - max + min] - p.coefficients_[i];

           }

       }
       return pans;
}

   template<class T, class U>
   Poly<T,U>& Poly<T,U>::operator+=(const Poly<T,U>& p){
       int d1, d2, degree, max, min;
       d1 = this->degree_;
       d2 = p.degree_;
       max = degree = d1 > d2 ? d1 : d2;
       min = d1 < d2 ? d1 : d2;
       Poly<T, U> pans(this->degree_);
       for (int i = 0; i <= degree; i++){
           if (i < max - min)
           {
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i];
               else
                   pans.coefficients_[i] = p.coefficients_[i];
           }
           else{
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i] + p.coefficients_[i - max + min];
               else
                   pans.coefficients_[i] = p.coefficients_[i] + this->coefficients_[i - max + min];

           }

       }
       this->coefficients_.clear();
       this->coefficients_.resize(max + 1);
       for (int i = 0; i <= max; i++)
       {
           this->coefficients_[i] = pans.coefficients_[i];
       }
       return *this;

   }

  

   template<class T, class U>
   Poly<T, U>& Poly<T, U>::operator-=(const Poly<T, U>& p){
       int d1, d2, degree, max, min;
       d1 = this->degree_;
       d2 = p.degree_;
       degree = d1 > d2 ? d1 : d2;
       max = degree;
       min = d1 < d2 ? d1 : d2;
       Poly<T, U> pans(this->degree_);
       for (int i = 0; i <= degree; i++){
           if (i < max - min)
           {
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i];
               else
                   pans.coefficients_[i] = (-1)*p.coefficients_[i];
           }
           else{
               if (max == d1)
                   pans.coefficients_[i] = this->coefficients_[i] - p.coefficients_[i - max + min];
               else
                   pans.coefficients_[i] = this->coefficients_[i - max + min] - p.coefficients_[i];

           }
          
       }
      
       this->coefficients_.clear();
       this->coefficients_.resize(degree + 1);
       for (int i = 0; i <= max; i++)
       {   
           this->coefficients_[i] = pans.coefficients_[i];
       }
       return *this;

   }

   template<class T, class U>
   ostream& operator<<(ostream& os, const Poly<T, U>& p){
      
       for (int i = 0; i <= (int)p.degree_; i++)
       {
      
          
           if (i == p.degree_){
              
               if ((U)p.coefficients_[i] < 0){
                   os << \"-\" << -(U)p.coefficients_[i];
                  
               }
               else
                   os << \"+\"<<(U)p.coefficients_[i];

           }
           else
           {
              
               if ((U)p.coefficients_[i]<0 && i>0)
               {
                  
                   os << \"-\" << -(U)p.coefficients_[i] << \"x^\" << p.degree_ - i;
               }if ((U)p.coefficients_[i]>0 && i > 0)
                   os << \"+\" << p.coefficients_[i] << \"x^\" << p.degree_ - i;
               if (i==0)
                   os << p.coefficients_[i] << \"x^\" << p.degree_ - i;
      
           }
  
       }
       return os;
   }
   template<class T, class U>
   istream& operator>>(istream& is, Poly<T, U>& p){
  
       for (int i = 0; i <= (int)p.degree_; i++)
       {
           cout << \"Enter coefficient of x^\" << (p.degree_ - i) << endl;
           is >> p.coefficients_[i];
       }

       return is;
  
   }

   template<class T,class U>
   T Poly<T, U>::Evaluate(const T& x){
       int i;
       T res = 0;
       int s = this->degree_;
       for (i = s; i >= 0; i--)
       {
           res = res * x + this->coefficients_[s-i];
       }
       return res;
   }

*******************************************************************************************

testpoly.cxx

#include <iostream>
using namespace std;

#include \"Fraction.hxx\"
#include \"Polynomial.hxx\"

void main()
{
   cout << \"\ Test Poly Data Type\" << endl;

   // Polynomial with int value and int coefficients
   Poly<int, int> p(3);
  
   cout << \"Input Poly<int, int>: p(x)\" << endl;
   cin >> p;
   cout << \"\ p(x) = \" << p << endl;
  
   int x = 5;
   cout << \"p(\" << x << \") = \" << p.Evaluate(5) << endl;
  
   Poly<int, int> q = p;
   cout << \"Copied Polynomial: \" << q << endl;
  
   Poly<int, int> r;
   r = p;
   cout << \"Assigned Polynomial: \" << r << endl;
  
   r = -p;
   cout << \"Negated Polynomial -p(x) = \" << r << endl;
  
   cout << \"Input Poly<int, int>: q(x)\" << endl;
   cin >> q;
   cout << \"\ q(x) = \" << q << endl;
  
   r = p + q;
   cout << \"p(x) + q(x) = \" << r << endl;
  
   r = p - q;
   cout << \"p(x) - q(x) = \" << r << endl;
  
   p += q;
   cout << \"p(x) <-- p(x) + q(x): \" << p << endl;
  
   q -= p;
   cout << \"q(x) <-- q(x) - p(x): \" << q << endl;
  
   // Polynomial with Fraction value and int coefficients
   Poly<Fraction, int> pFi(3);

   cout << \"Input Poly<Fraction, int>: pFi(x)\" << endl;
   cin >> pFi;
   cout << \"pFi(x) = \" << pFi << endl;
  
   Fraction f;
   cout << \"Input Fraction\" << endl;
   cin >> f;
   cout << \"At \" << f << \": \" << pFi.Evaluate(f) << endl;
  
   // Polynomial with Fraction value and Fraction coefficients
   Poly<Fraction, Fraction> piF(3);

   cout << \"Input Poly<Fraction, Fraction>: piF(x)\" << endl;
   cin >> piF;
   cout << \"piF(x) = \" << piF << endl;

   cout << \"At \" << f << \": \" << piF.Evaluate(f) << endl;
  
   cin.get();
   cin.get();
   return;
}


Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site