Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
150
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 "poly.h"
  3. #include <algorithm> //max min
  4. using namespace std;
  5. poly::poly() {
  6. this->wsp = new double[1];
  7. this->wsp[0] = 0.0;
  8. this->size = 1;
  9. }
  10.  
  11. poly::poly(double a) {
  12. this->wsp = new double[1];
  13. this->wsp[0] = a;
  14. this->size = 1;
  15. }
  16. bool operator==(const poly& a, const poly& b) {
  17. return (a.size == b.size && a.wsp == b.wsp);
  18. }
  19. poly& poly::operator=(const poly& a) {
  20. if (*this == a || a.wsp == NULL)
  21. return *this;
  22. delete[] wsp;
  23. this->wsp = new double[a.size];
  24. if (wsp == NULL)
  25. exit(2);
  26. size = a.size;
  27. memcpy(this->wsp, a.wsp, size * sizeof(double));
  28. return *this;
  29. }
  30. double& poly::operator[](int index) {
  31. if (index < size)
  32. return this->wsp[index];
  33. else {
  34. double * realok=new double[index+1];
  35. if (realok == NULL) {
  36. delete[] this->wsp;
  37. abort();
  38. }
  39. /*for (int i = 0; i < this->size; i++) {
  40. realok[i] = 0;
  41. }*/
  42. memcpy(realok, this->wsp,(this->size)*sizeof(double));
  43. /*for (int i = 0; i < this->size; i++) {
  44. realok[i] = this->wsp[i];
  45. }*/
  46. delete[] this->wsp;
  47. this->wsp = realok;
  48. this->size = index + 1;
  49. return this->wsp[index];
  50. }
  51. }
  52. poly operator+(const poly& a, const poly& b) {
  53. poly wynik;
  54. wynik.wsp = new double[max(a.size, b.size)];
  55. wynik.size = max(a.size, b.size);
  56. //wynik.wsp.resize(max(a.wsp.size(), b.wsp.size()), 0.0);
  57. auto i = 0;
  58. for (i = 0; i != max(a.size, b.size); i++) {
  59. wynik.wsp[i] = a.wsp[i] + b.wsp[i];
  60. }
  61. if (wynik.size == a.size) {
  62. for (auto j = i; j != a.size; j++)
  63. wynik.wsp[j] = a.wsp[j];
  64. }
  65. else {
  66. for (auto j = i; j != b.size; j++)
  67. wynik.wsp[j] = b.wsp[j];
  68. }
  69. return wynik;
  70. }
  71. poly operator*(const poly & a, const poly & b) {
  72. poly wynik;
  73. wynik.wsp = new double[a.size + b.size - 1];
  74. wynik.size = a.size + b.size - 1;
  75. for (auto i = 0; i != wynik.size; i++) wynik[i] = 0;
  76. for (auto i = 0; i < a.size; i++) {
  77. for (auto j = 0; j < b.size; j++) {
  78. wynik[i + j] += a.wsp[i] * b.wsp[j];
  79. }
  80. }
  81. return wynik;
  82. }
  83. poly operator*(const double& a, const poly & b) {
  84. poly wynik;
  85. wynik.wsp= new double[b.size];
  86. wynik.size = b.size;
  87. for (auto i = 0; i != b.size; i++) {
  88. wynik.wsp[i] = a * b.wsp[i];
  89. }
  90. return wynik;
  91. }
  92.  
  93. double poly::operator()(const double& a) {
  94. double suma = 0.0;
  95. for (auto i = size; i >= 0; i--) {
  96. suma += this->wsp[i];
  97. if (i - 1 != this->wsp[0])
  98. suma *= a;
  99. }
  100. return suma;
  101. }
  102. ostream& operator<<(ostream & s, const poly & a) {
  103. auto potega = a.size - 1;
  104. if (potega == -1) {
  105. s << 0;
  106. return s;
  107. }
  108. for (auto i = a.size-1; i != 0; i--, potega--) {
  109. double index = a.wsp[i];
  110. if (index != 0) {
  111. if (index > 0 && potega != a.size - 1)
  112. s << " + ";
  113. }
  114. if (potega == 0) s << index;
  115. else if (index == 1) s << "x";
  116. else if (index == -1) s << "-x";
  117. else if (index == 0) s << " + 0x";
  118. else s << index << "x";
  119. if (potega > 1) s << "^" << potega;
  120. }
  121. return s;
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement