Advertisement
Guest User

Untitled

a guest
Nov 18th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3. #ifndef Poly
  4. #define Poly
  5.  
  6. using namespace std;
  7.  
  8. class poly {
  9. private:
  10. vector <double> tab;
  11. public:
  12. friend ostream& operator<<(ostream&, poly);
  13. friend poly operator+(poly p1, poly p2);
  14. friend poly operator*(poly p1, poly p2);
  15. double operator()(double x);
  16. double& operator[](int x);
  17. poly(double x = 0) {
  18. tab.push_back(x);
  19. }
  20.  
  21. };
  22.  
  23. #endif
  24.  
  25.  
  26.  
  27.  
  28. #include <iostream>
  29. #include<cmath>
  30. #include "poly.h"
  31.  
  32. using namespace std;
  33.  
  34. double poly::operator()(double x) {
  35. double res=0;
  36. unsigned int power=0;
  37. int i;
  38. for(i=0;i<tab.size();i++){
  39. res+=tab[i]*pow(x,power);
  40. power++;
  41. }
  42. return res;
  43. }
  44.  
  45. double& poly::operator[](int x) {
  46. if (tab.size() < x) tab.resize(x+1 ,0);
  47.  
  48. return tab[x];
  49. }
  50.  
  51.  
  52. poly operator+(poly p1,poly p2){
  53. poly tmp;
  54. (p1.tab.size() > p2.tab.size()) ? tmp.tab.resize(p1.tab.size(), 0) : tmp.tab.resize(p2.tab.size(),0);
  55. int i;
  56.  
  57. for (i = 0;i < tmp.tab.size();i++) {
  58. if (p1.tab.size() > i&&p2.tab.size() > i)tmp[i] = p1[i] + p2[i];
  59. else if (p1.tab.size() > i&&p2.tab.size() <= i)tmp[i] = p1[i];
  60. else tmp[i] = p2[i];
  61. }
  62. return tmp;
  63. }
  64.  
  65. poly operator*(poly p1, poly p2){
  66. poly tmp;
  67. tmp.tab.resize(p1.tab.size() + p2.tab.size(), 0);
  68. int i,j;
  69. for (i = 0;i < p1.tab.size();i++) {
  70. if (p1[i] != 0) {
  71. for (j = 0;j < p2.tab.size();j++) {
  72. tmp[i + j] += p1[i] * p2[j];
  73. }
  74. }
  75. }
  76. return tmp;
  77. }
  78.  
  79. ostream& operator<<(ostream &Out, poly p2) {
  80. int i;
  81. for (i = p2.tab.size()-1;i >=0;i--) {
  82.  
  83. if(p2[i]!=0){
  84. if(p2[i]!=1)Out << p2[i];
  85. if(i>0)Out<<"x";
  86. if(i>1)Out<<"^"<<i;
  87. if (i - 1 !=-1 && p2[i - 1] >= 0) Out << " + ";
  88. }
  89. }
  90. return Out;
  91. }
  92.  
  93.  
  94. //rule of 3 c++
  95.  
  96. int main(void)
  97. {
  98. poly P1; //Declare object representing polynomial P1
  99.  
  100. P1[3] = 2; P1[1] = 3.6; P1[0] = 7; //Specify coefficients of P1 = 2x^3 + 3.6x + 7
  101.  
  102. poly P2=5; //Declare object representing polynomial P2 = 5
  103. P2[1] = 3; P2[2] = 6; P2[4] = 1; //Specify additional coefficients of P2 = x^4 + 6x^2 + 3x + 5
  104.  
  105. cout << "Polynomial P1: " << P1 << endl; //Print P1
  106. cout << "Polynomial P2: " << P2 << endl; //Print P2
  107.  
  108. poly P3 = P1 + P2; //Add P1 and P2
  109. cout << "Sum of polynomials P1 and P2: " << P3 << endl; //Print sum of P1 and P2
  110.  
  111. P3 = P1 * P2; //Multiply P1 by P2
  112. cout << "Product of polynomials P1 and P2: " << P3 << endl; //Print product of P1 and P2
  113.  
  114. P3 = 2 * P1; //Multiply P1 by 2
  115. cout << "Polynomial P1 multiplied by 2: " << P3 << endl; //Print product of P1 and 2
  116.  
  117. double val = P1(3.14); //Calculate the value of P1 at point 3.14
  118. cout << "Value of polynomial P1 at point 3.14: " << val << endl; //Print the value of P1 at point 3.14
  119.  
  120. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement