#include<iostream>
#include<Cmath>
using namespace std;
class Triangle
{
public:
void setABC(double x, double y, double z);//置三边的值,注意要能成三角形
void getABC(double *x, double *y, double *z);//取三边的值
double perimeter(void);//计算三角形的周长
double area(void);//计算并返回三角形的面积
private:
double a, b, c; //三边为私有成员数据
};
int main()
{
Triangle tri1; //定义三角形类的一个实例(对象)
tri1.setABC(4, 5, 6); //为三边置初值
double x, y, z;
cout << "请输入三边的值:";
cin >> x >> y >> z;
tri1.getABC(&x, &y, &z); //将三边的值为x,y,z赋值
cout << "三条边为:" << x << ‘\t‘ << y << ‘\t‘ << z << endl;
cout << "三角形的周长为:" << tri1.perimeter() << ‘\t‘ << "面积为:" << tri1.area() << endl;
cin >> x;
return 0;
}
void Triangle::setABC(double x, double y, double z)
{
a = x;
b = y;
c = z;
}
void Triangle::getABC(double *x, double *y, double *z)
{
while (*x + *y <= *z || *x + *z <= *y || *y + *z <= *x)
{
cout << "不能构成三角形,请重新输入:";
cin >> *x >> *y >> *z;
}
a = *x; b = *y; c = *z;
}
double Triangle::perimeter()
{
return a + b + c;
}
double Triangle::area()
{
double p;
p = (a + b + c) / 2;
return sqrt(p*(p - a)*(p - b)*(p - c));
}第3周项目-1
原文:http://blog.csdn.net/zhouwengong/article/details/45170861