Description
设计重载函数Area,分别实现圆、长方形和三角形面积的求解。
其中:
double Area(double r)用于圆的面积求解(PI=3.1416);
double Area(double len, double wid)用于长方形面积的求解;
double Area(double a, double b, double c)用于三角形面积的求解(注意采用海伦公式求解)。
Input
输入圆的半径,长方形的长与宽,三角形的三条边长。
Output
圆、长方形和三角形的面积。
Sample Input 1
2 3.2 4.5 3 4 5
Sample Output 1
12.5664 14.4 6
**************************************************************************************************************
代码实现
#include<iostream>
#include<iomanip>
#include<string>
#include<cmath>
#define PI 3.1416
using namespace std;
double Area(double r);
double Area(double len,double wid);
double Area(double a,double b,double c);
int main()
{
double r,len,wid,a,b,c;
double area1,area2,area3;
cin >> r ;
cin >> len >> wid ;
cin>> a >> b >> c ;
area1 = Area(r);
area2 = Area(len,wid);
area3 = Area(a,b,c);
cout<<area1<<endl<<area2<<endl<<area3;
return 0;
}
double Area(double r)
{
return (r*r*PI);
}
double Area(double len,double wid)
{
return (len * wid);
}
double Area(double a,double b,double c)
{
double s = (a+b+c)/2.0;
return sqrt(s*(s-a)*(s-b)*(s-c));
}