1. 程式人生 > >【c】【例3.5】求ax^2+bx+c=0方程的根。a,b,c由鍵盤輸入,設b^2-4*a*c>0。

【c】【例3.5】求ax^2+bx+c=0方程的根。a,b,c由鍵盤輸入,設b^2-4*a*c>0。

c
#include<stdio.h>
#include<math.h>


int main()
{
	double a,b,c,x1,x2,p,q,disc;
	printf("Please enter a,b,and c :");  //給使用者下達指令(書上沒有) 
	scanf("%lf,%lf,%lf",&a,&b,&c);   //自己犯的錯:(重點)輸入雙精度型變數的值要用格式宣告"%lf" 
        disc=b*b-4*a*c;                         //這裡預設大於零,即有兩個不等實根 
	p=-b/(2.0*a);                   //自己犯的錯:是2.0,不是2 
	q=sqrt(disc)/(2.0*a);           //同樣 
	x1=p+q;
	x2=p-q;
	printf("x1=%7.2f\nx2=%7.2f\n",x1,x2);
	return 0; 
}