c语言程序设计 设计一个求解一元二次方程的函数,在主函数中输入方程的系数!!!急!!!!!!!!
2个回答
展开全部
/*
供参考!
*/
# include <stdio.h>
# include <math.h>
void calcu(double a,double b,double c);
int main(void)
{
double a, b, c;
char ch;
do
{
printf("请输入一元二次方程的三个系数:\n");
printf("a=\t");
scanf("%lf", &a);
printf("b=\t");
scanf("%lf", &b);
printf("c=\t");
scanf("%lf", &c);
calcu(a,b,c);
printf("继续(Y/y),输入其他退出: ");
scanf(" %c", &ch);
printf("\n");
} while ('y'==ch || 'Y'==ch);
printf("成功退出!\n");
return 0;
}
void calcu(double a,double b,double c)
{
double delta;
double x1, x2;
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");
}
}
/*
在c++6.0中的运行结果是:
-------------------------
请输入一元二次方程的三个系数:
a= 1
b= -3
c= 2
有两个解,x1 = 2.000000, x2 = 1.000000
继续(Y/y),输入其他退出: y
请输入一元二次方程的三个系数:
a= 1
b= 1
c= 1
无实数解!
继续(Y/y),输入其他退出: y
请输入一元二次方程的三个系数:
a= 1
b= -2
c= 1
有唯一解,x1 = x2 = 1.000000
继续(Y/y),输入其他退出: n
成功退出!
--------------------------------
*/
供参考!
*/
# include <stdio.h>
# include <math.h>
void calcu(double a,double b,double c);
int main(void)
{
double a, b, c;
char ch;
do
{
printf("请输入一元二次方程的三个系数:\n");
printf("a=\t");
scanf("%lf", &a);
printf("b=\t");
scanf("%lf", &b);
printf("c=\t");
scanf("%lf", &c);
calcu(a,b,c);
printf("继续(Y/y),输入其他退出: ");
scanf(" %c", &ch);
printf("\n");
} while ('y'==ch || 'Y'==ch);
printf("成功退出!\n");
return 0;
}
void calcu(double a,double b,double c)
{
double delta;
double x1, x2;
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");
}
}
/*
在c++6.0中的运行结果是:
-------------------------
请输入一元二次方程的三个系数:
a= 1
b= -3
c= 2
有两个解,x1 = 2.000000, x2 = 1.000000
继续(Y/y),输入其他退出: y
请输入一元二次方程的三个系数:
a= 1
b= 1
c= 1
无实数解!
继续(Y/y),输入其他退出: y
请输入一元二次方程的三个系数:
a= 1
b= -2
c= 1
有唯一解,x1 = x2 = 1.000000
继续(Y/y),输入其他退出: n
成功退出!
--------------------------------
*/
展开全部
//用函数调用求一元二次方程的根
#include <stdio.h>
#include <math.h>
void main()
{
void greater(double a, double b, double c);
void equal(double a, double b,double c);
void little(); //进行函数声明
int a,b,c;
double compare;
scanf("%d %d %d",&a,&b,&c);
compare=b*b-4.0*a*c;
if(compare>0)
greater(a,b,c);
if(compare==0)
equal(a,b,c);
if(compare<0)
little(); //调用函数
}
void greater(double a, double b, double c) //当函数有两个根时调用此函数
{
double x1,x2,compare;
compare=b*b-4*a*c;
x1=(-b+sqrt(compare))/(2.0*a);
x2=(-b-sqrt(compare))/(2.0*a);
printf("this equetion has two root:x1=%.3lf,x2=%.3lf\n",x1+0.0005,x2+0.0005);
}
void equal(double a, double b, double c) //当函数有一个根时调用此函数
{
double x;
x=-b/2.0*a;
printf("this equetion has only one root:x=%.3lf\n",x+0.0005);
}
void little()
{
printf("this equetion hasn't root!\n"); //当函数无根是调用,输出this equetion hasn't root!
}
#include <stdio.h>
#include <math.h>
void main()
{
void greater(double a, double b, double c);
void equal(double a, double b,double c);
void little(); //进行函数声明
int a,b,c;
double compare;
scanf("%d %d %d",&a,&b,&c);
compare=b*b-4.0*a*c;
if(compare>0)
greater(a,b,c);
if(compare==0)
equal(a,b,c);
if(compare<0)
little(); //调用函数
}
void greater(double a, double b, double c) //当函数有两个根时调用此函数
{
double x1,x2,compare;
compare=b*b-4*a*c;
x1=(-b+sqrt(compare))/(2.0*a);
x2=(-b-sqrt(compare))/(2.0*a);
printf("this equetion has two root:x1=%.3lf,x2=%.3lf\n",x1+0.0005,x2+0.0005);
}
void equal(double a, double b, double c) //当函数有一个根时调用此函数
{
double x;
x=-b/2.0*a;
printf("this equetion has only one root:x=%.3lf\n",x+0.0005);
}
void little()
{
printf("this equetion hasn't root!\n"); //当函数无根是调用,输出this equetion hasn't root!
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询