功能

时间:2017-05-15 20:01:52

标签: c++ function equation dev-c++

我正在尝试使用函数制作程序来计算二次方程的输出,但是Dev C ++不断给出输出“[Error] Id返回1退出状态”。 我是C ++的新手,如果我犯了一些愚蠢的错误,请提前抱歉。

#include <iostream>
#include <cmath>
using namespace std;

float equation1 (float, float, float);
float equation2 (float, float, float);

main()
{
    float a, b, c, Res1, Res2;
    cout << "Insert the parameters of the equation.\n";
    cin >> a >> b >> c;
    if (a == 0)
    {
        Res1 = b / c;
        cout << "It's a 1st degree eq. and the result is " << Res1 << endl;
    }
    else
    {
        Res1 = equation1 (a, b, c);
        Res2 = equation2 (a, b, c);
        cout << "The results of the eq. are " << Res1 << " and " << Res2 << endl;
    }
    system ("pause");
    return 0;
}

float equation1 (double a, double b, double c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (double a, double b, double c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}

1 个答案:

答案 0 :(得分:0)

更改函数定义中的类型。您正在使用double,然后C ++期望使用float(精度低于double)。

float equation1 (float a, float b, float c)
{
    float D, Res1;
    D = (b * b) - 4 * a * c;
    Res1 = (- b + sqrt(D)) / (2 * a);
    return Res1;
}

float equation2 (float a, float b, float c)
{
    float D, Res2;
    D = (b * b) - 4 * a * c;
    Res2 = (- b - sqrt(D)) / (2 * a);
    return Res2;
}