求三角形的面积

1、数学知识

  求三角形的面积

  三边的边长分别为:a, b, c;

  公式:s = (a + b + c) / 2;

  area = √s * ( s - a) * (s - b) * (s -c);

2、源代码

#include<iostream>
#include<cmath>
using namespace std;
bool TriangleArea(double a, double b, double c, double &area)
{
     if(a + b <= c || fabs(a - b) >= c || a <= 0 || b <= 0 || c <= 0)
     {
         return false;
     }
     double s;
     s = (a + b + c) / 2.0;
     area = sqrt(s * (s - a) * (s - b) * (s - c));
     return true;
}

void main()
{
     double area, a, b, c;
     while(cin >> a >> b >> c)
     {
          area = 0;
          if(a == 0 && b == 0 && c == 0)
          {
              break;
          }
          if(TriangleArea(a, b, c, area))
          {
              cout << "面积为: " << area << endl;
          }
          else
          {
              cout << "不能够成三角形. " << endl;
          }
     }
}

 

posted on 2014-08-16 22:12  BestNow  阅读(245)  评论(0编辑  收藏  举报

导航