第四次上机实践项目-项目1-三角形类椎体-(4)



问题及代码:

/*

*文件名称:三角形类椎体

*作者:刘涛

完成日期:2016.3.26

*

*问题描述:输入三边,计算周长和面积

*输入描述:三个边长

程序输出:周长,面积

*/

#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; 
}  


运行及结果:

第四次上机实践项目-项目1-三角形类椎体-(4)_第1张图片


你可能感兴趣的:(第四次上机实践项目-项目1-三角形类椎体-(4))