1055: C++:重载函数2(计算面积)

题目描述
定义重载函数area(),分别计算正方形、长方形和三角形的面积。

输入
共计有3行。

第1行有一个实数,为正方形的边长;

第2行有二个实数,为长方形的两条边长;

第3行有三个实数,为三角形的三边长。

输出
正方形、长方形和三角形的面积(保留3位小数)。
样例输入 Copy
3.5
4.2 5.62
3.0 4.0 5.0
样例输出 Copy
12.250
23.604
6.000

# include <iostream>
using namespace std;
# include <iomanip>
# include <math.h>

void area(double a)
{
	double area = a * a;
	cout << fixed << setprecision(3) << area << endl;
}

void area(double a, double b)
{
	double area = a * b;
	cout << fixed << setprecision(3) << area << endl;
}

void area(double a, double b, double c)
{
	double s =  (a + b + c)/2;
	double area = sqrt(s * (s - a) * (s - b) * (s - c));

	cout << fixed << setprecision(3) << area << endl;
}

int main()
{
	double a, b, c, d, e, f;
	
	cin >> a;
	area(a);

	cin >> b >> c;
	area(b, c);

	cin >> d >> e >> f;
	area(d, e, f);

	return 0;
}
  • 10
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值