#include <iostream>
#include <cmath>
using namespace std;
class Triangle
{
public:
Triangle(double x = 1.0, double y = 1.0, double z = 1.0): a(x),b(y),c(z) {}
void setABC(double x, double y, double z);
double perimeter();
double area();
void setA(double x) {a = x;}
void setB(double y) {b = y;}
void setC(double z) {c = z;}
double getA() {return a;}
double getB() {return b;}
double getC() {return c;}
void showMessage();
bool isTriangle()
{
if(a+b > c && a+c > b && b+c > a)
return true;
else
return false;
}
private:
double a,b,c;
};
void Triangle::showMessage()
{
if(isTriangle())
{
cout << "三条边为:" << a << "," << b << "," << c << endl;
cout << "三角形的周长为:" << perimeter() << "\t" << "面积为:" << area() << endl;
}
else
cout << "不能构成三角形" << endl;
}
void Triangle::setABC(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
double Triangle::perimeter()
{
return a+b+c;
}
double Triangle::area()
{
double p = (a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
};
int main()
{
Triangle Tri1;
Tri1.showMessage();
Triangle Tri2(1.5);
Tri2.showMessage();
Triangle Tri3(1.5,1.5);
Tri3.showMessage();
Triangle Tri4(7,8,9);
Tri4.showMessage();
return 0;
}
运行结果: