Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. class Polynomial
  6. {
  7. unsigned int degree;
  8. double *coefficients;
  9. public:
  10. Polynomial(unsigned int n)
  11. {
  12. coefficients=new double[n+1];
  13. degree=n;
  14. }
  15. ~Polynomial()
  16. {
  17. delete coefficients;
  18. }
  19. void Polynomial::setCoefficients(unsigned int i, double val)
  20. {
  21. coefficients[i]=val;
  22. }
  23. double Polynomial::getCoefficient(unsigned int i)
  24. {
  25. return coefficients[i];
  26. }
  27. double Polynomial::value(double y)
  28. {
  29. int suma=0;
  30. for(int k=degree; k>=0; k--)
  31. suma+=coefficients[k]*pow(y,k);
  32. return suma;
  33. }
  34. friend ostream & operator<<(ostream & out, Polynomial & p)
  35. {
  36. out<<"Twoj wielomian to:";
  37. if(p.coefficients[p.degree]!=1 && p.degree!=1)
  38. out<<p.coefficients[p.degree]<<"x^"<<p.degree;
  39. else if(p.degree!=1)
  40. out<<"x^"<<p.degree;
  41. if (p.degree==1){
  42. if(p.coefficients[p.degree]==0 && p.coefficients[p.degree-1]!=0)
  43. out<<p.coefficients[p.degree-1];
  44. else
  45. out<<"";
  46. if(p.coefficients[p.degree]!=1 && p.coefficients[p.degree]!=0)
  47. out<<p.coefficients[p.degree]<<"x";
  48. if(p.coefficients[p.degree-1]>0)
  49. out<<"+"<<p.coefficients[p.degree-1];
  50. if(p.coefficients[p.degree-1]<0 && p.coefficients[p.degree]!=0)
  51. out<<p.coefficients[p.degree-1];
  52. if(p.coefficients[p.degree-1]==0)
  53. out<<"";
  54.  
  55. }
  56. else
  57. for(int k=p.degree-1; k>=0; k--)
  58. {
  59. if(k==0 && p.coefficients[k]>0)
  60. {
  61. out<<"+"<<p.coefficients[k];
  62. }
  63. else if(k==1 && p.coefficients[k]!=0)
  64. {
  65. if(p.coefficients[k]!=1)
  66. out<<"+"<<p.coefficients[k]<<"x";
  67. else
  68. out<<"+x";
  69. }
  70. if(p.coefficients[k]==0)
  71. out<<"";
  72. if (p.coefficients[k]==1 && k!=0 && k!=1)
  73. out<<"+x^"<<k;
  74. if(p.coefficients[k]>0 && k!=0 && k!=1)
  75. out<<"+"<<p.coefficients[k]<<"x^"<<k;
  76. if(p.coefficients[k]<0&& k!=0)
  77. out<<""<<p.coefficients[k]<<"x^"<<k;
  78. if(p.coefficients[k]<0 && k==0)
  79. out<<p.coefficients[k];
  80.  
  81. }
  82. return out;
  83. }
  84.  
  85. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement