Golang 计算圆柱体体积和表面积
在本教程中,我们将讨论使用高度和半径在Golang编程中计算圆柱体的体积和表面积的方法。
但在编写此代码之前,让我们简要讨论圆柱体及其体积和表面积。
圆柱体
圆柱体是一个具有圆形底面的三维图形。两个底面之间的距离称为圆柱体的高度’h’,底面的半径用’r’表示。冷饮罐就是圆柱体的一个很好的示例。
圆柱体的体积
圆柱体的容积通常称为其体积。在我们想要知道一个瓶子或容器的容量时,计算圆柱体的体积可以很有帮助。
\mathrm{体积 \, =\, \pi * \left ( r \right )^{2} * h}
圆柱体的表面积
圆柱体所包围的总面积称为圆柱体的表面积。
\mathrm{表面积 \, =\, 2\ast \pi * r * \left ( h+r \right )}
示例
- 高度 = 7,半径 = 5
圆柱体的体积 = 550
圆柱体的表面积 = 377.1428571428571
解释
圆柱体的体积 = π * (r) 2 * h
= (22/7) * 5 * 5 * 7
= 550
圆柱体的表面积 = 2 * π * r * (h + r)
= 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571
- 高度 = 21,半径 = 10
圆柱体的体积 = 6600
圆柱体的表面积 = 1948.5714285714284
解释
圆柱体的体积 = π * (r) 2 * h
= (22/7) * 10 * 10 * 21
= 6600
圆柱体的表面积 = 2 * π * r * (h + r)
= 2 * (22/7) * 10 * (21 + 10)
= 1948.5714285714284
计算圆柱体的体积和表面积
步骤
步骤 1 - 声明一个变量用于存储圆柱的高度- ‘h’。
步骤 2 - 声明一个变量用于存储圆柱的半径- ‘r’。
步骤 3 - 声明一个变量用于存储圆柱的面积- ‘area’,并声明另一个变量用于存储体积- ‘volume’,并将这两个变量都初始化为0。
步骤 4 - 使用公式 Volume = π * (r) 2 * h 计算体积,并将其存储在函数 calculateVolumeOfCylinder() 中的 ‘volume’ 变量中。
步骤 5 - 使用公式 Area = 2 * π * r * (h + r) 计算面积,并将其存储在函数 calculateAreaOfCylinder() 中的 ‘area’ 变量中。
步骤 6 - 打印计算得到的圆柱的体积和面积,即 ‘volume’ 和 ‘area’ 变量中存储的值。
示例
package main
// fmt package allows us to print formatted strings
import "fmt"
func calculateVolumeOfCylinder(h, r float64) float64 {
// declaring variable ‘volume’ for storing the volume of the cylinder
var volume float64 = 0
// calculating the volume of the cylinder using formula
volume = (22.0 / 7.0) * r * r * h
return volume
}
func calculateAreaOfCylinder(h, r float64) float64 {
// declaring variable ‘area’ for storing the area of the cylinder
var area float64 = 0
// calculating the area of the cylinder using formula
area = 2 * (22.0 / 7.0) * r * (h + r)
return area
}
func main() {
// declaring variable ‘height’ for storing the height of the cylinder
var h float64 = 7
// declaring variable ‘radius’ for storing the radius of the cylinder
var r float64 = 5
// declaring variable ‘area’ and ‘volume’ for storing the area and volume of the cylinder
var area, volume float64
fmt.Println("Program to calculate the volume and area of the Cylinder \n")
// calling function calculateVolumeOfCylinder() for
// calculating the volume of the cylinder
volume = calculateVolumeOfCylinder(h, r)
// calling function calculateAreaOfCylinder() for calculating
// the area of the cylinder
area = calculateAreaOfCylinder(h, r)
// printing the results
fmt.Println("Height of the cylinder : ", h)
fmt.Println("Radius of the cylinder : ", r)
fmt.Println("Therefore, Volume of cylinder : ", volume)
fmt.Println("Area of cylinder : ", area)
}
输出
Program to calculate the volume and area of the Cylinder
Height of the cylinder : 7
Radius of the cylinder : 5
Therefore, Volume of cylinder : 550
Area of cylinder : 377.1428571428571
代码描述
- var h float64 = 7, var r float64 = 5 − 在这一行中,我们声明了变量高度 ‘h’ 和半径 ‘r’。由于高度和半径的数据类型是浮点数,所以我们使用了 float64 数据类型。
-
calculateVolumeOfCylinder(h, r float64) float64 − 这是一个计算圆柱体体积的函数。该函数的参数是数据类型为 float64 的变量 ‘h’ 和 ‘r’,返回类型也是 float64。
-
volume = (22.0 / 7.0) * r * r * h − 如果我们期望输出的数据类型为浮点数,我们需要显式地转换数值,因此我们使用了 22.0 和 7.0 而不是 22 和 7。使用上述公式,我们计算了圆柱体的体积。
-
return volume − 返回圆柱体的体积。
-
volume = calculateVolumeOfCylinder(h, r) − 我们调用 calculateVolumeOfCylinder() 函数,并将计算得到的值存储在 ‘volume’ 变量中。
-
calculateAreaOfCylinder(h, r float64) float64 − 这是一个计算圆柱体表面积的函数。该函数的参数是数据类型为 float64 的变量 ‘h’ 和 ‘r’,返回类型也是 float64。
-
area = 2 * (22.0 / 7.0) * r * (h + r) − 由于我们期望输出的数据类型为浮点数,所以我们在这里也需要显式地转换数值,因此我们使用了 22.0 和 7.0 而不是 22 和 7。使用上述公式,我们计算了圆柱体的表面积。
-
return area − 返回圆柱体的表面积。
-
area = calculateAreaOfCylinder(h, r) − 我们调用 calculateAreaOfCylinder() 函数,并将计算得到的值存储在 ‘area’ 变量中。
因此,圆柱体的体积 = (22/7) * 5 * 5 * 7
= 550
圆柱体的表面积 = 2 * (22/7) * 5 * (7 + 5)
= 377.1428571428571
结论
这是使用 Go 编程计算圆柱体体积和表面积的全部内容。我们还通过使用单独的函数来计算面积和体积来保持了代码的模块化,这也增加了代码的可重用性。您可以使用 这些 教程进一步了解 Golang 编程。