C语言题 求方程ax^2+bx+c=0的根。分别考虑:有两个不等的实根;有两个相等的实根
3个回答
展开全部
楼主你好。
以下是我的代码,加了些注释,希望对你有帮助。
#include <stdio.h>
#include <math.h>
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("==========================================\n");
}
return 1;
}
输出:
Please enter a, b and c:1 -7 12
a=1.00, b=-7.00, c=12.00
x1=4.00, x2 = 3.00
==========================================
Please enter a, b and c:2 -6 -20
a=2.00, b=-6.00, c=-20.00
x1=5.00, x2 = -2.00
==========================================
Please enter a, b and c:1 4 3
a=1.00, b=4.00, c=3.00
x1=-1.00, x2 = -3.00
==========================================
Please enter a, b and c:4 1 4
a=4.00, b=1.00, c=4.00
deta is negtive! This quation doesn't have a solution!
==========================================
Please enter a, b and c:
以下是我的代码,加了些注释,希望对你有帮助。
#include <stdio.h>
#include <math.h>
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("==========================================\n");
}
return 1;
}
输出:
Please enter a, b and c:1 -7 12
a=1.00, b=-7.00, c=12.00
x1=4.00, x2 = 3.00
==========================================
Please enter a, b and c:2 -6 -20
a=2.00, b=-6.00, c=-20.00
x1=5.00, x2 = -2.00
==========================================
Please enter a, b and c:1 4 3
a=1.00, b=4.00, c=3.00
x1=-1.00, x2 = -3.00
==========================================
Please enter a, b and c:4 1 4
a=4.00, b=1.00, c=4.00
deta is negtive! This quation doesn't have a solution!
==========================================
Please enter a, b and c:
展开全部
#include<stdio.h>
#include<string.h>
#include <math.h>
void main()
{
double a, b, c;
double val;
double root1, root2;
printf("输入参数(空格分隔): ");
scanf("%lf %lf %lf", &a, &b, &c);
val = b*b-4*a*c;
if(val < 0)
{
printf("无解");
return;
}
if(val == 0) // 由于精度问题,一般小于一个值就认为是零, 写成abs(val) < eps
{
root1 = -b /(2*a);
printf("相同解 %f", root1);
}
else
{
val = sqrt(val);
root1 = (-b + val)/(2*a);
root2 = (-b - val)/(2*a);
printf("解 %f , %f", root1, root2);
}
}
#include<string.h>
#include <math.h>
void main()
{
double a, b, c;
double val;
double root1, root2;
printf("输入参数(空格分隔): ");
scanf("%lf %lf %lf", &a, &b, &c);
val = b*b-4*a*c;
if(val < 0)
{
printf("无解");
return;
}
if(val == 0) // 由于精度问题,一般小于一个值就认为是零, 写成abs(val) < eps
{
root1 = -b /(2*a);
printf("相同解 %f", root1);
}
else
{
val = sqrt(val);
root1 = (-b + val)/(2*a);
root2 = (-b - val)/(2*a);
printf("解 %f , %f", root1, root2);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
求根公式写出来,然后计算那个表达式就行了,开方是函数sqrt 加头文件math.h
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询