c++ implementation of quadratic equation, throws an error when implemented on a school testing website

无人久伴 提交于 2019-12-12 00:30:59

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!