# include stdio.h
成都創(chuàng)新互聯(lián)專注于巴彥企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站設(shè)計(jì),商城網(wǎng)站定制開發(fā)。巴彥網(wǎng)站建設(shè)公司,為巴彥等地區(qū)提供建站服務(wù)。全流程按需策劃設(shè)計(jì),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
# include math.h
int main(void)
{
double a, b, c;
double delta;
double x1, x2;
char ch;
do
{
printf("請(qǐng)輸入一元二次方程的三個(gè)系數(shù):\n");
printf("a = ");
scanf("%lf", a);
printf("b = ");
scanf("%lf", b);
printf("c = ");
scanf("%lf", c);
delta = b*b - 4*a*c;
if (delta 0)
{
x1 = (-b + sqrt(delta)) / (2*a);
x2 = (-b - sqrt(delta)) / (2*a);
printf("有兩個(gè)解,x1 = %lf, x2 = %lf\n", x1, x2);
}
else if (0 == delta)
{
x1 = x2 = (-b) / (2*a);
printf("有唯一解,x1 = x2 = %lf\n", x1, x2);
}
else
{
printf("無(wú)實(shí)數(shù)解!\n");
}
printf("您想繼續(xù)么(Y/N): ");
scanf(" %c", ch); //%c前面必須得加一個(gè)空格 原因略
} while ('y'==ch || 'Y'==ch);
return 0;
}
希望可以幫到你,如果滿意請(qǐng)采納!
#include stdio.h
int main(void)
{
double a,b,c,d,e;
double x1,x2;
printf("請(qǐng)輸入ax^2+bx +c = 0中a,b,c的值");
scanf("%lf,%lf,%lf",a,b,c);
e = b * b - 4 * a * c;
if (e0) {
printf("無(wú)解,請(qǐng)重新輸入\n");
scanf("%lf,%lf,%lf",a,b,c);
}
printf("輸入正確,正在計(jì)算....\n");
d = sqrt(e);
x1 = (-b + d)/(2 * a);
x2 = (-b - d)/(2 * a);
printf("x1=%f\n",x1);
printf("x2=%f\n",x2);
return 0;
}
#include stdio.h
#include math.h
#include malloc.h
void main()
{
double a,b,c;
double s;
double *e,*f;
void d(double a ,double b,double c,double s,double* e,double* f);
void de(double a ,double b,double c,double *e);
void x();
e=(double*)malloc(sizeof(double));
f=(double*)malloc(sizeof(double));
*e=0;
*f=0;
printf("Please input coefficient:\n");
scanf("%lf%lf%lf",a,b,c);
if(a==0)
{
if(b==0c==0)
printf("x is any number");
if(b==0c!=0)
x();
if(b!=0)
{
*e=-c/b;
printf("%lf",*e);
}
}
else
{
s=(b*b-4*a*c);
if(s0)
x();
else
{
s=sqrt(s);
printf("The equation is %lf x^2 + %f x + %f\n",a,b,c);
printf("s=%lf\n",s);
if(s0)
{
d(a,b,c,s,e,f);
printf("%lf\n",*e);
printf("%lf\n",*f);
}
else if(s==0)
{
de(a,b,c,e);
printf("%lf\n",*e);
}
}
}
}
void d(double a ,double b,double c,double s,double *e ,double *f)
{
double m,n;
m=-0.5*(b+s)/a;
n=-0.5*(b-s)/a;
*e=m;
*f=n;
}
void de(double a ,double b,double c,double *e)
{
double m;
m=-0.5*b/a;
*e=m;
}
void x()
{
printf("The equation has no root!\n");
}
這是改后正確的代碼,以下是你代碼的錯(cuò)誤:
1.函數(shù)如果想在main中聲明,不能在執(zhí)行語(yǔ)句后聲明。
2.當(dāng)調(diào)用d(double a...)函數(shù)時(shí),e和f作為參數(shù)穿過去只是他們的值,在函數(shù)內(nèi)部對(duì)它們賦值是不能改變他們內(nèi)存真正的值的。
3.if(s=0)是對(duì)s賦值使s=0,如果是判斷應(yīng)該是if(s==0)。
4.x函數(shù)中參數(shù)a、b、c沒有用到,而且你已經(jīng)把a(bǔ)bc設(shè)為全局變量,就不需要把他們當(dāng)成參數(shù)傳遞,所以我把所有變量都改為局部變量。
5.d函數(shù)的算法錯(cuò)誤,應(yīng)該是e=-(b+s)/2a;f=-(b-s)/2a;
6.你沒有考慮當(dāng)a=0,b=0,時(shí)的情況。
7.當(dāng)b2-4ac0時(shí),用sqrt開方就已經(jīng)錯(cuò)誤啦。
寫的有些亂,見諒~