ヘレンの公式は三角形の面積C++を計算します

874 ワード

ヘレン式:s=sqrt(p*(p-a)(p-b)(p-c))平面内には、辺長がそれぞれa,b,cの三角形があると仮定し、三角形の面積Sは、s=sqrt(p*(p-a)(p-b)(p-c))から求めることができる.式のpは半周長(周長の半分):p=1/2(a+b+c)である.
C++コード:
#include 
#include 

using namespace std;


int main()
{
	//  n 
	int n;
	double a[3];

	cout << "Input number:";
	cin >> n;

	while(n--){  //      

		for(int i=0; i<3; i++){
			cin >> a[i];
		}

		double a1=a[0];
		double b=a[1];
		double c=a[2];
		double p=a1+b+c;
		//cout << a1 << b << c << p << endl;

		if(a1+b<=c || a1+c<=b || b+c<=a1){
			cout << "0.00" << endl;
		}
		else if(a1-b>=c || a1-c>=b || b-c>=a1){
			cout << "0.00" << endl;
		}
		else{
			int s=sqrt(p*(p-a1)*(p-b)*(p-c));
			cout << s<< endl;
		}
	}

	return 0;
}