如何在 Golang 中求長方體的表面積和體積?


在本教程中,我們將學習 Golang 程式,用於計算長方體的表面積和體積。面積是指任何封閉圖形所覆蓋的總空間。體積是指容器內部容納東西的能力。


公式

l - 長方體的長度

h - 長方體的高度

w - 長方體的寬度。

長方體的表面積 - 2*l*w + 2*w*h + 2*l*h

例如,長方體的長度為 10 釐米,高度為 5 釐米,寬度為 8 釐米,則長方體的表面積為 -

l = 10 cm

h = 5 cm

w = 4 cm

面積 = 2*l*w + 2*w*h + 2*l*h

= 2*10*4 + 2*4*5 + 2*5*10

= 80 + 40 + 100

= 220 cm^2

長方體的體積 - l * b * h

例如,長方體的長度為 10 釐米,高度為 5 釐米,寬度為 8 釐米,則長方體的體積為 -

l = 10 cm

h = 5 cm

w = 4 cm

體積 = l * b * h

= 10 * 5 * 4

= 200

演算法

步驟 1 - 宣告用於長度、寬度、高度、體積和麵積的 float64 資料型別變數。

步驟 2 - 初始化變數。

步驟 3 - 使用上述公式在函式中計算表面積和體積。

步驟 4 - 列印結果。

示例

在此示例中,我們將透過函式計算長方體的表面積和體積。

package main

// fmt package provides the function to print anything
import (
   "fmt"
)
func main() {
   
   // declaring the floating variables using the var keyword for
   
   // storing the length of the parallel sides of the Cuboid,
   
   // distance between the parallel sides and also a variable area
   
   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")
   
   // initializing the length of a Cuboid
   l = 10
   
   // initializing the width of a Cuboid
   w = 8
   
   // initializing the height of a Cuboid
   h = 4
   
   // finding the surface Area of a Cuboid
   surfaceArea = 2*l*w + 2*w*h + 2*l*h
   
   // finding the volume of a Cuboid
   volume = l * w * h
   
   // printing the result
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid within the function)")
}

輸出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid within the function)

演算法

步驟 1 - 宣告用於長度、寬度、高度、體積和麵積的 float64 資料型別變數。

步驟 2 - 初始化變數。

步驟 3 - 使用長方體的長度、寬度和高度作為引數呼叫函式,並將函式返回的面積儲存起來。

步驟 4 - 使用長方體的長度、寬度和高度作為引數呼叫函式,並將函式返回的體積儲存起來。

步驟 5 - 列印結果。

示例

在此示例中,我們將透過定義單獨的函式來計算長方體的面積。

package main

// fmt package provides the function to print anything
import ( 
   "fmt"
)

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func areaOfCuboid(l, w, h float64) float64 {
   
   // returning the area by applying the formula
   return 2*l*w + 2*w*h + 2*l*h
}

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func volumeOfCuboid(l, w, h float64) float64 {
   
   // returning the volume by applying the formula
   return l * w * h
}
func main() {
   
   // declaring the floating variables using the var keyword for
   
   // storing the length of the parallel sides of the Cuboid,
   
   // distance between the parallel sides and also a variable area
   
   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")
   
   // initializing the length of a Cuboid
   l = 10
   
   // initializing the width of a Cuboid
   w = 8
   
   // initializing the height of a Cuboid
   h = 4
   
   // finding the surface Area of a Cuboid by calling the function
   surfaceArea = areaOfCuboid(l, w, h)
   
   // finding the volume of a Cuboid
   volume = volumeOfCuboid(l, w, h)
   
   // printing the result 
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid in the seperate function)")
} 

輸出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid in the seperate function)

結論

以上是兩種在 Golang 中計算長方體面積和體積的方法。從模組化和程式碼重用性的角度來看,第二種方法更好,因為我們可以在專案的任何地方呼叫該函式。要了解更多關於 Go 的知識,您可以瀏覽這些 教程。

更新於: 2022-11-29

133 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告