<img src="https://img-blog.csdn.net/20150325083335385?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd3l6OTUwNjIw/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />#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;
tri1.getABC (&x,&y,&z); //将三边的值为x,y,z赋值
cout<<"三条边为:"<<x<<'\t'<<y<<'\t'<<z<<endl;
cout<<"三角形的周长为:"<< tri1.perimeter()<<'\t'<<"面积为:"<< tri1.area()<<endl;
return 0;
}
void Triangle::setABC(double x,double y,double z)
{
if(x+y>z&&x+z>y&&y+z>x)
{
a=x;
b=y;
c=z;}
else
cout<<"请重新输入:";
}
void Triangle::getABC(double *x,double *y,double *z)
{
*x=a;
*y=b;
*z=c;
}
double Triangle::perimeter(void)
{
return a+b+c;
}
double Triangle::area(void)
{
double s;
s=a+b+c;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
第三周项目1——三角形类1
最新推荐文章于 2016-03-29 00:43:29 发布