设圆半径为r,圆柱的高为h,求圆周长,圆面积,圆球表面积,圆柱体积。用cin输入数据,输出计算结果(规定pi=3.1415,结果取小数点后两位数字)
时间: 2024-12-17 14:23:49 浏览: 8
好的,我可以帮你写出C++代码来计算这些几何形状的相关属性。首先,我们需要分别计算圆周长(Circumference)、圆面积(Area)、圆球表面积(Surface Area of a Sphere)以及圆柱体积(Cylinder Volume)。以下是一段示例代码:
```cpp
#include <iostream>
#include <iomanip>
const double PI = 3.1415;
// 计算圆的周长和面积
double circle(double radius) {
double circumference = 2 * PI * radius;
double area = PI * std::pow(radius, 2);
return {circumference, area};
}
// 计算球的表面积
double sphere(double radius) {
double surface_area = 4 * PI * std::pow(radius, 2);
return surface_area;
}
// 计算圆柱体的体积
double cylinder(double radius, double height) {
double volume = PI * std::pow(radius, 2) * height;
return volume;
}
int main() {
double radius, height; // 圆半径和圆柱高
std::cout << "Enter the radius of the circle and the height of the cylinder: ";
std::cin >> radius >> height;
auto circle_info = circle(radius);
std::cout << "Circle circumference: " << std::fixed << std::setprecision(2) << circle_info.first << "\n";
std::cout << "Circle area: " << std::fixed << std::setprecision(2) << circle_info.second << "\n";
double sphere_surface_area = sphere(radius);
std::cout << "Sphere surface area: " << std::fixed << std::setprecision(2) << sphere_surface_area << "\n";
double cylinder_volume = cylinder(radius, height);
std::cout << "Cylinder volume: " << std::fixed << std::setprecision(2) << cylinder_volume << "\n";
return 0;
}
```
在这个程序中,我们定义了四个函数来计算每个几何形体的属性。`main`函数中从用户那里获取输入并调用这些函数,然后按照要求输出结果,保留两位小数。
阅读全文