编写一个程序计算出球、圆柱和圆锥的表面积和体积
时间: 2023-04-27 14:04:51 浏览: 194
好的,以下是一个计算球、圆柱和圆锥表面积和体积的程序示例:
```
import math
# 计算球的表面积和体积
def sphere(radius):
surface_area = 4 * math.pi * radius ** 2
volume = 4 / 3 * math.pi * radius ** 3
return surface_area, volume
# 计算圆柱的表面积和体积
def cylinder(radius, height):
surface_area = 2 * math.pi * radius * height + 2 * math.pi * radius ** 2
volume = math.pi * radius ** 2 * height
return surface_area, volume
# 计算圆锥的表面积和体积
def cone(radius, height):
surface_area = math.pi * radius * (radius + math.sqrt(radius ** 2 + height ** 2))
volume = 1 / 3 * math.pi * radius ** 2 * height
return surface_area, volume
# 测试
print("球的表面积和体积:", sphere(3))
print("圆柱的表面积和体积:", cylinder(2, 5))
print("圆锥的表面积和体积:", cone(4, 6))
```
程序中使用了数学库中的`math`模块,其中包含了一些常用的数学函数,如圆周率`pi`和平方根函数`sqrt`。通过定义三个函数`sphere`、`cylinder`和`cone`,分别计算球、圆柱和圆锥的表面积和体积。最后通过调用这些函数,输出计算结果。