展开全部
#include<iostream>
#include<cmath>
using namespace std;
enum type
{
area,
circumference,
};
class triangle
{
public:
triangle(float a,float b,float c)
{
this->a=a;
this->b=b;
this->c=c;
valid=((a+b)>c&&(a+c)>b&&(b+c)>a);
}
float getarea() const
{
float t;
if(valid)
{
t=(a+b+c)*0.5;
return sqrt(t*(t-a)*(t-b)*(t-c));
}
else
throw "不能构成三角形,无法求面积";
}
float getcircumference() const
{
if(valid)
return a+b+c;
else
throw "不能构成三角形,无法求周长";
}
static void cal(triangle &tr,enum type t)
{
try
{
switch(t)
{
case area:
cout<<"面积:"<<tr.getarea()<<endl;
break;
case circumference:
cout<<"周长:"<<tr.getcircumference()<<endl;
break;
}
}
catch(const char *ex)
{
cout<<ex<<endl;
}
}
private:
float a,b,c;
bool valid;
};
int main()
{
float a,b,c;
cout<<"请输入三角形的三条边长:";
cin>>a>>b>>c;
triangle t(a,b,c);
triangle::cal(t,area);
triangle::cal(t,circumference);
return 0;
}
#include<cmath>
using namespace std;
enum type
{
area,
circumference,
};
class triangle
{
public:
triangle(float a,float b,float c)
{
this->a=a;
this->b=b;
this->c=c;
valid=((a+b)>c&&(a+c)>b&&(b+c)>a);
}
float getarea() const
{
float t;
if(valid)
{
t=(a+b+c)*0.5;
return sqrt(t*(t-a)*(t-b)*(t-c));
}
else
throw "不能构成三角形,无法求面积";
}
float getcircumference() const
{
if(valid)
return a+b+c;
else
throw "不能构成三角形,无法求周长";
}
static void cal(triangle &tr,enum type t)
{
try
{
switch(t)
{
case area:
cout<<"面积:"<<tr.getarea()<<endl;
break;
case circumference:
cout<<"周长:"<<tr.getcircumference()<<endl;
break;
}
}
catch(const char *ex)
{
cout<<ex<<endl;
}
}
private:
float a,b,c;
bool valid;
};
int main()
{
float a,b,c;
cout<<"请输入三角形的三条边长:";
cin>>a>>b>>c;
triangle t(a,b,c);
triangle::cal(t,area);
triangle::cal(t,circumference);
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询