请编写函数,求三角形的面积。 函数原型 double TriangleArea(double base, double height); 说明:参数 base 和 height 分别为三角形的底和高。函数值为三角形的面积。 裁判程序 #include <stdio.h> double TriangleArea(double base, double height); int main() { double b, h, a; scanf("%lf%lf", &b, &h); a = TriangleArea(b, h); printf("%g\n", a); return 0; } /* 你提交的代码将被嵌在这里 */
时间: 2024-03-23 22:39:23 浏览: 30
好的,根据您的要求,我来编写函数 TriangleArea(double base, double height),实现求三角形的面积。
函数原型如下:
```c
double TriangleArea(double base, double height);
```
函数说明:
参数 base 和 height 分别为三角形的底和高。函数返回值为三角形的面积。
函数实现如下:
```c
double TriangleArea(double base, double height) {
return 0.5 * base * height;
}
```
完整的程序如下:
```c
#include <stdio.h>
double TriangleArea(double base, double height);
int main() {
double b, h, a;
scanf("%lf%lf", &b, &h);
a = TriangleArea(b, h);
printf("%g\n", a);
return 0;
}
double TriangleArea(double base, double height) {
return 0.5 * base * height;
}
```
希望我的回答能够帮到您,有什么问题可以随时问我哦!
相关问题
编写 C++程序, 定义重载函数 double area(形参表),分别用来求长方形、三角形、正方形、圆的面积,主函数中给定必要初始据分别调用这些函数求面积
以下是C++程序代码:
```
#include <iostream>
using namespace std;
double area(double length, double width); // 长方形面积函数声明
double area(double base, double height); // 三角形面积函数声明
double area(double side); // 正方形面积函数声明
double area(double radius); // 圆面积函数声明
int main()
{
double length, width, base, height, side, radius;
cout << "请输入长方形的长和宽:" << endl;
cin >> length >> width;
cout << "长方形的面积为:" << area(length, width) << endl;
cout << "请输入三角形的底和高:" << endl;
cin >> base >> height;
cout << "三角形的面积为:" << area(base, height) << endl;
cout << "请输入正方形的边长:" << endl;
cin >> side;
cout << "正方形的面积为:" << area(side) << endl;
cout << "请输入圆的半径:" << endl;
cin >> radius;
cout << "圆的面积为:" << area(radius) << endl;
return 0;
}
double area(double length, double width)
{
return length * width;
}
double area(double base, double height)
{
return 0.5 * base * height;
}
double area(double side)
{
return side * side;
}
double area(double radius)
{
return 3.1415926535 * radius * radius;
}
```
程序中定义了四个重载函数 `area`,分别计算长方形、三角形、正方形、圆的面积。在主函数中分别调用这些函数,输入必要的参数,计算并输出面积。
编写 C++程序,定义重载函数 double area(形参表),分别用来求长方形、三角形、正方形、圆的面积,主函数中给定必要的初始数据,分别调用这些函数求面积。
好的,这是一个比较基础的 C++ 编程问题。以下是代码实现:
```c++
#include <iostream>
using namespace std;
double area(double length, double width) {
return length * width;
}
double area(double base, double height, char type) {
if (type == 'T') {
return base * height / 2;
} else if (type == 'C') {
return 3.14 * base * base;
} else {
return -1;
}
}
double area(double side) {
return side * side;
}
int main() {
double length = 10;
double width = 5;
double base = 8;
double height = 6;
double side = 4;
double rectangleArea = area(length, width);
double triangleArea = area(base, height, 'T');
double circleArea = area(base, height, 'C');
double squareArea = area(side);
cout << "Rectangle area: " << rectangleArea << endl;
cout << "Triangle area: " << triangleArea << endl;
cout << "Circle area: " << circleArea << endl;
cout << "Square area: " << squareArea << endl;
return 0;
}
```
在这个程序中,我们定义了三个重载函数 `area`,分别用来计算长方形、三角形、正方形、圆的面积。第一个函数 `area(double length, double width)` 用来计算长方形的面积,第二个函数 `area(double base, double height, char type)` 用来计算三角形和圆的面积,其中参数 `type` 用来区分三角形和圆。第三个函数 `area(double side)` 用来计算正方形的面积。
在主函数中,我们给定了必要的初始数据,并调用了这些函数来计算面积。最后输出计算结果。注意,这里为了简化问题,我们没有对输入数据进行任何验证。在实际开发中,需要对输入数据进行一定的验证和处理,以保证程序的正确性和健壮性。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)