include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
inline void setA(double x)//置三边的值,注意要能成三角形
{
a=x;
}
inline void setB( double x)
{
b=x;
}
inline void setC( double x)
{
c=x;
}
inline int getA()//取三边的值
{
return a;
}
inline int getB()
{
return b;
}
inline int getC()
{
return c;
}
void setABC(double x, double y, double z);//置三边的值,注意要能成三角形
void getABC(double *x, double *y, double *z);//取三边的值
bool isTriangle();
double perimeter(void);//计算三角形的周长
double Area(void);//计算并返回三角形的面积
private:
double a,b,c; //三边为私有成员数据
};
int main()
{ Triangle tri1; //定义三角形类的一个实例(对象)
double x,y,z;
cout<<"请输入三角形的三边:";
cin>>x>>y>>z;
tri1.setA(x);tri1.setB(y);tri1.setC(z); //为三边置初值
if(tri1.isTriangle())
{
cout<<"三条边为:"<<tri1.getA()<<','<<tri1.getB()<<','<<tri1.getC()<<endl;
cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.Area()<<endl;
}
else
cout<<"不能构成三角形"<<endl;
system("pause");
return 0;
}
bool Triangle::isTriangle()
{
if((a+b)>c&&(a-b)<c)
return true;
else
return false;
}
void Triangle::setABC(double x ,double y, double z)
{
if(x+z>y&&y+x>z&&z+y>x)
{ a=x;
b=y;
c=z;
}else{
a=0;
b=0;
c=0;
}
}
void Triangle::getABC(double *x,double*y,double*z)
{
*x=a;
*y=b;
*z=c;
}
double Triangle::perimeter(void)
{
return a+b+c;
}
double Triangle::Area(void)
{
double p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
运行结果: