C语言,求一元二次方程的解

来源:学生作业帮助网 编辑:作业帮 时间:2024/05/03 16:07:49
C语言,求一元二次方程的解

C语言,求一元二次方程的解
C语言,求一元二次方程的解

C语言,求一元二次方程的解
#include "stdio.h"
#include "math.h"
/*求一元二次方程ax*x+bx+c=0的解*/
main()
{
float a,b,c,x1,x2,d;
printf("请输入a:");
scanf("%f",&a);
printf("请输入b:");
scanf("%f",&b);
printf("请输入c:");
scanf("%f",&c);
d=b*b-4*a*c;
if(d < 0)
printf("方程没有实数解.\n");
if (d==0)
{
x1=(-b)/(2*a);
printf("x1=%f\n",x1);
}
if (d>0)
{
x1=(-b+sqrt(d))/(2*a);
x2=(-b-sqrt(d))/(2*a);
printf("x1=%f,x2=%f\n",x1,x2);}
}

#include
#include
void main()
{
double a,b,c,disc,s,x1,x2,p,q,x;
printf("请输入a,b,c:");
scanf("%lf%lf%lf",&a,&b,&c);
disc=sqrt(b*b-4*a*c);
p=-b/(2*a...

全部展开

#include
#include
void main()
{
double a,b,c,disc,s,x1,x2,p,q,x;
printf("请输入a,b,c:");
scanf("%lf%lf%lf",&a,&b,&c);
disc=sqrt(b*b-4*a*c);
p=-b/(2*a);
q=sqrt(b*b-4*a*c)/(2*a);
x1=p+q;
x2=p-q;
x=-c/b;
if(a==0)
printf("x=%5.2lf\n",x);
else
{
if(disc<0)
printf("无实根");
else if(disc==0)
printf("x1=x2=%5.2lf\n",x1);
else
printf("x1=%5.2lf\nx2=%5.2lf\n",x1,x2);
}
}

收起

#include
#include
#include
#include
int func(int a,int b,int c);
int func1(int a,int b,int c);
int main()
{
M:printf("at f...

全部展开

#include
#include
#include
#include
int func(int a,int b,int c);
int func1(int a,int b,int c);
int main()
{
M:printf("at first output the function on the screen,please.");
int q,w,e;
scanf("%d%d%d",&q,&w,&e);
fflush(stdin);
printf("%dx^2+%dy+%d=0.",q,w,e);
char a;
scanf("%c/n",&a);
fflush(stdin);
printf("please tell me the Y or N:%c",a);
if (a=='Y')
{
printf("the function is ok");
}
else
{
printf("the function is worried");
}
int result,result1;
result=func(q,w,e);
result1=func1(q,w,e);
printf("%d%d",result,result1);
char b;
scanf("%c/n",&b);
printf("Do u exectue the function agin(Y/N):%c",b);
getchar();
system("cls");
if (b=='Y')
{
goto M;
}
else
{
return0;
}
return0;
}
int func(int a,int b,int c)
{
int v=pow(b, 2)-4*a*c;
int j=2*a;
int k=(-b)/2*a;
int x1;
if (v>0)
{
printf("the function has the two result.");
x1=k+j*sqrt(v);
}
else
{
printf("the function has not function result.");
}
printf("%d",x1);
return x1;
}
int func1(int a,int b,int c)
{
int v=pow(b, 2)-4*a*c;
int j=2*a;
int k=(-b)/2*a;
int x2;
if (v>0)
{
printf("the function has tow result.");
x2=k+j*sqrt(v);
}
else
{
printf("the function has not result.");
}
return x2;
}

收起