📜 ⬆️ ⬇️

Quadratic equation? Yes, just spit!

Argued that only 10% of programmers are able to write a binary search . We will not have this opinion, but what about the quadratic equation?

We set the problem more specifically: solving a quadratic equation of the form ax 2 + bx + c = 0 with integer coefficients. Three integers within the int (coefficients a, b and c) are fed to the input. The program should always produce a result.
It would seem a trifling matter: five minutes and it's done! And now, after five minutes, we have the following code at the output:
Code
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; 
Is everything cool? As if not so. The “customer”, fifth-grader Petya, is not satisfied: the program issued some “nan” and “inf” to his input.
 0 -1 -1 D > 0; 2 : x1 = inf x2 = nan 

Shrug, add a condition for a degenerate case:
Code
 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 { //  ,   . } 
Got better? And no.
 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.
Code
All code of this stage.
 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.
Five years have passed. Peter moved to the tenth grade. There in an algebra lesson he was told about imaginary and complex numbers. Having dug up our program somewhere on the disk, he is trying with its help - a scoundrel! - do homework. But what is it?
 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).
Code
 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 

Petya finishes school with a five in the certificate, goes to a prestigious university and crashes after the first session.
The end.
The final code.

')

Source: https://habr.com/ru/post/175533/


All Articles