入门级求根
#include
#include
using namespace std;
int main()
{
float a,b,c,d;
cout<<"输入三个系数";
cin>>a>>b>>c;
cout<<"a="<
该方法无法求出虚根(无法求出的d<0时的虚根);下面用调用函数的方法求一元二次方程的根
先补充一个知识点:
例题如下:设置精度的 *** 作
图片来源:C语言程序设计教程(第三版)课后习题4.9 (C++代码)-Dotcpp编程社区
#include
#include
#include
using namespace std;
void fun1(float a, float b, float d) //d>0时,直接带公式
{
float x1,x2;
d = sqrt(d);
x1 = (-b + d) / 2 / a;
x2 = (-b - d) / 2 / a;
cout << "x1= "<> a >> b>>c;
d = b * b - 4*a*c;
if (d > 0)
fun1(a, b, d);
else if (d == 0) //易错写成=
fun2(a, b);
else
fun3(a, b, d);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)