Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class Polynomial
  8. {
  9. private:
  10.     unsigned int degree;
  11.     double* coefficients;
  12. public:
  13.     char a;
  14.     Polynomial(unsigned int n);
  15.     ~Polynomial();
  16.     void setCoefficient(unsigned int i, double value);
  17.     double getCoefficient(unsigned int i);
  18.     double value(double x);
  19.     friend ostream & operator<<(ostream & out, Polynomial & p)
  20.         {
  21.         out<<p.coefficients[p.degree]<<"x^"<<p.degree;
  22.         for(int k=p.degree-1;k>=0;k--)
  23.             {
  24.             if (k==1)
  25.                 {
  26.                 if(p.coefficients[k]>0)
  27.                     out<<"+";
  28.                 if (p.coefficients[k]!=0)
  29.                     {
  30.                     out<<p.coefficients[k];
  31.                     out<<"x";
  32.                     }
  33.                 }
  34.             else if(k==0)
  35.                {
  36.                 if (p.coefficients[k]!=0)
  37.                     {
  38.                     if(p.coefficients[k]>0)
  39.                         out<<"+";
  40.                     out<<p.coefficients[k];
  41.                     }
  42.                 else out<<"";
  43.                 }
  44.             else{
  45.  
  46.                 if (p.coefficients[k]!=0)
  47.                     {
  48.                     if(p.coefficients[k]>0)
  49.                         out<<"+";
  50.                     out<<p.coefficients[k]<<"x^"<<k;
  51.                     }
  52.                 }
  53.             }
  54.         }
  55. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement