给出三角形三边长,计算三角形面积

#include <cmath>
#include <iostream>
#include <stdexcept>
using namespace std;
//给出三角形三边长,计算三角形面积
double area(double a, double b,
            double c) throw(invalid_argument) { //判断三角形边长是否为正
  if (a <= 0 || b <= 0 || c <= 0)
    throw invalid_argument("the side length should be positive");
  //判断三边长是否满足三角不等式
  if (a + b <= c || b + c <= a || c + a <= b)
 throw invalid_argument("the side length should fit the triangle inequation"); 
 //由Heron公式计算三角形面积 
  double s = (a + b + c) / 2; 
 return sqrt(s * (s - a) * (s - b) * (s - c));
}
int main() {
  double a, b, c; //三角形三边长
  cout << "Please input the side lengths of a triangle: ";
  cin >> a >> b >> c;
  try {
    double s = area(a, b, c); //尝试计算三角形面积
    cout << "Area: " << s << endl;
  } catch (exception &e) {
    cout << "Error: " << e.what() << endl;
  }
  return 0;
}

 

上一篇:使用layer 弹出对话框 子父页面相互参数传递 父页面获取子页面参数实例


下一篇:java中的输入流(Scanner),数据类型,运算符,switch,数组的用法