问题
I am trying to solve a very simple task tested by my univeristy's code checker. The code is about a c++ implementation of the equation of the quadratic equation. The code won't work for all cases, I am supplying my code, and if there is some comment, hint, please help me with it.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b, c, delta;
double x1, x2, x;
cin >> a >> b >> c;
delta = ((b*b) - (4 * a * c));
if (a == 0) {
if (b != 0) {
x = (-(c*1.0) / (b));
cout << 1 << " " << x;
}
else
cout << "-1";
}
else
if (delta > 0)
{
x1 = ((-(b + sqrt(delta))*1.0) / (2 * a));
x2 = ((-(b - sqrt(delta))*1.0) / (2 * a));
cout << 2 << " " << x1 << " " << x2;
}
else if (delta == 0)
{
x = (-(b*1.0) / (2 * a));
cout << 1 << " " << x;
}
else
cout << "-1";
return 0;
}
来源:https://stackoverflow.com/questions/57960792/c-implementation-of-quadratic-equation-throws-an-error-when-implemented-on-a