Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. #include<iostream>
  2. #include<cmath>
  3. using namespace std;
  4.  
  5. class Trinomial {
  6. private: int x, x1, x2;
  7. private: int firstСoefficient, secondСoefficient, thirdСoefficient;
  8. private: int first = 3, second = 15, third = 34;
  9. private: char check;
  10.  
  11. public: Trinomial(int first, int second, int third) {
  12. firstСoefficient = first;
  13. secondСoefficient = second;
  14. thirdСoefficient = third;
  15. }
  16.  
  17. public: void display() {
  18. cout << "Your expression: " << firstСoefficient << "'x^2 + ";
  19. cout << secondСoefficient << "x + " << thirdСoefficient;
  20.  
  21. cout << "\nSecond expression: " << first << "'x^2 + ";
  22. cout << second << "x + " << third;
  23.  
  24. }
  25. public: void sum() {
  26. cout << "Result of sum: ";
  27. check = checkSign(second, secondСoefficient);
  28. cout << first + firstСoefficient << "x^2 " << check << ' ';
  29. check = checkSign(third, thirdСoefficient);
  30. cout << second + secondСoefficient << "x " << check << ' ';
  31. cout << third + thirdСoefficient << '\n';
  32. }
  33. public: void difference() {
  34. cout << "Result of difference: ";
  35. char check = checkSignInDifference(second, secondСoefficient);
  36. cout << first - firstСoefficient << "x^2 " << check << ' ';
  37. check = checkSignInDifference(third, thirdСoefficient);
  38. cout << second - secondСoefficient << "x " << check << ' ';
  39. cout << third - thirdСoefficient << '\n';
  40. }
  41.  
  42. private: char checkSign(int first, int second) {
  43. int result = first + second;
  44.  
  45. if (result >= 0) {
  46. return '+';
  47. }
  48. else {
  49. return '-';
  50. }
  51. }
  52. private: char checkSignInDifference(int first, int second) {
  53. int result = first - second;
  54.  
  55. if (result >= 0) {
  56. return '+';
  57. }
  58. else {
  59. return '-';
  60. }
  61. }
  62.  
  63. public: void complainig() {
  64.  
  65. double firstD = calculation(firstСoefficient, secondСoefficient, thirdСoefficient);
  66. double secondD = calculation(first, second, third);
  67.  
  68. cout << "Result of complaining: ";
  69. if (firstD > secondD) {
  70. cout << "First expression is bigger than second\n";
  71. }
  72. else if (firstD < secondD) {
  73. cout << "First expression is bigger than second\n";
  74. }
  75. else {
  76. cout << "They are equal\n";
  77. }
  78.  
  79. }
  80. private: int calculation(double Сoefficient, double sСoefficient, double tСoefficient) {
  81. if ((pow(sСoefficient, 2) - 4 * Сoefficient * tСoefficient) >= 0)
  82. {
  83. x1 = (-1 * sСoefficient + sqrt(pow(sСoefficient, 2) - 4 * Сoefficient * tСoefficient)) / (2 * Сoefficient);
  84. x2 = (-1 * sСoefficient + sqrt(pow(sСoefficient, 2) - 4 * Сoefficient * tСoefficient)) / (2 * Сoefficient);
  85. }
  86. else
  87. {
  88. return -1;
  89. }
  90. return x1 + x2;
  91. }
  92.  
  93. public: void sumWithDouble() {
  94. double firstD = 3.0, secondD = 4.0, thirdD = 3.0;
  95.  
  96. cout << "Result of double number sum: ";
  97. check = checkSign(secondD, secondСoefficient);
  98. cout << firstD + firstСoefficient << "x^2 " << check << ' ';
  99. check = checkSign(thirdD, thirdСoefficient);
  100. cout << secondD + secondСoefficient << "x " << check << ' ';
  101. cout << thirdD + thirdСoefficient << '\n';
  102. }
  103. public: void multiplyWithDouble() {
  104. double firstEx[3] = { firstСoefficient, secondСoefficient, thirdСoefficient };
  105. int secondEx[3] = { first, second, third };
  106. int resultF = 0, resultS = 0, resultT = 0;
  107.  
  108. for (int i = 0; i < 3; i++) {
  109. for (int j = 0; j < 3; j++) {
  110. if (i == 1) {
  111. resultS += (firstEx[i] * secondEx[j]);
  112. }
  113. else if (i == 2) {
  114. resultT += (firstEx[i] * secondEx[j]);
  115. }
  116. else {
  117. resultF += (firstEx[i] * secondEx[j]);
  118. }
  119. }
  120. }
  121.  
  122. cout << "Result of multiply: ";
  123. cout << resultF << " + " << resultS << " + " << resultT << '\n';
  124. }
  125. public: void calculation(int x) {
  126. this->x = x;
  127.  
  128. cout << "Result if x == " << x << ": ";
  129. cout << firstСoefficient * pow(x, 2) + secondСoefficient * x + thirdСoefficient<< '\n';
  130. }
  131.  
  132. };
  133.  
  134. int main() {
  135. Trinomial t(1, 3, 4);
  136. t.display();
  137. t.sum();
  138. t.difference();
  139. t.complainig();
  140. t.sumWithDouble();
  141. t.multiplyWithDouble();
  142. t.calculation(2);
  143.  
  144.  
  145. return 0;
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement