首先你已經(jīng)很清楚的說明了你這個程序是用C語言寫二次函數(shù)的,而當(dāng)a=0時,就不是二次函數(shù)了,應(yīng)該按照一次函數(shù)來進行計算,否則 一個數(shù)除以0就沒有意義了.~
創(chuàng)新互聯(lián)建站"三網(wǎng)合一"的企業(yè)建站思路。企業(yè)可建設(shè)擁有電腦版、微信版、手機版的企業(yè)網(wǎng)站。實現(xiàn)跨屏營銷,產(chǎn)品發(fā)布一步更新,電腦網(wǎng)絡(luò)+移動網(wǎng)絡(luò)一網(wǎng)打盡,滿足企業(yè)的營銷需求!創(chuàng)新互聯(lián)建站具備承接各種類型的成都網(wǎng)站設(shè)計、網(wǎng)站制作項目的能力。經(jīng)過十年的努力的開拓,為不同行業(yè)的企事業(yè)單位提供了優(yōu)質(zhì)的服務(wù),并獲得了客戶的一致好評。
#include stdio.h
#include stdlib.h
#include math.h
int main()
{
float a,b,c;
float x1,x2,m;
printf("input number a=:");
scanf("%f",a);
printf("input number b=:");
scanf("%f",b);
printf("input number c=:");
scanf("%f",c);
if(a==0)
printf("一根:%f\n",c*(-1)/b);
else if(a==0b==0)
printf("無意義!");
else
{
m=b*b-4*a*c;
if(m0)
{
printf("兩根\n");
printf("x1=%f\n",(-b+sqrt(m))/(2*a));
printf("x2=%f\n",(-b-sqrt(m))/(2*a));
}
else if(m==0)
printf("x1=x2=%f\n",x1);
}
else
printf("無實根\n");
}
return 0;
}
c語言中一個完整的函數(shù)由函數(shù)首部和函數(shù)體構(gòu)成,而且定義函數(shù)時兩者都是必不可少的。
函數(shù)定義的一般形式如下:
類型標識符
函數(shù)名(形參表列)
//
這是函數(shù)首部
//
以下{
}內(nèi)的是函數(shù)體
{
說明部分
執(zhí)行部分
}
舉例說明如下:
//
定義一個不帶返回值的函數(shù)
//
函數(shù)功能:輸出形參的值
void
fun(int
a,
int
b)
{
printf("%d,
%d\n",
a,
b);
}
//
定義一個帶返回值的函數(shù)
//
函數(shù)功能:返回2個整數(shù)數(shù)的最大值
int
fun(int
a,
int
b)
{
return
ab
?
a
:
b;
}
這個簡單啊
#includestdio.h
#includemath.h
main()
{
double a,b,c,w;
printf("請輸入三個數(shù)(方程的系數(shù)),中間用空格分開\n");
scanf("%lf%lf%lf",a,b,c);
w=b*b-4*a*c;
if (w0)printf("方程無解\n");
else if(w==0)printf("方程有一個解:x=%lf\n",-b/(2*a));
else printf("方程有兩個解:x1=%lf,x2=%lf\n",(-b+sqrt(w))/(2*a),(-b-sqrt(w))/(2*a));
}
我已經(jīng)按你的意思修改了,也運行出來了,希望對你有幫助,代碼附帶在下面:
#includestdio.h
#includemath.h
float t,x1,x2;
void main()
{
void situ1(float a,float b,float c);
void situ2(float a,float b,float c);
void situ3();
float x,a,b,c;
scanf("%f%f%f",a,b,c);
if (a==0)
{
x=-c/b;
printf("x=%.2f\n",x);
}
else
{
t=b*b-4*a*c;
if (t0)
situ1(a,b,c);
else if(t==0)
situ2(a,b,c);
else
situ3();
}
}
void situ1(float a,float b,float c)
{
x1=(-b+sqrt(t))/(2*a);
x2=(-b-sqrt(t))/(2*a);
printf("x1=%.2f\tx2=%.2f\n",x1,x2);
}
void situ2(float a,float b,float c)
{
x1=x2=(-b+sqrt(t))/(2*a);
printf("x1=x2=%.2f\n",x1);
}
void situ3()
{
printf("沒有實根\n");
}
我在c++里做了一個測試,程序如下:
#includestdio.h
int max(int x,int y){return x+y;}
int max(int x,int y,int z){return x+y+z;}
void main()
{
printf("%d\n",max(1,2));
printf("%d",max(1,2,3));
}
如果定義別的名字相同的函數(shù),可以把它們的參數(shù)設(shè)置成不同的,否則會出現(xiàn)二義性
#include stdio.h
#include stdlib.h
#include math.h
int main()
{
float a,b,c;
float x1,x2,m;
printf("input number a=:");
scanf("%f",a);
printf("input number b=:");
scanf("%f",b);
printf("input number c=:");
scanf("%f",c);
m=b*b-4*a*c;
if(m=0a!=0){
if(m0){
x1=(-b+sqrt(m))/(2*a);
x2=(-b-sqrt(m))/(2*a);
printf("兩根\n");
printf("x1=%f\n",x1);
printf("x2=%f\n",x2);}
else
printf("一根\n");
printf("x1=x2=%f\n",x1);}
else
{
if(a=0 b!=0) printf("根是x=-c/b");
if(a=0b=0) printf("為常函數(shù)");
if(a!=0) printf("無根\n");
}
system("PAUSE");
return 0; }
分享標題:C語言中如何定義二次函數(shù),c語言中如何定義二次函數(shù)圖像
文章鏈接:http://chinadenli.net/article44/heephe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供微信小程序、小程序開發(fā)、靜態(tài)網(wǎng)站、App設(shè)計、品牌網(wǎng)站建設(shè)、App開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)