一元二次方程

#include <iostream>
#include <cmath>
using namespace std;

double max(double a, double b) { return (a > b) ? a : b; }
double min(double a, double b) { return (a > b) ? b : a; }

int main()
{
	double a, b, c;
	while (cin>>a>>b>>c)
	{
		double res1 = 0, res2 = 0;
		if ((b*b - 4 * a*c) < 0)
		{
			cout << "No solution!" << endl;
			continue;
		}

        res1 = (-b + sqrt(b*b - 4 * a*c)) / (2* a);
        res2 = (-b - sqrt(b*b - 4 * a*c)) / (2 * a);
        if (fabs(res1)<1e-6) res1 = 0;
        if (fabs(res2)<1e-6) res2 = 0;
        printf("x1=%.2lf, x2=%.2lf\n", max(res1, res2), min(res1, res2));//一定要写max和min因为2a可能为负


	}
}

猜你喜欢

转载自blog.csdn.net/dzydzy7/article/details/80517515