当前位置: > 求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0,等于0,和小于0时的根并输出结果.从主函数a,b,c输...
题目
求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0,等于0,和小于0时的根并输出结果.从主函数a,b,c输
用c语言编程,

提问时间:2021-01-02

答案
#include
#include
// b^2-4ac == 0
void fun1(double &a,double &b,double &c,double &d){
double ans = -b/(2*a);
printf("b^2-4ac == 0 , x1 = x2 = %lf. ",ans);
}
// b^2-4ac > 0
void fun2(double &a,double &b,double &c,double &d){
double ans1,ans2;
ans1 = (-b+sqrt(d)) / (2*a);
ans2 = (-b-sqrt(d)) / (2*a);
printf("b^2-4ac > 0 , x1 = %lf , x2 = %lf. ",ans1,ans2);
}
// b^2-4ac < 0
void fun3(double &a,double &b,double &c,double &d){
double real,imar;
real = -b/(2*a);
imar = sqrt(-d) / (2*a);
printf("b^2-4ac < 0 , x1 = %lf+%lfi , x2 = %lf-%lfi. ",real,imar,real,imar);
}
int main(){
double a,b,c,d;
printf("please input a,b,c. ");
while(scanf("%lf%lf%lf",&a,&b,&c)!=EOF){
d = b*b-4*a*c;
if(d==0) fun1(a,b,c,d);
else if(d>0) fun2(a,b,c,d);
else fun3(a,b,c,d);
printf("please input a,b,c. ");
}
}
举一反三
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
1,人们染上烟瘾,最终因吸烟使自己丧命.
版权所有 CopyRight © 2012-2019 超级试练试题库 All Rights Reserved.