C语言-解方程
描述
求方程 的根,用三个函数分别求当b^2-4ac大于0、等于0、和小于0时的根,并输出结果。从主函数输入a、b、c的值。
输入
a b c
输出
x1=? x2=?
输入样例 1
4 1 1
输出样例 1
x1=-0.125+0.484i x2=-0.125-0.484i
套公式
#include <iostream>
#include <cmath>
#include <stdio.h>
using namespace std;
void getanswer1(double a,double b,double x) //>0
{
printf("x1=%.3f x2=%.3f\n",(-b+sqrt(x))/(2.00*a),(-b-sqrt(x))/(2.00*a));
}
void getanswer2(double a,double b,double x) //=0
{
printf("x1=%.3f x2=%.3f\n",(-b+sqrt(x))/(2.00*a),(-b-sqrt(x))/(2.00*a));
}
void getanswer3(double a,double b,double x) //<0
{
printf("x1=%.3f+%.3fi x2=%.3f-%.3fi\n",(-b)/(2.00*a),sqrt(-x)/(2.00*a),(-b)/(2.00*a),sqrt(-x)/(2.00*a));
}
int main()
{
double a,b,c;
while(cin>>a>>b>>c)
{
double x=b*b-4*a*c;
if(x>0)
getanswer1(a,b,x);
else if(x==0)
getanswer2(a,b,x);
else
getanswer3(a,b,x);
}
return 0;
}