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

#include<iostream>
#include<math.h>
using namespace std;
void equation_1(int a,int b,int c);
void equation_2(int a,int b,int c);
void equation_3(int a,int b,int c);
int main()
{
	cout<<"请输入二元方程的系数:"<<endl;
	int a,b,c;
	double temp;
	cin>>a>>b>>c;
	cout<<"方程为:"<<a<<"x*x"<<"+"<<b<<"x"<<"+"<<c<<"=0"<<endl;
	temp=b*b-4*a*c;
	if(temp>0)
	{
		equation_1(a,b,c);
	}
	if(temp==0)
	{
		equation_2(a,b,c);
	}
	if(temp<0)
	{
		equation_3(a,b,c);
	}
	return 0;
}
void equation_1(int a,int b,int c)
{
	double x1,x2,temp;
	temp=b*b-4*a*c;
	x1=(-b+sqrt(temp))/(2*a);
	x2=(-b-sqrt(temp))/(2*a);
	cout<<"两个不相等的实数跟"<<endl;
	cout<<"x1="<<x1<<","<<"x2="<<x2<<endl; 
}
void equation_2(int a,int b,int c)
{
	double x1,x2,temp;
	temp=b*b-4*a*c;
	x1=(-b+sqrt(temp))/(2*a);
	x2=x1;
	cout<<"两个相等的实数跟"<<endl;
	cout<<"x1=x2="<<x1<<endl; 
}
void equation_3(int a,int b,int c)
{
	cout<<"没有实数跟"<<endl; 
}

猜你喜欢

转载自blog.csdn.net/qq_41404557/article/details/85310548