飛飛(二十二)求三角形周长和面积升级版

#include <iostream>
#include"cmath"

using namespace std;
class Triangle
{
public:
      void setABC(double x,double y,double z)
      {
            a=x;
            b=y;
            c=z;
      }
      double perimeter();
      double area();
      bool istriangle()
      {
            if((a>0&&b>0&&c>0)&&(a+b>c&&a+c>b&&b+c>a))
                  return true;
            else
                  return false;
      }
      private:
      double a,b,c;
};
int main()
{
      Triangle tril;
      double x,y,z;
      cout<<"请输入三边: ";
      cin>>x>>y>>z;
      tril.setABC(x,y,z);
      if(tril.istriangle()){
      cout <<"三角形的周长为: "<<tril.perimeter()<<"面积为: "<<tril.area()<<endl;

      }
      else
            {
                  cout<<"不构成三角形"<<endl;
            }

      return 0;
}
double Triangle::perimeter()
{
      return (a+b+c);
}
double Triangle:: area()
{
      return sqrt(((a+b+c)/2)*((a+b+c)/2-a)*((a+b+c)/2-b)*((a+b+c)/2-c));
}
<img src="http://img.blog.csdn.net/20160410150441828?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
 
 

你可能感兴趣的:(C++)