求解一元二次方程ax^2+bx+c=0的根
#include <iostream>
#include <string>
#include <cmath>
using namespace std
;
int main()
{
float a
, b
, c
;
float x1
, x2
;
cout
<< "请输入a的值:" << endl
;
cin
>> a
;
cout
<< "请输入b的值:" << endl
;
cin
>> b
;
cout
<< "请输入c的值:" << endl
;
cin
>> c
;
cout
<< "您输入的方程为:" << a
<< "x^2+" << b
<< "x+" << c
<< "=0" << endl
;
float t
= b
* b
- 4 * a
* c
;
if (t
< 0)
{
cout
<< "此方程无实数根" << endl
;
}
else
{
x1
= (-b
+ sqrt(t
)) / (2 * a
);
x2
= (-b
- sqrt(t
)) / (2 * a
);
cout
<< "一个根x1=" << x1
<< endl
;
cout
<< "一个根x2=" << x2
<< endl
;
}
system("pause");
return 0;
}
转载自:黑凤梨の博客
转载请注明原文地址:https://blackberry.8miu.com/read-2400.html