Go語言程式:計算圓錐的體積和表面積
在本教程中,我們將討論使用Go語言程式設計計算圓錐的體積和表面積的方法。
但在編寫程式碼之前,讓我們簡要討論一下圓錐及其體積和表面積。
圓錐
圓錐是一種三維圖形,頂部尖銳,底部是平的曲面。圓錐的高度用“h”表示,平曲面的半徑用“r”表示。冰淇淋蛋筒就是一個很好的圓錐例子。

$$\mathrm{其中:斜高 \, =\, \sqrt{(r^{2} + h^{2}))}}$$
圓錐的體積
圓錐佔據的空間大小稱為體積。計算圓錐的體積在我們需要知道可以裝多少冰淇淋時很有用。
$$\mathrm{體積 = (1/3) \ast \pi \ast (r)^{2} \ast h}$$
圓錐的表面積
圓錐佔據的總面積稱為圓錐的表面積。
$$\mathrm{表面積 \, =\, \pi \ast r \ast (r+l) }$$
$$\mathrm{其中:斜高 \, l \, =\, \sqrt{(r^{2} + h^{2}))}}$$
示例
高 = 7,半徑 = 5
圓錐體積 = 183.33333333333331
圓錐表面積 = 213.75082562495555
解釋
圓錐體積 = (⅓) * π * (r)2* h
= (⅓) * (22/7) * (5*5) * 7
= 183.33333333333331
對於圓錐的表面積,首先計算“l”的值
斜高 ‘l’ = √(r2 + h2))
= √(52 + 72))
= 8.602
現在將‘l’的值代入圓錐表面積公式:
圓錐表面積 =π* r * (r + l)
= (22/7) * 5 * (5 + 8.602)
= 213.75082562495555
計算圓錐的體積和表面積
演算法
步驟 1 − 宣告一個變數用於儲存圓錐的高度 - ‘h’。
步驟 2 − 宣告一個變數用於儲存圓錐的半徑 - ‘r’。
步驟 3 − 宣告兩個變數用於儲存圓錐的表面積 - ‘area’ 和圓錐的體積 - ‘volume’,並將兩個變數初始化為 0。
步驟 4 − 使用公式計算體積 - 體積 = π * (r)2 * h,並將其儲存在函式 `calculateVolumeOfCone()` 中的 ‘volume’ 變數中。
步驟 5 − 使用 √(r2 + h2)) 計算 ‘l’ 的值,以便將其用於圓錐表面積的公式。
步驟 6 − 使用公式計算表面積 - 表面積 = π * 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()` 並將計算出的值儲存在 ‘area’ 變數中。
結論
這就是使用 Go 語言程式設計計算圓錐體積和表面積的全部內容。我們還透過使用單獨的函式來計算面積和體積來保持程式碼模組化,這也提高了程式碼的可重用性。您可以使用這些教程進一步瞭解 Go 語言程式設計。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP