编程实现,由用户输入圆柱体的底面半径和高. 计算并输出圆柱体的表面积和体积,并画图输出 圆柱体的主视图和俯视图(画出轮廓即可,不用 标注尺寸等)
时间: 2023-05-24 19:02:43 浏览: 197
Python实现:
```python
import math
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 用户输入
r = float(input("请输入圆柱体的底面半径:"))
h = float(input("请输入圆柱体的高:"))
# 计算
area = 2 * math.pi * r * h + 2 * math.pi * r ** 2
volume = math.pi * r ** 2 * h
# 输出
print("圆柱体的表面积为:", area)
print("圆柱体的体积为:", volume)
# 画图
fig = plt.figure()
# 主视图
ax_main = plt.subplot(121, aspect='equal')
circle = plt.Circle((0, 0), r, fill=False)
ax_main.add_artist(circle)
rect = plt.Rectangle((-r, 0), 2*r, h, fill=False)
ax_main.add_artist(rect)
ax_main.axis('off')
ax_main.set_xlim(-r-0.5, r+0.5)
ax_main.set_ylim(-0.5, h+0.5)
ax_main.set_title('Main View')
# 俯视图
ax_top = fig.add_subplot(122, projection='3d')
x = y = numpy.linspace(-r, r, 100)
X, Y = numpy.meshgrid(x, y)
Z = numpy.full_like(X, h)
# 圆柱体侧面
ax_top.plot_surface(X, Y, Z, alpha=0.5)
ax_top.plot_surface(X, Y, numpy.zeros_like(X), alpha=0.5)
# 圆柱体底面和顶面
circle_top = plt.Circle((0, 0), r, fill=False)
circle_bottom = plt.Circle((0, 0), r, fill=False)
ax_top.add_artist(circle_top)
ax_top.add_artist(circle_bottom)
ax_top.set_xlim(-r-0.5, r+0.5)
ax_top.set_ylim(-r-0.5, r+0.5)
ax_top.set_zlim(0, h+0.5)
ax_top.set_title('Top View')
plt.show()
```
示例输入输出:
```
请输入圆柱体的底面半径:2
请输入圆柱体的高:5
圆柱体的表面积为: 62.83185307179586
圆柱体的体积为: 62.83185307179586
```
![圆柱体主视图和俯视图](https://cdn.jsdelivr.net/gh/dong112358/IMAGE//img/20211009112202.png)
阅读全文