Golang 计算锥体体积和面积
在本教程中,我们将讨论使用Golang编程计算锥体的体积和面积的方法。
但在编写此代码之前,让我们简要讨论锥体及其体积和面积。
锥体
锥体是一个三维图形,顶部是尖的,底部是扁平而弯曲的。锥体的高度用’h’表示,底面的曲面的半径用’r’表示。冰淇淋蛋筒是一个很好的锥体示例。
其中,长度=√(r^2 + h^2)
锥体的体积
锥体所占空间的容量被称为其体积。计算锥体的体积在我们想知道在锥体中可以填充多少冰淇淋的情况下很有用。
体积=(1/3)π(r)^2*h
锥体的面积
锥体所占据的总面积称为锥体的表面积。
面积 = πr(r+l)
其中,l=√(r^2 + h^2)
示例
- 高度=7,半径=5
锥体的体积=183.33333333333331
锥体的面积=213.75082562495555
解释
- 圆锥体积 = (⅓) * π * (r) 2 * h
= (⅓) * (22/7) * (5*5) * 7
= 183.33333333333331
- 对于圆锥的表面积,首先计算‘l’的值
长度 ‘l’ = √(r 2 + h 2 ))
= √(5 2 + 7 2 ))
= 8.602
现在把‘l’的值代入圆锥表面积的公式-
圆锥的表面积 =π* r * (r + l)
= (22/7) * 5 * (5 + 8.602)
= 213.75082562495555
计算圆锥的体积和表面积
步骤
步骤1 − 声明变量用于存储圆锥的高度- ‘h’。
步骤2 − 声明变量用于存储圆锥的半径- ‘r’。
步骤3 − 声明两个变量用于存储圆锥的表面积- ‘area’ 和圆锥的体积- ‘volume’,并将这两个变量都初始化为0。
步骤4 − 使用公式计算体积- Volume = π * (r) 2 * h,并将结果存储在函数 calculateVolumeOfCone() 中的 ‘volume’ 变量中。
步骤5 − 计算‘l’的值,使用公式 √(r 2 + h 2 )),以便在计算圆锥的表面积时使用。
步骤6 − 使用公式计算表面积- Area = π * r * (r + l),并将结果存储在函数 calculateAreaOfCone() 中的‘area’变量中。
步骤7 − 打印计算出的圆锥的体积和表面积,即‘volume’变量和‘area’变量中的值。
示例
package main
// fmt package allows us to print formatted strings
import (
"fmt"
"math"
)
func calculateVolumeOfCone(h, r float64) float64 {
// declaring variable ‘volume’ for storing the volume of the cone
var volume float64 = 0
// calculating the volume of the cone using its formula
volume = (1.0 / 3.0) * (22.0 / 7.0) * r * r * h
return volume
}
func calculateAreaOfCone(h, r float64) float64 {
// declaring variable ‘area’ for storing the area of the cone
var area float64 = 0
// calculating the value of length 'l'
l := math.Sqrt((r * r) + (h * h))
// calculating the area of the cone using its formula
area = (22.0 / 7.0) * r * (r + l)
return area
}
func main() {
// declaring variable ‘height’ for storing the height of the cone
var h float64 = 7
// declaring variable ‘radius’ for storing the radius of the cone
var r float64 = 5
// declaring variables ‘area’ and ‘volume’ for storing the area and volume of the cone
var area, volume float64
fmt.Println("Program to calculate the volume and area of the Cone \n")
// calling function calculateVolumeOfCone() for
// calculating the volume of the cone
volume = calculateVolumeOfCone(h, r)
// calling function calculateAreaOfCone() for calculating
// the area of the cone
area = calculateAreaOfCone(h, r)
// printing the results
fmt.Println("Height of the cone : ", h)
fmt.Println("Radius of the cone : ", r)
fmt.Println("Therefore, Volume of cone : ", volume)
fmt.Println("Area of cone : ", area)
}
输出
Program to calculate the volume and area of the Cone
Height of the cone : 7
Radius of the cone : 5
Therefore, Volume of cone : 183.33333333333331
Area of cone : 213.75082562495555
代码描述
- var h float64 = 7,var r float64 = 5 - 在这一行中,我们声明了高度’h’和半径’r’的变量。
-
calculateVolumeOfCone(h, r float64) float64 - 这是我们计算圆锥体积的函数。该函数以参数类型为float64的’h’和’r’变量为参数,并且具有 float64 的返回类型。
-
volume = (1.0 / 3.0) * (22.0 / 7.0) * r * r * h - 如果我们希望输出值的数据类型为float,我们需要显式进行值转换,这就是为什么我们使用值22.0和7.0而不是22和7的原因。使用上述公式,我们可以计算圆锥的体积。
-
return volume - 返回圆锥体的体积。
-
volume = calculateVolumeOfCone(h, r) - 我们调用函数calculateVolumeOfCone()并将计算出的值存储在主方法中的’volume’变量中。
-
calculateAreaOfCone(h, r float64) float64 - 这是我们计算圆锥面积的函数。该函数以参数类型为float64的’h’和’r’变量为参数,并且具有float64的返回类型。
-
l := math.Sqrt((r * r) + (h * h)) - 我们需要长度’l’的值来计算圆锥的面积。
-
area = (22.0 / 7.0) * r * (r + l) - 使用这个公式,我们计算圆锥面的面积。
-
return area - 返回圆锥的面积。
-
area = calculateAreaOfCone(h, r) - 我们调用函数 calculateAreaOfCone() 并将计算出的值 stored in the ‘area’ variable。
结论
这就是使用Go编程计算圆锥体积和面积的介绍。我们还通过使用单独的函数计算面积和体积来保持了代码的模块性,这也增加了代码的可重用性。您可以使用这些教程来探索更多关于Golang编程的内容。