#include <iostream>
#include <Cmath>
using namespace std;
class Triangle
{
public:
void setA(double x)
{
a=x;
}
void setB(double y)
{
b=y;
}
void setC(double z)
{
c=z;
}
int getA()
{
return a;
}
int getB()
{
return b;
}
int getC()
{
return c;
}
double perimeter(void);//计算三角形周长
double area(void);//计算并返回三角形的面积
bool isTriangle();
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.getB()<<endl;
cout<<"三角形的周长为:"<<tri1.perimeter()<<'\t'<<"面积为:"<<tri1.area()<<endl;
}
else
cout<<"不能构成三角形"<<endl;
return 0;
}
bool Triangle::isTriangle()
{
if((a+b>c)&(a+c>b)&(b+c>a))//若两边之和大于第三边且两边之差小于第三边则能构成三角形
return true;
return false;
}
double Triangle::perimeter()//周长
{
return a+b+c;
}
double Triangle::area()//面积
{
double p,s;
p=(a+b+c)/2;
s=sqrt(p*(p-a)*(p-b)*(p-c));
return s;
}
运行结果: