第三周上机项目2三角形类2

来源:互联网 发布:java 注册短信验证码 编辑:程序博客网 时间:2024/05/06 15:42
/*  *Copyright (c) 2015, 烟台大学计算机学院  *All rights reserved.  *文件名称:text.cpp  *作者:陈栋梁  *完成日期:2015年 3 月 25 日  *版本号:v1.0  *  */  #include<iostream>#include<Cmath>#include<cstdlib>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;    }    bool isTriangle();    double perimeter(void);    double area(void);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;}bool Triangle::isTriangle(){    bool p=false;    if((a+b>c)&&(a+c>b)&&(b+c>a))    {        p=true;    }    return p;}double Triangle::perimeter(void){    return a+b+c;}double Triangle::area(void){    double p=0;    p=(a+b+c)/2;    return sqrt(p*(p-a)*(p-b)*(p-c));}


运行结果:

0 0