跪求大神编写程序,由键盘输入一元二次方程ax2+bx+c=0(a≠0)的a、b、c的值,求方程的解
2个回答
展开全部
#include <iostream>
#include <cmath>
bool funct(double a,double b,double c,double &x1,double& x2)
{
double coeff = b * b - 4 * a * c;
if (a == 0 || coeff < 0)
return false;
x1 = (-b + sqrt(coeff)) / (2 * a);
x2 = (-b - sqrt(coeff)) / (2 * a);
return true;
}
int main()
{
double x1,x2;
std::cout << "please input 3 numbers a b c " << endl;
double a,b,c;
std::cin >> a >> b >> c;
if (funct(a,b,c,x1,x2))
{
std::cout << "x1 = " << x1 << " x2 = " << x2;
}
else
{
std::cout << "error" << endl;
}
return 0;
}
展开全部
#include <stdio.h>
void main()
{
double a,b,c;
double temp;
double x1,x2;
while(1)
{
printf("请依次输入a b c的值:\n");
scanf("%lf %lf %lf",&a,&b,&c);
if(a==0.0)
{
printf("a=0,请重新输入");
}
else
{
temp=b*b-4*a*c;
if(temp<0.0)
{
printf("无解");
}
else
{
x1=(b*(-1)+temp)/(2*a);
x2=(b*(-1)-temp)/(2*a);
printf("结果:\nx1=%.3lf\nx2=%.3lf\n",x1,x2);
}
}
}
}
void main()
{
double a,b,c;
double temp;
double x1,x2;
while(1)
{
printf("请依次输入a b c的值:\n");
scanf("%lf %lf %lf",&a,&b,&c);
if(a==0.0)
{
printf("a=0,请重新输入");
}
else
{
temp=b*b-4*a*c;
if(temp<0.0)
{
printf("无解");
}
else
{
x1=(b*(-1)+temp)/(2*a);
x2=(b*(-1)-temp)/(2*a);
printf("结果:\nx1=%.3lf\nx2=%.3lf\n",x1,x2);
}
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询