int a, b, c; std::cin >> a >> b >> c; long long discriminant = b*b - 4*a*c; if (discriminant > 0) std::cout << "D > 0; 2 :\n" << "x1 = " << (-b + sqrt(discriminant))/(2*a) << "\n" << "x2 = " << (-b - sqrt(discriminant))/(2*a) << std::endl; else if (discriminant == 0) std::cout << "D = 0; 1 :\n" << "x = " << -1.*b/(2*a) << std::endl; else if (discriminant < 0) std::cout << "D < 0; ." << std::endl;
0 -1 -1 D > 0; 2 : x1 = inf x2 = nan
if (a == 0) { if (b != 0) std::cout << "a = 0; :\n" << "x = " << -1.*c/b << std::endl; else if (c == 0) std::cout << " ; x - ." << std::endl; else std::cout << " ." << std::endl; } else { // , . }
1 0 -2 D > 0; 2 : x1 = 1.41421 x2 = -1.41421 49 7 -2 D > 0; 2 : x1 = 0.142857 x2 = -0.285714
For such answers, Petya was scolded at school: the root should remain the root, and the fraction - a fraction. We scratch the back of the head, sigh and take on the code; structure after structure, function after function, it swells ten times, but the main function remains relatively unchanged, although it has gained a lot of weight. if (a == 0) { if (b != 0) std::cout << "a = 0; :\n" << "x = " << fraction(-c,b).toString() << std::endl; else if (c == 0) std::cout << " ; x - ." << std::endl; else std::cout << " ." << std::endl; } else { long long discriminant = b*b - 4*a*c; if (discriminant > 0) { std::cout << "D > 0; 2 :\n"; radical dRoot (discriminant); if (dRoot.isInteger()) { std::cout << "x1 = " << fraction(-b + sqrt(discriminant), 2*a).toString() << "\n" << "x2 = " << fraction(-b - sqrt(discriminant), 2*a).toString() << std::endl; } else { std::string rational = fraction(-b, 2*a).toString(), irrational = fraction(radical(discriminant), 2*a).toString(); if (rational == "0") std::cout << "x1 = " << irrational << "\n" << "x2 = " << "- " << irrational << std::endl; else std::cout << "x1 = " << rational << " + " << irrational << "\n" << "x2 = " << rational << " - " << irrational << std::endl; } } else if (discriminant == 0) std::cout << "D = 0; 1 :\n" << "x = " << fraction(-b, 2*a).toString() << std::endl; else if (discriminant < 0) std::cout << "D < 0; ." << std::endl; }
1 0 -2 D > 0; 2 : x1 = ┐/2 x2 = - ┐/2 49 7 -2 D > 0; 2 : x1 = 1/7 x2 = -2/7
Petya is satisfied, Petya brought the five maths from school to parents and a chocolate to us. 1 0 25 D < 0; .
The indignant Peter brings us the program for the finishing touches. After a couple of hours of reading the old code, we understand that the problem is being solved ... by adding a couple of lines (to be honest, I did not expect it myself). if (discriminant != 0) { std::cout << "D != 0; 2 :\n"; radical dRoot (discriminant); if (dRoot.isInteger()) { std::cout << "x1 = " << fraction(-b + sqrt(discriminant), 2*a).toString() << "\n" << "x2 = " << fraction(-b - sqrt(discriminant), 2*a).toString() << std::endl; } else { std::string rational = fraction(-b, 2*a).toString(), irrational = fraction(radical(discriminant), 2*a).toString(); if (rational == "0") std::cout << "x1 = " << irrational << "\n" << "x2 = " << "- " << irrational << std::endl; else std::cout << "x1 = " << rational << " + " << irrational << "\n" << "x2 = " << rational << " - " << irrational << std::endl; } } else if (discriminant == 0) std::cout << "D = 0; 1 :\n" << "x = " << fraction(-b, 2*a).toString() << std::endl;
1 0 25 D != 0; 2 : x1 = i*5 x2 = - i*5 1 2 53 D != 0; 2 : x1 = -1 + i*2┐/13 x2 = -1 - i*2┐/13
Source: https://habr.com/ru/post/175533/
All Articles