Go語言程式:計算圓柱體的體積和表面積
在本教程中,我們將討論使用Go語言程式設計,根據圓柱體的高度和半徑計算其體積和表面積的方法。
但在編寫程式碼之前,讓我們簡要討論一下圓柱體及其體積和表面積。
圓柱體
圓柱體是一個三維圖形,其底面為圓形。兩個底面之間的距離稱為圓柱體的高度“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 − 使用公式計算體積 - 體積 = π * (r)2 * h,並將結果儲存在函式calculateVolumeOfCylinder()中的‘volume’變數中。
步驟 5 − 使用公式計算表面積 - 表面積 = 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語言程式設計計算圓柱體體積和表面積的全部內容。我們還透過使用單獨的函式來計算面積和體積來保持程式碼模組化,這也提高了程式碼的可重用性。您可以使用這些教程進一步瞭解Go語言程式設計。