用c++编写一个函数分别求三角形、矩形,圆形的图形面积
时间: 2023-05-17 09:03:19 浏览: 156
好的,以下是用C++编写的函数分别求三角形、矩形和圆形的面积:
```c++
#include <iostream>
#include <cmath>
using namespace std;
const double PI = 3.14159265358979323846;
double triangleArea(double base, double height) {
return 0.5 * base * height;
}
double rectangleArea(double length, double width) {
return length * width;
}
double circleArea(double radius) {
return PI * pow(radius, 2);
}
int main() {
double base, height, length, width, radius;
cout << "Enter base and height of triangle: ";
cin >> base >> height;
cout << "Area of triangle: " << triangleArea(base, height) << endl;
cout << "Enter length and width of rectangle: ";
cin >> length >> width;
cout << "Area of rectangle: " << rectangleArea(length, width) << endl;
cout << "Enter radius of circle: ";
cin >> radius;
cout << "Area of circle: " << circleArea(radius) << endl;
return 0;
}
```
希望这个函数可以帮助你计算图形的面积。
阅读全文
相关推荐


















