01./*
02.* 程序的版权和版本声明部分
03.* Copyright (c)2013, 烟台大学计算机学院学生
04.* All rightsreserved.
05.* 文件名称: Triangle.cpp
06.* 作 者:赵冠哲
07.* 完成日期:2013年3月22日
08.* 版本号: v1.0
09.* 输入描述:
10.* 问题描述:
11.*/
#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;
}
double getA()
{
return a;
}
double getB()
{
return b;
}
double getC()
{
return c;
}
double perimeter()
{
return(a+b+c);
}
double area()
{
double s;
s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
bool isTriangle()
{
return ((b+c>a) && (a+b>c) && (a+c>b))?true:false;
}
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;
return 0;
}
运行结果: