2 Replies - 2247 Views - Last Post: 21 August 2010 - 05:52 AM Rate Topic: -----

#1 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Polynomials with rational coefficients...

Posted 20 August 2010 - 03:05 PM

Good evening!

I am currently working with a program that does basic arithmetic with polynomials that can have rational coefficients. I was able to come up with seperate classes and implementation files for my two classes, Polynomial.h and Rational.h. I can run two different programs, one for each class, fine. I am trying to figure out how to use both classes in order to handle the rational coefficients.

The related files:


// Polynomial.h
//#include "Rational.h"
class Polynomial
{
 public:
        static const int maxTerms = 10; // maximum number of terms
        Polynomial(); // constructor
        Polynomial operator+(const Polynomial &) const; // addition
        Polynomial operator-(const Polynomial &) const; // subtraction
        Polynomial operator*(const Polynomial &) const; // multiplication
        Polynomial &operator=(const Polynomial &); // assignment
        Polynomial &operator+=(const Polynomial &);
        Polynomial &operator-=(const Polynomial &);
        Polynomial &operator*=(const Polynomial &);
        void enterTerms();
        void printPolynomial() const;
        int getNumTerms() const;
        int getTermExp(int) const;
        int getTermCoeff(int) const;
        void setCoeff(int, int);
        ~Polynomial(); // destructor
        
 private:
        int numTerms;
        int exp[maxTerms]; // exponent array
        int coeff[maxTerms]; // coefficients array
        static void polyCombine(Polynomial &); // combine commn terms
}; // end class Polynomial

=====================================================================

// Polynomial.cpp
#include <iostream>
#include <iomanip>
#include "Polynomial.h"
//#include "Rational.h"
using namespace std;

Polynomial::Polynomial()
{
   for(int t = 0; t < maxTerms; t++)
   {
      coeff[t] = 0;
      exp[t] = 0;
   } // end for
   numTerms = 0;
} // end Polynomial constructor

Polynomial Polynomial::operator+(const Polynomial &r) const // addition
{
   Polynomial temp;
   bool expExists;
   int s;
   
   // process element with a zero exponent
   temp.coeff[0] = coeff[0] + r.coeff[0];
   
   // copy right arrays into temp object; s will be used to keep
   // track of first open coefficient element
   for(s = 1; (s < maxTerms) && (r.exp[s] != 0); s++)
   {
      temp.coeff[s] = r.coeff[s];
      temp.exp[s] = r.exp[s];
   } // end for
   for(int x = 1; x < maxTerms; x++)
   {
      expExists = false; // assume exponent will not be found
      
      for(int t = 1; (t < maxTerms) && (!expExists); t++)
        if(exp[x] == temp.exp[t])
        {
           temp.coeff[t] += coeff[x];
           expExists = true; // exponent found
        } // end if
      // exponent was found, insert into temp
      if(!expExists)
      {
         temp.exp[s] = exp[x];
         temp.coeff[s] += coeff[x];
         s++;
      } // end if
   } // endfor
   return temp;
} // end operator+

Polynomial Polynomial::operator-(const Polynomial &r) const // subtraction
{
   Polynomial temp;
   bool expExists;
   int s;
   
   // process element with zero exponent
   temp.coeff[0] = coeff[0] - r.coeff[0];
   
   // copy left arrays into temp object; s will be used to keep
   // track of first open coefficient element
   for(s = 1; (s < maxTerms) && (exp[s] != 0); s++)
   {
      temp.coeff[s] = coeff[s];
      temp.exp[s] = exp[s];
   } // end for
   for (int x = 1; x < maxTerms; x++)
   {
      expExists = false;
      
      for(int t = 1; (t < maxTerms) && (!expExists); t++)
      
        if(r.exp[x] == temp.exp[t])
        {
           temp.coeff[t] -= r.coeff[x];
           expExists = true;
        }
      if(!expExists)
      {
         temp.exp[s] = r.exp[x];
         temp.coeff[s] -= r.coeff[x];
         s++;
      } // end if
   } // end for
} // end operator- function

Polynomial Polynomial::operator*(const Polynomial &r) const // multiplication
{
   Polynomial temp;
   int s = 1;
   
   for(int x = 0; (x < maxTerms) && (x == 0 || coeff[x] != 0); x++)
   
           for(int y = 0; (y < maxTerms) && (y == 0 || r.coeff[y] != 0); y ++)
           
                   if(coeff[x] * r.coeff[y])
                   
                     if((exp[x] == 0) && (r.exp[y] == 0))
                       temp.coeff[0] += coeff[x] * r.coeff[y];
                     else
                     {
                        temp.coeff[s] = coeff[x] * r.coeff[y];
                        temp.exp[s] = exp[x] + r.exp[y];
                        s++;
                     } // end else
   polyCombine(temp); // combine common terms
   return temp;
} // end operator* function


Polynomial &Polynomial::operator=(const Polynomial &r) // assignment
{
   exp[0] = r.exp[0];
   coeff[0] = r.coeff[0];
   
   for(int s = 1; s < maxTerms; s++)
   {
      if(r.exp[s] != 0)
      {
         exp[s] = r.exp[s];
         coeff[s] = r.coeff[s];
      }
      else
      {
         if(exp[s] == 0)
           break;
           
         exp[s] = 0;
         coeff[s] = 0;
      } // end else
   } // end for
   return *this;
} // end operator= function

Polynomial &Polynomial::operator+=(const Polynomial &r)
{
   *this = *this + r;
   return *this;
} // end operator+= function

Polynomial &Polynomial::operator-=(const Polynomial &r)
{
   *this = *this -r;
   return *this;
} // end operator-= function

Polynomial &Polynomial::operator*=(const Polynomial &r)
{
   *this = *this * r;
   return *this;
}// end operator*= function

void Polynomial::enterTerms()
{
   bool found = false;
   int c, e, term;
   
   cout << endl << "Enter number of polynomial terms: ";
   cin >> numTerms;
   
   for(int n = 0; n < maxTerms && n < numTerms; n++)
   {
      cout << endl << "Enter coefficient: ";
      cin >> c;
      cout << "Enter exponent: ";
      cin >> e;
      
      if(c != 0)
      {
         // exponent of zero are forced into first element
         if(e == 0)
         {
            coeff[0] += c;
            continue;
         } // end if
         for(term = 1; (term < maxTerms) && (coeff[term] !=0); term++)
         
           if(e == exp[term])
           {
              coeff[term] += c;
              exp[term] = e;
              found = true; // existing exponent updated
           } // end if
         if(!found) // add term
         {
            coeff[term] += c;
            exp[term] = e;
         } // end if
      } // end outer if
   } // end outer for
} // end enterTerms function

void Polynomial::printPolynomial() const
{
   int start;
   bool zero = false;
   
   if(coeff[0]) // output constraints
   {
      cout << coeff[0];
      start = 1;
      zero = true; // at least one term exists
   }
   else
   {
       if(coeff[1])
       {
          cout << coeff[1] << 'x';
          if((exp[1] != 0) && (exp[1] != 1))
            cout << '^' << exp[1];
          zero = true; // at least one term exists
       } // end if
       start = 2;
   } // end else
   
   // output remaining polynomial terms
   for(int x = start; x < maxTerms; x++)
   {
      if(coeff[x] != 0)
      {
         cout << showpos << coeff[x] << noshowpos << 'x';
         
         if((exp[x] != 0) && (exp[x] != 1))
           cout << '^' << exp[x];
         
         zero = true; // at least one term exists
      } // end if
   } // end for
   if(!zero) // no terms exist in the polynomial
     cout << '0';
     cout << endl;
} // end printPolynomial function

int Polynomial::getNumTerms() const
{
   return numTerms;     
} // end getTerms function

int Polynomial::getTermExp(int term) const
{
   return exp[term];
} // end getTermExp function

int Polynomial::getTermCoeff(int term) const
{
   return coeff[term];
} // end getTermCoeff function

void Polynomial::setCoeff(int term, int coeffs)
{
   if(coeff[term] == 0) // no term at this location
     cout << "No term at this location, cannot see term." << endl;
   else // otherwise set term
     coeff[term] = coeffs;
} // end setCoeff function

void Polynomial::polyCombine(Polynomial &w)
{
   Polynomial temp = w;
   
   // zero out elements of w
   for(int x = 0; x < maxTerms; x++)
   {
      w.coeff[x] = 0;
      w.exp[x] = 0;
   }
   for(int x = 1; x < maxTerms; x++)
   {
      for(int y = x + 1; y < maxTerms; y++)
        if(temp.exp[x] == temp.exp[y])
        {
           temp.coeff[x] += temp.coeff[y];
           temp.exp[y] = 0;
           temp.coeff[y] = 0;
        }
   }
   w = temp;
} // end polyCombine function

Polynomial::~Polynomial() // destructor
{
   // empty
}







// Rational.h
#include <iostream>
using namespace std;
class Rational
{
 private:
                int num;
                int denom;

public:
                void setNum(int);
                void setDenom(int);
                int getNum();
                int getDenom();

                int gcd();
                void simplify();

                Rational operator+(Rational);
                Rational operator-(Rational);
                Rational operator*(Rational);
                Rational operator/(Rational);
                bool operator==(Rational);
                bool operator!=(Rational);
                bool operator>(Rational);
                bool operator<(Rational);
                bool operator>=(Rational);
                bool operator<=(Rational);
};

=====================================================================

// Rational.cpp
#include <iostream>
#include "Rational.h"
using namespace std;

void Rational::setNum(int n)
{
   num = n;
}

void Rational::setDenom(int n)
{
   denom = n;
}

int Rational::getNum()
{
   return num;
}
int Rational::getDenom()
{
   return denom;
}

int Rational::gcd()
{
   int x = num;
   int y = denom;
   int temp;
 
   while (y)
   {
    temp = y;
    y = x % y;
    x = temp;
   }
   return x;
}

void Rational::simplify()
{
   int gcdNum = gcd();

   if(gcdNum != 0)
   {
    num = num / gcdNum;
    denom = denom / gcdNum;
   }
}

Rational Rational::operator+(Rational passed)
{   
   Rational Result;

   Result.num = num * passed.denom + denom * passed.num;
   Result.denom = denom * passed.denom;
   
   return Result;
}

Rational Rational::operator-(Rational passed)
{
   Rational Result;
   
   Result.num = num * passed.denom - denom * passed.num;
   Result.denom = denom * passed.denom;
   
   return Result;
}

Rational Rational::operator*(Rational passed)
{
   Rational Result;

   Result.num = num * passed.num;
   Result.denom = denom * passed.denom;
   
   return Result;
}


Rational Rational::operator/(Rational passed)
{
   Rational Result;
   Result.num = num * passed.denom;
   Result.denom = denom * passed.num;
   
   return Result;
}

bool Rational::operator==(Rational passed)
{
   simplify();
   passed.simplify();

   return (passed.num == num) && (passed.denom == denom);
}

bool Rational::operator!=(Rational passed)
{
   simplify();  
   passed.simplify();

   return (passed.num != num) && (passed.denom != denom);
}

bool Rational::operator>(Rational passed)
{

   simplify();
   passed.simplify();

   return (num * passed.denom) > (passed.num * denom);
}

bool Rational::operator<(Rational passed)
{
   simplify();
   passed.simplify();

   return (num * passed.denom) < (passed.num * denom);
}

bool Rational::operator>=(Rational passed)
{

   simplify();
   passed.simplify();

   return (num * passed.denom) >= (passed.num * denom);
}

bool Rational::operator<=(Rational passed)
{
   simplify();
   passed.simplify();

   return (num * passed.denom) <= (passed.num * denom);
}

=====================================================================





Suggestions?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Polynomials with rational coefficients...

#2 jimblumberg  Icon User is online

  • member icon

Reputation: 3055
  • View blog
  • Posts: 9,291
  • Joined: 25-December 09

Re: Polynomials with rational coefficients...

Posted 20 August 2010 - 07:31 PM

So what kind of problems are you having when trying to combine these classes?



Jim
Was This Post Helpful? 0
  • +
  • -

#3 DamienCurr  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 69
  • Joined: 03-May 09

Re: Polynomials with rational coefficients...

Posted 21 August 2010 - 05:52 AM

Good morning jimblumberg and thank you very much for the reply. The problem is I do not know where to begin. I know how to create a program that performs arithmetic operations on either rational numbers or polynomials with integer coefficients, but not polynomials with rational coefficients. What I am trying to figure out is if I need to make minor changes to the code I have or if I have to scrap everything and re-write from scratch.

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1