1元のプログラミング言語を二次方程式の根を解く、採用することを願っています!!!

1.fabs();例えば、絶対値関数である:Eが表しファブ(N)<1E-6は、ゼロに近い、10,1e-6(-6)10番目を表し、この式のデフォルトそう数がゼロに等しいかどうかを決定するために使用されます。 

//求一元二次方程的根
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
	float a, b, c,d, x1, x2;
	cout << "input a ,b,c:" << endl;
	cin >> a >> b >> c;
	d = b * b - 4 * a * c;
	if (fabs(a)<1e-6)
	{
		cout << "这个方程不是一元二次方程";
	}
	else if
		(d<1e-6)
	{
		cout << "x1=x2="<<((-b) / (2 * a)) << endl;
	}
	else
	{
		x1 = (-b + sqrt(d)) / (2 * a);
		x2 = (-b - sqrt(d))/ (2 * a);
		cout << "x1="<<x1 << endl;
		cout << "x2=" << x2 << endl;
	}
	return 0;
}

 

公開された17元の記事 ウォンの賞賛9 ビュー186

おすすめ

転載: blog.csdn.net/shnagmiao/article/details/104573714