# include stdio.h
成都创新互联专注于巴彦企业网站建设,响应式网站设计,商城网站定制开发。巴彦网站建设公司,为巴彦等地区提供建站服务。全流程按需策划设计,专业设计,全程项目跟踪,成都创新互联专业和态度为您提供的服务
# include math.h
int main(void)
{
double a, b, c;
double delta;
double x1, x2;
char ch;
do
{
printf("请输入一元二次方程的三个系数:\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("有两个解,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("无实数解!\n");
}
printf("您想继续么(Y/N): ");
scanf(" %c", ch); //%c前面必须得加一个空格 原因略
} while ('y'==ch || 'Y'==ch);
return 0;
}
希望可以帮到你,如果满意请采纳!
#include stdio.h
int main(void)
{
double a,b,c,d,e;
double x1,x2;
printf("请输入ax^2+bx +c = 0中a,b,c的值");
scanf("%lf,%lf,%lf",a,b,c);
e = b * b - 4 * a * c;
if (e0) {
printf("无解,请重新输入\n");
scanf("%lf,%lf,%lf",a,b,c);
}
printf("输入正确,正在计算....\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");
}
这是改后正确的代码,以下是你代码的错误:
1.函数如果想在main中声明,不能在执行语句后声明。
2.当调用d(double a...)函数时,e和f作为参数穿过去只是他们的值,在函数内部对它们赋值是不能改变他们内存真正的值的。
3.if(s=0)是对s赋值使s=0,如果是判断应该是if(s==0)。
4.x函数中参数a、b、c没有用到,而且你已经把abc设为全局变量,就不需要把他们当成参数传递,所以我把所有变量都改为局部变量。
5.d函数的算法错误,应该是e=-(b+s)/2a;f=-(b-s)/2a;
6.你没有考虑当a=0,b=0,时的情况。
7.当b2-4ac0时,用sqrt开方就已经错误啦。
写的有些乱,见谅~