Guest User

Untitled

a guest
Mar 19th, 2019
64
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Polynomial
  7. {
  8. public:
  9.  
  10. vector <double> wspol;
  11.  
  12. public:
  13.  
  14. Polynomial(){};
  15.  
  16. Polynomial(double *tab, int size)
  17. {
  18. for(int i=0; i <size; ++i)
  19. wspol.push_back(*(tab+i));
  20. }
  21.  
  22.  
  23. int check_degree()
  24. {
  25. return wspol.size()-1;
  26. }
  27.  
  28. void add_zero(int num)
  29. {
  30. for(int i=0;i<num;i++)
  31. {
  32. this->wspol.insert(this->wspol.begin(),0);
  33.  
  34. }
  35.  
  36. }
  37.  
  38. void print()
  39. {
  40. for (int i=0;i<this->wspol.size();i++)
  41. cout<<this->wspol[i]<<" ";
  42. }
  43.  
  44.  
  45.  
  46. void add(Polynomial w1, Polynomial w2 )
  47. {
  48. Polynomial w3;
  49. if (w1.check_degree() == w2.check_degree())
  50. {
  51. for (int i = 0; i < w1.wspol.size(); i++)
  52. w3.wspol.push_back(w1.wspol[i] + w2.wspol[i]);
  53. }
  54. else if(w1.check_degree() > w2.check_degree())
  55. {
  56. int num=abs(w1.check_degree()-w2.check_degree());
  57. w2.add_zero(num);
  58. for (int i = 0; i < w1.wspol.size(); i++)
  59. w3.wspol.push_back(w1.wspol[i] + w2.wspol[i]);
  60. }
  61. else
  62. {
  63. int num=abs(w1.check_degree()-w2.check_degree());
  64. w1.add_zero(num);
  65. for (int i = 0; i < w1.wspol.size(); i++)
  66. w3.wspol.push_back(w1.wspol[i] + w2.wspol[i]);
  67. }
  68.  
  69. }
  70.  
  71.  
  72.  
  73.  
  74.  
  75. };
  76.  
  77.  
  78.  
  79. int main()
  80. {
  81. double tab1[]={7,3,0,1,0,1};
  82. double tab2[]={6,0,3,2,4,2};
  83. vector<double>vec1;
  84. int length1= sizeof(tab1)/sizeof(double);
  85. int length2= sizeof(tab1)/sizeof(double);
  86. Polynomial p1(tab1,length1),p2(tab2,length2),p3;
  87. p3.add(p1,p2);
  88.  
  89.  
  90.  
  91.  
  92. return 0;
  93. }
RAW Paste Data