0

我试图从我的代码主体中排除我的二次方程,现在它返回十六进制数字......这些数字非常随机地返回,它们的值大约每运行程序 3-10 次就会改变一次,即使变量是持续的。这远远超出了我对 C++ 的了解。

理想情况下,我希望以双精度数据类型和以 10 为底返回二次方程的根。

最后注释掉的部分只是为了向您展示我最初所做的事情,尽管大部分都是多余的。

提前致谢。

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


double a,b,c,bsq;

double quadpos() {

    double quadpos = (-b+(sqrt((bsq)-4.0*a*c)))/(2.0*a);

    double a=1.0,b=2.0,c=1.0;
    double bsq = pow(2.0,2);

    return quadpos;
}

double quadneg() {

    double quadneg = (-b-(sqrt((bsq)-4.0*a*c)))/(2.0*a);
    return quadneg;
}


int main() {
    // 4a)
    cout << /*" - Question 4 - \n\n" << "Part A\n" << */quadpos << "\n" << quadneg << std::endl;

    /*float a = 5.0, b = 7.0, c = -8.0;
    int bsq;
    bsq = pow(b,2);
    double quadpos = (-b+(sqrt((bsq)-4.0*a*c)))/(2.0*a), quadneg = (-b-(sqrt((bsq)-4.0*a*c)))/(2.0*a);

    cout << a << "\n" << b << "\n" << c << "\n" << bsq << "\n" << quadpos << "\n" << quadneg << endl;
    The above line is used for troubleshooting

    cout << "The roots of the quadratic equation 5x^2 + 7x - 8 are " << quadpos << " and " << quadneg << ".\n" << "The roots are the values of the x variable when the equation, 5x^2 + 7x - 8 is equal to 0.\n" << endl;
    */
}
4

1 回答 1

1
cout << /* ... */ quadpos << "\n" << quadneg << std::endl;
                       ^^^^ ?

您正在直接打印函数并获取它们的地址(在此上下文中它们被解释为函数指针)。而不是quadpos你想要的quadpos()

于 2013-10-03T00:13:23.973 回答