函数:求方程ax2+bx+c=0的根,用3个函数分别求当b2-4ac大于0,等于0,和小于0时的根并输出结果。从主函数中输入a,b,c。

#include
using namespace std;
#include
void fun1(int a, int b, int c, int s)
{
	cout << (-b + sqrt(s)) / 2 * a<<","<< (-b - sqrt(s)) / 2 * a;
}
void fun2(int a, int b, int c, int s)
{
	cout << (-b + sqrt(s)) / 2 * a << "," << (-b + sqrt(s)) / 2 * a;
}
void fun3(int a,int b,int c,int s)
{
	cout << "b2-4ac<0,方程无解" << endl;
}
int main()
{
	int a, b, c;
	int s;
	cout << "请输入a,b,c:" << endl;
	cin >> a >> b >> c;
	s = b * b - 4 * a * c;
	if (s > 0)
		fun1(a,b,c,s);
	else if (s == 0)
		fun2(a,b,c,s);
	else
		fun3(a,b,c,s);
	return 0;
}

你可能感兴趣的:(c语言,c++,算法)