#include<iostream>
#include "Triangle.h"
using namespace std;
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;
return 0;
}
/主函数
#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
private:
double a;
double b;
double c;
public:
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;
}
bool isTriangle();
double perimeter();
double area();
};
/头文件
#include "Triangle.h"
#include <Cmath>
bool Triangle::isTriangle()
{
if(a+b<=c)
{
return 0;
}
else if(a+c<=b)
{
return 0;
}
else if(b+c<=a)
{
return 0;
}
else
{
return 1;
}
}
double Triangle::perimeter()
{
return a+b+c;
}
double Triangle::area()
{
double q,m;
q=(a+b+c)/2;
m=sqrt(q*(q-a)*(q-b)*(q-c));
return m;
}
/其他成员函数