1014: 求三角形的面积

54 篇文章 3 订阅

使用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;
}

程序运行结果如下:

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值