我试图从我的主体代码中排除我的二次方程式,现在它返回的是十六进制数字......数字是相当随机返回的,它们在运行程序时每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;
*/
}
答案 0 :(得分:1)
cout << /* ... */ quadpos << "\n" << quadneg << std::endl; ^^^^ ?
您正在直接打印函数并获取其地址(在此上下文中它们被解释为函数指针)。而不是quadpos
,而是quadpos()
。