Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. #include <iostream>
  2. #include<cmath>
  3. #include<fstream>
  4. #include<string>
  5. #include<vector>
  6. using namespace std;
  7.  
  8. class Quadratic {
  9. public:
  10. Quadratic(double a, double b, double c);
  11. ~Quadratic();
  12. void Solution();
  13. private:
  14. double *x;
  15. double a;
  16. double b;
  17. double c;
  18. };
  19. Quadratic::Quadratic(double a, double b, double c) {
  20. x = new double[2];
  21. this->a = a;
  22. this->b = b;
  23. this->c = c;
  24. };
  25. Quadratic::~Quadratic() {
  26. delete[]x;
  27. };
  28. void Quadratic::Solution() {
  29. if (a == 0) {
  30. if (b == 0) {
  31. if (c == 0) {
  32. cout << "х - любой число!" << endl;
  33. }
  34. else {
  35. cout << "c!=0\n" << "Нет решений!" << endl;
  36. };
  37. }
  38. else {
  39. if (c == 0) {
  40. x[0] = (c / b);
  41. cout << "x=" << x[0] << endl;
  42. }
  43. else {
  44. x[0] = -(c / b);
  45. cout << "x=" << x[0] << endl;
  46. }
  47. };
  48. }
  49. else {
  50. double D;
  51. D = (b*b) - 4 * a*c;
  52. if (D >= 0) {
  53. if (b == 0) {
  54. if (c == 0) {
  55. cout << "x1=x2=0" << endl;
  56. }
  57. else {
  58. x[0] = (sqrt(D)) / (2 * a);
  59. x[1] = (-sqrt(D)) / (2 * a);
  60. cout << "x1=" << x[0] << endl << "x2=" << x[1] << endl;
  61. };
  62. }
  63. else {
  64. x[0] = (-b + sqrt(D)) / (2 * a);
  65. x[1] = (-b - sqrt(D)) / (2 * a);
  66. cout << "x1=" << x[0] << endl << "x2=" << x[1] << endl;
  67. }
  68. }
  69. else {
  70. if (b == 0) {
  71. cout << "x1=" << "+i*" << sqrt(abs(D)) / (2 * a) << endl;
  72. cout << "x2=" << "-i*" << sqrt(abs(D)) / (2 * a) << endl;
  73.  
  74. }
  75. else {
  76. cout << "x1=" << (-b) / (2 * a) << "+i*" << sqrt(abs(D)) / (2 * a) << endl;
  77. cout << "x2=" << (-b) / (2 * a) << "-i*" << sqrt(abs(D)) / (2 * a) << endl;
  78. };
  79. };
  80. };
  81. };
  82.  
  83. int main()
  84. {
  85. double a;
  86. double b;
  87. double c;
  88. setlocale(LC_ALL, "ru");
  89. int menu;
  90. bool t = true;
  91. while (t) {
  92. cout << "ax^2+bx+c=0" << endl;
  93. cout << "1.Ввести коэффициенты с клавиатуры" << endl;
  94. cout << "2.Взять коэффициенты из файла" << endl;
  95. cout << "0.Выход" << endl;
  96. cin >> menu;
  97. switch (menu)
  98. {
  99. case 1: {
  100. system("cls");
  101. for (int i = 0; i < 3; i++)
  102. {
  103. if (i == 0) {
  104. cout << "Введите коэффициент а" << endl;
  105. cin >> a;
  106. }
  107. if (i == 1) {
  108. cout << "Введите коэффициент b" << endl;
  109. cin >> b;
  110. }
  111. if (i == 2) {
  112. cout << "Введите коэффициент c" << endl;
  113. cin >> c;
  114. };
  115. }
  116. Quadratic equation(a, b, c);
  117. equation.Solution();
  118. break;
  119. }
  120. case 2: {
  121. system("cls");
  122. fstream fs;
  123. fs.open("Text.txt", fstream::in);
  124. if (!fs.is_open()) {
  125. cout << "Файл не найден" << endl;
  126. }
  127. else {
  128. /*string str="";
  129. while (!fs.eof()) {
  130.  
  131. getline(fs, str);
  132. if (str.find('a') != string::npos&&str.find(';') != string::npos) {
  133. a = stod(str.substr(str.find('=')+1, str.find(';') - (str.find('=')+1)));
  134.  
  135. }
  136. if (str.find('b') != string::npos&&str.find(';') != string::npos) {
  137. b = stod(str.substr(str.find('=') + 1, str.find(';') - (str.find('=') + 1)));
  138. }
  139. if (str.find('c') != string::npos&&str.find(';') != string::npos) {
  140. c = stod(str.substr(str.find('=') + 1, str.find(';') - (str.find('=') + 1)));
  141. }
  142. cout << str << endl;
  143. }*/
  144. string str = "";
  145. char ch;
  146. vector<int> values;
  147. fstream fs;
  148. fs.open("Text.txt", fstream::in);
  149. while (!fs.eof()) {
  150. fs.get(ch);
  151. str = str + ch;
  152. if (ch == ' ') {
  153. if (str == " " || str == "" || str == "\0") {
  154.  
  155. }
  156. else {
  157. values.push_back(stoi(str));
  158. str = "";
  159. }
  160. };
  161. }
  162. auto it = values.begin();
  163. if (values.size() < 3) {
  164. cout << "Недостаточно коэффициентов" << endl;
  165. }
  166. else {
  167. a = *it;
  168. ++it;
  169. b = *it;
  170. ++it;
  171. c = *it;
  172. values.erase(values.begin(),it);
  173. }
  174.  
  175. Quadratic equation(a, b, c);
  176. equation.Solution();
  177. }
  178.  
  179. break;
  180. }
  181. default:
  182. t = false;
  183. break;
  184. }
  185.  
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement