我遇到的问题与下面的代码:C++二次代码错误
#include<iostream>
#include<fstream>
#include<cmath>
using namespace std;
int main()
{
ifstream fin("input.txt");
ofstream fout("output.txt");
float discriminant, A, B, C, root1, root2;
fin >> A >> B >> C;
while (A != -99)
{
discriminant = (pow(B, 2.0) - 4 * A*C);
if (A == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant > 0)
{
root1 = (-B - sqrt(discriminant))/(2.0*A);
root2 = (-B + sqrt(discriminant))/(2.0*A);
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else if (discriminant == 0)
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
else
{
fout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
}
fin >> A >> B >> C;
}
fout.close();
ifstream fin2("output.txt");
fin2 >> A >> B >> C >> root1 >> root2;
while (!fin2.eof())
{
cout << A << "\t" << B << "\t" << C << "\t" << root1 << "\t" << root2 << endl;
fin2 >> A >> B >> C >> root1 >> root2;
}
cout << endl;
cout << "Coded by Paye W. Kialain" << "\t"<< endl;
system("pause");
return 0;
}
在项目描述中,有人告诉我,创建一个包含,b和c,我做到了输入文件。输出格式也是正确的。它是一个显示a,b和c值以及2个计算出的根的表格。然而,根的计算似乎是关闭的。我的if语句是否是这个问题?
也许你可以告诉我们什么输入会给出什么错误的输出。 –
输入是: 6 -10 -4 但输出似乎是: 6 -10 -4 -0.333333 2 2 6 9 - 0.333333 2 2 4 8 -0.333333 2 0 2 4 -0.333333 2 2 4 2 2 -0.333333 通过佩耶W. Kialain 按编码任意键继续。 。 。 –
一些最新出现错误的细节:[为什么浮点数不准确?](http://stackoverflow.com/questions/21895756/why-are-floating-point-numbers-incucurate) – user4581301