首页 > 代码库 > The Area of an Arbitrary Triangle-任意三角形的面积
The Area of an Arbitrary Triangle-任意三角形的面积
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?
The Area of an Arbitrary Triangle-任意三角形的面积
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。