作业帮 > 综合 > 作业

C语言题 求方程ax^2+bx+c=0的根.分别考虑:有两个不等的实根;有两个相等的实根

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/19 10:25:39
C语言题 求方程ax^2+bx+c=0的根.分别考虑:有两个不等的实根;有两个相等的实根
我大一新生刚学C语言,请用易懂的算法,最起码那些符号,变量名我得能看懂.
楼主你好.
以下是我的代码,加了些注释,希望对你有帮助.
#include
#include
int main(){
double a,b,c;
double x1,x2;
double deta;
while(1){
printf("Please enter a, b and c:");
scanf("%lf %lf %lf",&a, &b, &c);//读取数据到a,b和c
printf("a=%.2f, b=%.2f, c=%.2f\n", a, b, c);//先让用户看一下a,b和c
deta = b*b-4*a*c;//计算deta
if(deta < 0){//若deta小于零,提示用户此方程无实数解
printf("deta is negtive! This quation doesn't have a real solution!\n");
}else{
if(deta == 0){
x1=x2= -b/(2*a);//若deta为0,两根一样
}else{
deta = sqrt(deta);//若deta大于零,分别求出两个根.sqrt函数是math.h中声明的求平方根函数
x1 = (-b+deta)/(2*a);
x2 = (-b-deta)/(2*a);
}
printf("x1=%.2f, x2 = %.2f\n", x1, x2);//输出两个根
}
printf("
Please enter a, b and c: