
C语言,求一元二次方程的解
8个回答
展开全部
我这个是可以无限循环求解的,如果不需要无限循环,可以把do while给删掉即可
#include <stdio.h>
#include <math.h>
int main(void)
{
float a,b,c;
float x1,x2;
float delta;
do
{
printf("请输入一元二次方程的三个系数a,b,c\n");
printf("请输入系数a=\n");
scanf("%lf",&a);
printf("请输入系数b=\n");
scanf("%lf",&b);
printf("请输入系数c=\n");
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(delta==0)
{
x1=x2=(-b)/(2*a);
printf("该方程有唯一解,x1=x2=%lf\n",x1,x2);
}
else
printf("该方程无解");
}
while(1);
return 0;
}
#include <stdio.h>
#include <math.h>
int main(void)
{
float a,b,c;
float x1,x2;
float delta;
do
{
printf("请输入一元二次方程的三个系数a,b,c\n");
printf("请输入系数a=\n");
scanf("%lf",&a);
printf("请输入系数b=\n");
scanf("%lf",&b);
printf("请输入系数c=\n");
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(delta==0)
{
x1=x2=(-b)/(2*a);
printf("该方程有唯一解,x1=x2=%lf\n",x1,x2);
}
else
printf("该方程无解");
}
while(1);
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2020-03-30
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <complex.h>
void main()
{
double a,b,c,k,x,x1r,x1i,x2r,x2i;
double _Complex x1,x2;
const double EPS=1e-6;
while (1)
{
printf("解一元二次方程:\n ax^2+bx+c=0 \na = ");
scanf("%lf",&a);
printf("\nb = ");
scanf("%lf",&b);
printf("\nc = ");
scanf("%lf",&c);
printf("\n");
if (fabs(b*b-4*a*c)<EPS)
{
printf("x=");
x=-b;
x=x/2;
x=x/a;
printf("%lf\n\n",x);
system("pause");
system("cls");
}
else
{
x1=cpow(b*b-4*a*c,0.5)-b;
x1=x1/2;
x1=x1/a;
x2=-cpow(b*b-4*a*c,0.5)-b;
x2=x2/2;
x2=x2/a;
if (fabs(b*b-4*a*c)==b*b-4*a*c)
{
printf("x1=%lf\n",x1);
printf("x2=%lf\n",x2);
system("pause");
system("cls");
}
else
{
x1r=creal(x1);
x1i=cimag(x1);
x2r=creal(x2);
x2i=cimag(x2);
if (fabs(x1r)<EPS)
{
if (fabs(x1i-1)<EPS)
{
printf("x1=i");
}
else
{
printf("x1=%lfi",x1i);
}
}
else
{
if (fabs(x1i-1)<EPS)
{
printf("x1=%lf+i",x1r);
}
else
{
if (fabs(fabs(x1i)-x1i)<EPS)
{
printf("x1=%lf+%lfi",x1r,x1i);
}
else
{
printf("x1=%lf%lfi",x1r,x1i);
}
}
}
printf("\n");
if (fabs(x2r)<EPS)
{
if (fabs(x2i-1)<EPS)
{
printf("x2=i");
}
else
{
printf("x2=%lfi",x2i);
}
}
else
{
if (fabs(x2i-1)<EPS)
{
printf("x2=%lf+i",x2r);
}
else
{
if (fabs(fabs(x2i)-x2i)<EPS)
{
printf("x2=%lf+%lfi",x2r,x2i);
}
else
{
printf("x2=%lf%lfi",x2r,x2i);
}
}
}
system("pause");
system("cls");
}
}
}
}
#include <stdlib.h>
#include <math.h>
#include <complex.h>
void main()
{
double a,b,c,k,x,x1r,x1i,x2r,x2i;
double _Complex x1,x2;
const double EPS=1e-6;
while (1)
{
printf("解一元二次方程:\n ax^2+bx+c=0 \na = ");
scanf("%lf",&a);
printf("\nb = ");
scanf("%lf",&b);
printf("\nc = ");
scanf("%lf",&c);
printf("\n");
if (fabs(b*b-4*a*c)<EPS)
{
printf("x=");
x=-b;
x=x/2;
x=x/a;
printf("%lf\n\n",x);
system("pause");
system("cls");
}
else
{
x1=cpow(b*b-4*a*c,0.5)-b;
x1=x1/2;
x1=x1/a;
x2=-cpow(b*b-4*a*c,0.5)-b;
x2=x2/2;
x2=x2/a;
if (fabs(b*b-4*a*c)==b*b-4*a*c)
{
printf("x1=%lf\n",x1);
printf("x2=%lf\n",x2);
system("pause");
system("cls");
}
else
{
x1r=creal(x1);
x1i=cimag(x1);
x2r=creal(x2);
x2i=cimag(x2);
if (fabs(x1r)<EPS)
{
if (fabs(x1i-1)<EPS)
{
printf("x1=i");
}
else
{
printf("x1=%lfi",x1i);
}
}
else
{
if (fabs(x1i-1)<EPS)
{
printf("x1=%lf+i",x1r);
}
else
{
if (fabs(fabs(x1i)-x1i)<EPS)
{
printf("x1=%lf+%lfi",x1r,x1i);
}
else
{
printf("x1=%lf%lfi",x1r,x1i);
}
}
}
printf("\n");
if (fabs(x2r)<EPS)
{
if (fabs(x2i-1)<EPS)
{
printf("x2=i");
}
else
{
printf("x2=%lfi",x2i);
}
}
else
{
if (fabs(x2i-1)<EPS)
{
printf("x2=%lf+i",x2r);
}
else
{
if (fabs(fabs(x2i)-x2i)<EPS)
{
printf("x2=%lf+%lfi",x2r,x2i);
}
else
{
printf("x2=%lf%lfi",x2r,x2i);
}
}
}
system("pause");
system("cls");
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <stdio.h>
#include <math.h>
#include <windows.h>
#include <system.h>
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;
}
#include <math.h>
#include <windows.h>
#include <system.h>
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询