项目二,三角形类2

时间:2022-09-13 16:48:20
/*
*Copyright (c) 2013 ,烟台大学计算机学院
*All rights reserved.
*作者:王至超
*完成日期:2014年03月18
*版本号:v1.0
*问题描述:充分利用类
*样例输入:
*样例输出:
*问题分析:用简单的方法,学会活学活用
*/

#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(void)//取三边的值
{
return a;
}
double getB(void)
{
return b;
}
double getC(void)
{
return c;
}
double perimeter(void)//计算三角形的周长
{
return a+b+c;
}

double area(void)//计算并返回三角形的面积
{
double p;
p=(a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
bool isTriangle()
{
if(a+b>c&&a+c>b&&b+c>a)
{
return 1;
}
else
{
return 0;
}
}
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;
}
项目二,三角形类2