C++ 未知格式的二次方程求解器输出
我试图做一个二次方程求解器,但由于某种原因,我的程序以未知的格式给我答案 我输入了一个简单的二次方程x^2+2x+1=0,期望我的程序给出x=-1或x=-1,但它给出了x=0138151E或x=0138152D。它看起来像是为x输出这些值,用于任何无法识别和捕捉不真实答案的输入。这是为什么?我如何修复它C++ 未知格式的二次方程求解器输出,c++,C++,我试图做一个二次方程求解器,但由于某种原因,我的程序以未知的格式给我答案 我输入了一个简单的二次方程x^2+2x+1=0,期望我的程序给出x=-1或x=-1,但它给出了x=0138151E或x=0138152D。它看起来像是为x输出这些值,用于任何无法识别和捕捉不真实答案的输入。这是为什么?我如何修复它 #include "../std_lib_facilities_revised.h" class Imaginary {}; double square(int a) { retur
#include "../std_lib_facilities_revised.h"
class Imaginary {};
double square(int a)
{
return a * a;
}
double quadratic_solver_pos(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b + sqrt(radicand)) / (2 * a);
return x;
}
double quadratic_solver_neg(int a, int b, int c)
{
double x = 0.0;
double radicand = square(b) - 4 * a * c;
if (radicand < 0) throw Imaginary{};
x = (-b - sqrt(radicand)) / (2 * a);
return x;
}
int main()
try {
cout << "This program is a quadratic equation solver.\n";
cout << "Quadratic equations are of the form: ax^2 + bx + c = 0\n";
cout << "Enter a, b, and c, respectively:\n";
double a = 0;
double b = 0;
double c = 0;
cin >> a >> b >> c;
cout << "Your quadratic equation: " << a << "x^2 + " << b << "x + " << c << " = 0\n";
cout << "x = " << quadratic_solver_pos << " or x = " << quadratic_solver_neg << '\n';
}
catch (Imaginary) {
cout << "x is unreal\n";
}
您不会将变量传递给函数
你需要像这样做二次解算器 为什么你要打印出你函数的地址而不是调用函数呢?投票结束就像打字一样。您需要调用该函数。顺便说一句,删除square函数并用x*x替换调用。简单操作不需要调用函数。拥有两个函数二次解算器pos和二次解算器neg似乎也是多余的。恕我直言,但一个编写良好的函数可以处理这两种情况。另外:我是否应该大胆地问一下include../std_lib_facilities\u revision.h是什么?谢谢,我没有看到。