#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double m(int a, int b, int c);
double n(int a, int b, int c);
double p(int a, int b, int c);
double a, b, c, x, x1, x2, d;
cout << "please enter the a,b,c:";
cin >> a >> b >> c;
d = b * b - 4 * a*c;
if(d==0)
{
x = p(a, b, c);
cout << "x=" << x << endl;
}
else if (d > 0)
{
x1 = m(a, b, c);
x2 = n(a, b, c);
cout << "x1=" << x1 << endl;
cout<<"x2="<<x2<<endl;
}
else
cout << "No root!" << endl;
system("pause");
return 0;
}
double m(int a, int b, int c)
{
double w;
w = (-b + sqrt(b*b - 4 * a*c))/ (2 * b);
return w;
}
double n(int a, int b, int c)
{
double e;
e = (-b -sqrt(b*b - 4 * a*c)) / (2 * b);
return e;
}
double p(int a, int b, int c)
{
double r;
r = -b / (2 * a);
return r;
}
用三个函数求方程的根的算法实现
于 2022-05-02 19:01:50 首次发布