设计一个输入圆的半径和高,输出圆周长,圆面积,圆球面积,圆球体积,圆柱体积保留两位小数的代码
时间: 2024-10-25 08:07:34 浏览: 39
当你要求计算圆、圆球和圆柱的相关属性时,你需要分别定义这些几何体的公式。这里我会提供一个简单的C++代码示例,用于计算圆的周长(C),面积(A)以及圆柱(圆底为圆形)的表面积(SA,包括两个底面和侧面)和体积(V)。对于圆球(Sphere),我们将只计算其表面积(SB)和体积(VB)。所有的值都会被四舍五入到两位小数。
```cpp
#include <iostream>
#include <cmath>
// 定义圆相关的常量和函数
class Circle {
public:
double radius;
Circle(double r) : radius(r) {}
// 圆的周长和面积
double circumference() const { return 2 * M_PI * radius; }
double area() const { return M_PI * pow(radius, 2); }
};
// 定义圆柱相关的函数
class Cylinder {
private:
Circle base;
double height;
public:
Cylinder(double r, double h) : base(r), height(h) {}
// 圆柱的表面积和体积(保留两位小数)
double surface_area() const {
return 2 * base.area() + base.circumference() * height;
}
double volume() const { return base.area() * height; }
// 显示结果到两位小数
void display() const {
std::cout << "Surface Area: " << std::fixed << std::setprecision(2) << surface_area() << std::endl;
std::cout << "Volume: " << std::fixed << std::setprecision(2) << volume() << std::endl;
}
};
// 定义圆球相关的函数
class Sphere {
private:
double radius;
public:
Sphere(double r) : radius(r) {}
// 圆球的表面积和体积(保留两位小数)
double surface_area() const { return 4 * M_PI * pow(radius, 2); }
double volume() const { return (4 / 3) * M_PI * pow(radius, 3); }
// 显示结果到两位小数
void display() const {
std::cout << "Surface Area: " << std::fixed << std::setprecision(2) << surface_area() << std::endl;
std::cout << "Volume: " << std::fixed << std::setprecision(2) << volume() << std::endl;
}
};
int main() {
double circle_radius;
double cylinder_radius, cylinder_height;
std::cout << "Enter the radius of a circle: ";
std::cin >> circle_radius;
Circle circle(circle_radius);
std::cout << "Enter the radius and height of a cylinder: ";
std::cin >> cylinder_radius >> cylinder_height;
Cylinder cylinder(cylinder_radius, cylinder_height);
cylinder.display();
double sphere_radius;
std::cout << "Enter the radius of a sphere: ";
std::cin >> sphere_radius;
Sphere sphere(sphere_radius);
sphere.display();
return 0;
}
```
阅读全文
相关推荐
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)