2 回答

TA貢獻(xiàn)3593條經(jīng)驗(yàn) 獲得超0個(gè)贊

TA貢獻(xiàn)1條經(jīng)驗(yàn) 獲得超0個(gè)贊
#include<stdio.h>
#include<math.h>
int main()
{
float a, b, c, d, f1, f2, y;
scanf_s("%f %f %f", &a,&b,&c);
d = b*b - 4 * a*c;
y = (float)sqrt(d);
if (a == 0)
{
printf("The equation is not quadratic.");
}
else if (a != 0)
{
f1 = (-b + y) / (2 * a);
f2 = (-b - y) / (2 * a);
if (d == 0)
{
printf("The eaquation has two equal roots: %.4f.", f1);
}
else if (d>0)
{
printf("The eaquation has two distinct real roots: %.4f and %.4f.", f1, f2);
}
else if (d<0)
{
printf("The eaquation has two complex roots: %.4f and %.4f",f1,f2);
}
}
system("pause");
return 0;
}

TA貢獻(xiàn)2條經(jīng)驗(yàn) 獲得超0個(gè)贊
求ax2+bx+c=0的根。分別考慮d=b2-4ac大于零,等于零和小于零三種情況。
a、b、c要求是浮點(diǎn)型。程序要對(duì)a、b、c的各種情況進(jìn)行處理。如判斷a是否為0,b2-4ac分別為大于0、小于0、等于0。
解答提示:
1)求一浮點(diǎn)數(shù)的平方根可以用庫函數(shù)sqrt(x)。x要求是浮點(diǎn)數(shù)。如以下賦值語句:y=sqrt(x);表示求x的平方根,賦值給y。為了使用該函數(shù),需要在main函數(shù)之前加上預(yù)處理語句:#include<math.h>。
2)如何判斷兩個(gè)浮點(diǎn)數(shù)是否相等:
假設(shè)f1和f2是兩個(gè)浮點(diǎn)數(shù)。若想寫一個(gè)關(guān)系表達(dá)式,判斷f1和f2是否相等,不能寫成:if(f1==f2),而是要寫成f1和f2的差的絕對(duì)值近似接近于0,如寫成:if(fabs(f1-f2)<=1e-6)。其中1e-6表示10的-6次方,fabs函數(shù)用于求絕對(duì)值。原因:浮點(diǎn)數(shù)在內(nèi)存中是以近似值存儲(chǔ)的,所以不能執(zhí)行是否相等的比較。
輸入格式
輸入3個(gè)浮點(diǎn)數(shù),代表a,b,c。
輸出格式
輸出對(duì)應(yīng)方程的根:
當(dāng)該方程非一元二次方程時(shí),輸出“The equation is not quadratic.”。
當(dāng)該一元二次方程有兩個(gè)相等的實(shí)數(shù)根時(shí),輸出“The equation has two equal roots: x.”。
當(dāng)該一元二次方程有兩個(gè)不相等的實(shí)數(shù)根時(shí),輸出“The equation has two distinct real roots: x1 and x2.”。(x1 ?>?x2)
當(dāng)該一元二次方程有兩個(gè)不相等的虛數(shù)根時(shí),輸出“The equation has two complex roots: x1 and x2.”。
?
若x為虛數(shù),則x1的格式為a+bi,x2的格式為a-bi.(其中a,b保留4位有效數(shù)字, 例如:3.0000+3.0000i)
所有的數(shù)均保留4位有效數(shù)字。
- 2 回答
- 1 關(guān)注
- 2352 瀏覽
添加回答
舉報(bào)