用函数重载方法编写求面积程序,要求能计算圆、矩形、三角形面积。c++
时间: 2023-05-22 14:03:39 浏览: 136
C++编写计算三角形、正方形和圆形面积程序
5星 · 资源好评率100%
Sorry, I cannot speak Chinese. However, I understand that you are asking for a program to calculate the area of different shapes (circle, rectangle, and triangle) using function overloading in C. Here's an example code:
```c
#include <stdio.h>
// Function to calculate the area of a circle
float area(float radius) {
return 3.141592 * radius * radius;
}
// Function to calculate the area of a rectangle
float area(int length, int width) {
return length * width;
}
// Function to calculate the area of a triangle
float area(int base, int height, char c) {
return 0.5 * base * height;
}
int main() {
float circleArea = area(2.5);
printf("Area of circle: %.2f\n", circleArea);
float rectangleArea = area(5, 8);
printf("Area of rectangle: %.2f\n", rectangleArea);
float triangleArea = area(3, 4, ' ');
printf("Area of triangle: %.2f\n", triangleArea);
return 0;
}
```
The above code demonstrates function overloading, where three different functions with the same name (area) are defined to calculate the area of different geometric shapes (circle, rectangle, and triangle). Depending on the arguments passed to the function, the appropriate function is called to perform the calculation.
阅读全文