面向对象c++——三角形求周长和面积

作者在 2008-04-08 15:18:24 发布以下内容

这几天放假耍了几天,没有ACM题可贴,就只有贴作业了,很水的作品请指教

 

源代码:

 

/***************************************
          c++编程题
 定义一个三角形类求三角形面积和周长
          keloy    2008.4.7
****************************************/
#include <iostream>
#include <math.h>
using namespace std;
class Ctriangle                                                    //定义三角形类
{
    private:
  double S1,S2,S3;
 public:
  Ctriangle(int x=0,int y=0,int z=0)
  {
   this->S1=x;
   this->S2=y;
   this->S3=z;
  }
  double Girth();                                          //三角形周长
  bool Istriangle();                                       //判断是否是三角形
  double Area();                                           //三角形面积
  void init_triangle();                                    //输入三角形边长
};
bool Ctriangle::Istriangle()                                     //判断是否是三角形
{
 if((S1+S2)>S3&&(S2+S3)>S1&&(S1+S3)>S2)//使用两边之和大于第三边判断
 {
  cout<<"S1="<<S1<<" S2="<<S2<<" S3="<<S3<<"."<<endl;//合法输出边长
  return true;
 }
 else
 {
  cout<<"Input is illegal."<<endl;//不合法输出错误
  return false;
 }
}
void Ctriangle::init_triangle()                                 //输入三角形边长
{
 cout<<"please input three edge of the triangle."<<endl;
   cin >>S1>>S2>>S3;
}
double Ctriangle::Girth()                                       //求周长
{
 return S1+S2+S3;
}
double Ctriangle::Area()                                        //求面积
{
     double l;
  l=(S1+S2+S3)/2;
  return sqrt(l*(l-S1)*(l-S2)*(l-S3));//使用海伦公式
}
int main()
{
 Ctriangle t;
 t.init_triangle();
 if(t.Istriangle())
 {
  cout<<"The area of triangle is "<<t.Area()<<endl;
  cout<<"The girth of triangle is "<<t.Girth()<<endl;
 }
    return 0;
}

 

这还是我头一次在这么短代码里面写这么多的注释。

哎,AcM题写多了,养成的坏习惯,老师点名要我改。

写注释是个好习惯,要多写注释。

数据结构 | 阅读 11089 次
文章评论,共0条
游客请输入验证码
浏览255939次