The Area of an Arbitrary Triangle-任意三角形的面积,允许重复计算:
//The Area of an Arbitrary Triangle-任意三角形的面积 #include<iostream> #include<cmath> using namespace std; bool IsTriangle(double a,double b,double c); void areafun(double a,double b,double c,double& area,double& s); int main() { double a,b,c,s,area; char ans; do{ cout<<"Enter the three length of the Triangle:\n"; cin>>a>>b>>c; if(IsTriangle(a,b,c)) areafun(a,b,c,area,s); else cout<<"The three length are error!\n"; cout<<"Do you want again?"; cin>>ans; }while(‘Y‘ == ans || ‘y‘ == ans); return 0; } bool IsTriangle(double a,double b,double c) { if((a+b>c && a+c>b && b+c>a && abs(a-b)<c && abs(a-c)<b && abs(b-c)<a) ) return 1; return 0; } void areafun(double a,double b,double c,double& area,double& s) { s = (a+b+c)/2; area = sqrt(s*(s-a)*(s-b)*(s-c)); cout<<"The area of the Triangle is:"<<area<<endl; }
结果:
Enter the three length of the Triangle: 3 4 5 The area of the Triangle is:6 Do you want again?y Enter the three length of the Triangle: 3 4 4 The area of the Triangle is:5.56215 Do you want again?y Enter the three length of the Triangle: 3 4 9 The three length are error! Do you want again?y Enter the three length of the Triangle: 4 4 4 The area of the Triangle is:6.9282 Do you want again?
时间: 2024-10-21 05:13:57