使用C++编写程序
题目描述
给出三角形的三条边,求三角形的面积。
输入
输入三角形的三条边长(实数),数据之间用空格隔开。
输出
输出三角形的面积,结果保留2位小数。
样例输入 Copy
2.5 4 5
样例输出 Copy
4.95
提示
用海伦公式或其他方法均可。
程序代码如下:
#include<iostream>
#include<iomanip>
#include<cmath>
#define ElemType double
using namespace std;
class Triangle
{
public:
Triangle(ElemType a, ElemType b, ElemType c) :A(a), B(b), C(c) {};
void GetArea();
private:
ElemType A, B, C;
};
inline void Triangle::GetArea()
{
ElemType P;
P = (A + B + C) / 2.0;
cout.setf(ios::fixed); //设置输出为浮点数
cout << setprecision(2) << sqrtf(P*(P - A)*(P - B)*(P - C)); //海伦公式
}
int main()
{
ElemType a, b, c;
cin >> a >> b >> c;
Triangle T(a, b, c);
T.GetArea();
return 0;
}