Advertisement
Guest User

Untitled

a guest
Jul 27th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <cstdlib>
  4.  
  5. using namespace std;
  6.  
  7. class Poly {
  8. public:
  9. Poly();
  10. Poly(double c);
  11. Poly(double a[], int size);
  12. Poly(const Poly& source);
  13. ~Poly();
  14. Poly& operator=(const Poly& source);
  15. friend const Poly operator + (const Poly& p1, const Poly& p2);
  16. friend const Poly operator - (const Poly& p1, const Poly& p2);
  17. friend const Poly operator * (const Poly& p1, const Poly& p2);
  18. friend ostream& operator << (ostream& output, const Poly& p);
  19. friend void maxPoly(const Poly p1, const Poly p2);
  20. double& operator [] (const int position);
  21. double polyVal(double x) const;
  22. void print() const;
  23. private:
  24. double* coef;
  25. int degree;
  26. };
  27.  
  28. ostream& operator<<(ostream& output, const Poly& p) {
  29. p.print();
  30. return output;
  31. }
  32. void Poly::print() const {
  33. bool firstNonZero = false;
  34. for (int i = 0; i < degree; i++) {
  35. if (coef[degree - i - 1] != 0) {
  36. firstNonZero = true;
  37. cout << coef[degree - i - 1];
  38. if ((degree - i - 1) != 0) {
  39. cout << "x^" << degree - i - 1 ;
  40. }
  41. }
  42. if (firstNonZero && (i < (degree - 1))) {
  43. cout << " + ";
  44. }
  45. }
  46. if (firstNonZero == false) {
  47. cout << "0";
  48. }
  49. }
  50. Poly::Poly() {
  51. degree = 1;
  52. coef = new double[degree];
  53. coef[0] = 0;
  54. }
  55. Poly::Poly(double c) {
  56. coef = new double[degree];
  57. coef[0] = c;
  58. }
  59. Poly::Poly(double a[], int SIZE) {
  60. degree = SIZE;
  61. coef = new double[SIZE];
  62. for (int i = 0; i <= SIZE; i++) {
  63. coef[i] = a[i];
  64. }
  65. }
  66. Poly::Poly(const Poly& source) {
  67. if (degree != source.degree) {
  68. delete [] coef;
  69. degree = source.degree;
  70. coef = new double[degree];
  71. }
  72. for (int i = 0; i < degree; i++) {
  73. coef[i] = source.coef[i];
  74. }
  75. }
  76. Poly::~Poly() {
  77. delete [] coef;
  78. }
  79. Poly& Poly::operator=(const Poly& source) {
  80. if (degree != source.degree) {
  81. delete [] coef;
  82. degree = source.degree;
  83. coef = new double[degree];
  84. }
  85. for (int i = 0; i < degree; i++) {
  86. coef[i] = source.coef[i];
  87. }
  88. return *this;
  89. }
  90. const Poly operator+(const Poly& p1, const Poly& p2) {
  91. Poly sum;
  92. if (p1.degree >= p2.degree) {
  93. sum.degree = p1.degree;
  94. } else {
  95. sum.degree = p2.degree;
  96. }
  97. delete [] sum.coef;
  98. sum.coef = new double[sum.degree];
  99. for (int i = 0; i < sum.degree; i++) {
  100. sum.coef[i] = 0;
  101. if (i < p1.degree) {
  102. sum.coef[i] += p1.coef[i];
  103. }
  104. if (i < p2.degree) {
  105. sum.coef[i] += p2.coef[i];
  106. }
  107. }
  108. return sum;
  109. }
  110. const Poly operator-(const Poly& p1, const Poly& p2) {
  111. Poly diff;
  112. if (p1.degree >= p2.degree) {
  113. diff.degree = p1.degree;
  114. } else {
  115. diff.degree = p2.degree;
  116. }
  117. delete [] diff.coef;
  118. diff.coef = new double[diff.degree];
  119. for (int i = 0; i < diff.degree; i++) {
  120. diff.coef[i] = 0;
  121. if (i < p1.degree) {
  122. diff.coef[i] += p1.coef[i];
  123. }
  124. if (i < p2.degree) {
  125. diff.coef[i] -= p2.coef[i];
  126. }
  127. }
  128. return diff;
  129. }
  130. const Poly operator*(const Poly& p1, const Poly&p2) {
  131. Poly product;
  132. product.degree = p1.degree + p2.degree;
  133. delete [] product.coef;
  134. product.coef = new double[product.degree];
  135. for (int i = 0; i < product.degree; i++) {
  136. product.coef[i] = 0;
  137. }
  138. for (int i = 0; i < p1.degree; i++) {
  139. for (int j = 0; j < p2.degree; j++) {
  140. product.coef[i + j] += p1.coef[i] * p2.coef[j];
  141. }
  142. }
  143. return product;
  144. }
  145. double& Poly::operator [ ] (const int position) {
  146. return coef[position];
  147. }
  148. double Poly::polyVal(double x) const {
  149. double polySum = 0;
  150. for (int i = 0; i <= degree - 1; i++) {
  151. polySum += coef[i] * pow(x,(degree - 1 - i));
  152. }
  153. return polySum;
  154. }
  155. void maxPoly(const Poly p1, const Poly p2) {
  156. double p3 = p1.polyVal(5);
  157. double p4 = p2.polyVal(5);
  158. if (p3 > p4) {
  159. cout << "The first array is larger than the second.";
  160. }
  161. else {
  162. cout << "The second array is larger than the first.";
  163. }
  164. }
  165.  
  166. int main() {
  167. Poly p1;
  168. Poly p2(3);
  169. double a[] = {1, -2, 3, 4};
  170. double b[] = {50, 0, 1};
  171. Poly p3(a, 4);
  172. Poly p4(b, 3);
  173. cout << "p1: " << p1 << endl;
  174. cout << "p2: " << p2 << endl;
  175. cout << "p3: " << p3 << endl;
  176. cout << "p4: " << p4 << endl;
  177. cout << "------------------------------" << endl;
  178.  
  179. Poly p5 = p1 + p2 + p3;
  180. cout << "p5 = p1 + p2 + p3: \n" << "p5: " << p5 << endl;
  181.  
  182. Poly p6 = p1 - p2;
  183. cout << "p1 - p2\n" << p6 << endl;
  184. p6 = p2 - p2;
  185. cout << "p2 - p2\n" << p6 << endl;
  186. p6 = p2 - p1;
  187. cout << "p2 - p1\n" << p6 << endl;
  188. cout << "p4 - p3: " << (p4 - p3) << endl;
  189. cout << "p1 * p3: " << (p1 * p3) << endl;
  190. cout << "p2 * p3: " << (p2 * p3) << endl;
  191. cout << "p4 * p3: " << (p4 * p3) << endl;
  192.  
  193. cout << p3.polyVal(0);
  194. cout<< p3[2];
  195. maxPoly(p3,p4);
  196. return 0;
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement