求出方程ax^2+bx+c=0的实根

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zheweizhe/article/details/72232546

代码

/*01.
02.*程序的版权和版本声明部分
03.*Copyright(c)2017,陕西科技大学镐京学院
04.*All rightsreserved.
05.*文件名称:
06.*作者:郑伟哲
07.*完成日期:2017年5月13日
08.*版本号:v1.0
09.*输入描述:
10.*问题描述:求出方程ax^2+bx+c=0的是根 
11.           需要对b^2-4ac判定 
12.做了这题的心得
13.有哪些不明白
14.运行状况:
15.*/
#include<stdio.h>
#include<math.h>//程序中要调用求平方根函数sqrt
int main()
{
	double a,b,c,disc,x1,x2,p,q;//disc是判别式sqrt(b*b-4ac)
	scanf("%lf%lf%lf",&a,&b,&c);
	disc=b*b-4*a*c;
	if(disc<0)
	printf("this equation hasn't real roots\n");
	else
	{
		p=-b/(2.0*a);
		q=sqrt(disc)/(2.0*a);
		x1=p+q;x2=p-q;
		printf("real roots:\nx1=%7.2f\nx2=%7.2f\n",x1,x2);
	 } 
	 return 0;
 } 

运行结果

心得

有些实践还得多练,

猜你喜欢

转载自blog.csdn.net/zheweizhe/article/details/72232546
今日推荐