Advertisement
jbonanno

polynomialTypeImp.cpp

Jul 11th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1.  
  2. #include "polynomialType.h"
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. polynomialType::polynomialType(int size)
  7.           : arrayListType<double>(size)
  8. {
  9.     length = size;
  10.     for(int i = 0; i < size; i++)
  11.         list[i] = 0;
  12. }
  13.  
  14. double polynomialType::operator() (double x)
  15. {
  16.     double value = 0.0;
  17.  
  18.     for(int i = 0; i < length; i++)
  19.     {
  20.         if(list[i] != 0.0)
  21.             value = value + list[i] * pow(x,i);
  22.     }
  23.  
  24.     return value;
  25. }
  26.  
  27. polynomialType polynomialType::operator+
  28.                (const polynomialType& right)
  29. {
  30.     int size = max(length, right.length);
  31.     int i;
  32.  
  33.     polynomialType temp(size);
  34.  
  35.     for(i = 0; i < min(length, right.length); i++)
  36.         temp.list[i] = list[i] + right.list[i];
  37.  
  38.     if(size == length)
  39.         for(i = min(length, right.length); i < length; i++)
  40.             temp.list[i] = list[i];
  41.     else
  42.         for(i = min(length, right.length); i < right.length; i++)
  43.             temp.list[i] = right.list[i];
  44.  
  45.     return temp;
  46.  
  47. }
  48.  
  49. polynomialType polynomialType::operator-
  50.                (const polynomialType& right)
  51. {
  52.     int size = max(length, right.length);
  53.     int i;
  54.  
  55.     polynomialType temp(size);
  56.  
  57.     for(i = 0; i < min(length, right.length); i++)
  58.         temp.list[i] = list[i] - right.list[i];
  59.  
  60.     if(size == length)
  61.         for(i = min(length, right.length); i < length; i++)
  62.             temp.list[i] = list[i];
  63.     else
  64.         for(i = min(length, right.length); i < right.length; i++)
  65.             temp.list[i] = -right.list[i];
  66.  
  67.     return temp;
  68.  
  69. }
  70.  
  71. polynomialType polynomialType::operator*
  72.                (const polynomialType& right)
  73. {
  74.  
  75. int m;
  76. int i;
  77. int arraySize;
  78. arraySize = (right.length +length) - 1;
  79.  
  80.  
  81. polynomialType temp(arraySize);
  82.  
  83. for(m = 0; m < arraySize; m++)
  84.     {
  85.     temp.list[m] = 0;
  86.  
  87.     for(i = 0; i <= min(m, length); i++)
  88.     {
  89.     if(length > i && right.length > (m-i))
  90.      temp.list[m] += [{right.list[m - i]*list[i])];
  91.         }
  92.  
  93.         }
  94.  
  95.     return temp;
  96.  
  97. }
  98.  
  99. int polynomialType::min(int x, int y)
  100. {
  101.     if(x <= y)
  102.         return x;
  103.     else
  104.         return y;
  105. }
  106.  
  107. int polynomialType::max(int x, int y)
  108. {
  109.     if(x >= y)
  110.         return x;
  111.     else
  112.         return y;
  113. }
  114.  
  115. ostream& operator<<(ostream& os, const polynomialType& p)
  116. {
  117.     int i;
  118.     int indexFirstNonzeroCoeff = 0;
  119.  
  120.     for(i = 0; i < p.length; i++)
  121.         if(p.list[i] != 0.0)
  122.         {
  123.             indexFirstNonzeroCoeff = i;
  124.             break;
  125.         }
  126.  
  127.     if(indexFirstNonzeroCoeff < p.length)
  128.     {
  129.         if(indexFirstNonzeroCoeff == 0)
  130.             os<<p.list[indexFirstNonzeroCoeff]<<" ";
  131.         else
  132.             os<<p.list[indexFirstNonzeroCoeff]<<"x^"
  133.               <<indexFirstNonzeroCoeff<<" ";
  134.  
  135.         for(i = indexFirstNonzeroCoeff + 1; i < p.length; i++)
  136.         {
  137.             if(p.list[i] != 0.0)
  138.                 if(p.list[i] >= 0.0)
  139.                     os<<"+ "<<p.list[i]
  140.                         <<"x^"<<i<<" ";
  141.                 else
  142.                     os<<"- "<<-p.list[i]
  143.                         <<"x^"<<i<<" ";
  144.         }
  145.     }
  146.     else
  147.         os<<"0";
  148.  
  149.     return os;
  150. }
  151.  
  152. istream& operator>>(istream& is, polynomialType& p)
  153. {
  154.     cout<<"The degree of this polynomial is: "
  155.         <<p.length - 1<<endl;
  156.     for(int i = 0; i < p.length; i++)
  157.     {
  158.         cout<<"Enter the coefficient of x^"<<i<<": ";
  159.         is>>p.list[i];
  160.     }
  161.  
  162.     return is;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement