如何在Go語言中求梯形的面積?


在本教程中,我們將學習Go語言程式如何計算梯形的面積。面積是任何封閉圖形所覆蓋的總空間。


公式

Area of Trapezium - ½ * (b1 + b2) * h
b1 - length of one parallel side of a Trapezium
b2 - length of another parallel side of a Trapezium
h - the distance between the parallel sides of a Trapezium.

例如,梯形的一條平行邊的長度為10釐米,另一條平行邊的長度為8釐米,它們之間的距離為4釐米,則梯形的面積為:

b1 = 10 cm
b2 = 8 cm
h = 4 cm
Area = ½ * (a + b) * h
   = ½ *( 10 + 8) * 4
   = ½ * 18 * 4
   = 9 * 4
   = 32 cm^2

在函式中計算梯形的面積

演算法

步驟1 - 宣告用於儲存平行邊長度、平行邊之間距離和麵積的float64型別變數。

步驟2 - 初始化變數。

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

步驟4 - 列印結果。

Time Complexity:
O(1)

Space Complexity:
O(1)

示例1

在這個例子中,我們將學習如何在函式中計算梯形的面積。

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 Trapezium, // distance between the parallel sides and also a variable area // to store Area var a, b, d, Area float64 fmt.Println("Program to find the Area of a Trapezium.") // initializing the length of one of the parallel side of a Trapezium a = 10 // initializing the length of another parallel side of a Trapezium b = 8 // initializing the length of distance between parallel sides of a Trapezium d = 4 // finding the Area of a Trapezium Area = (1.0 / 2) * ((a + b) * d) // printing the result fmt.Println("The Area of a Trapezium whose length of the parallel sides and distance between the sides are", a,",", b,",", d, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Trapezium within the function)") }

輸出

% go run tutorialpoint.go
Program to find the Area of a Trapezium.
The Area of a Trapezium whose length of the parallel sides and distance between the sides are 10 8 4 is 36 cm * cm.
(Finding the Area of a Trapezium within the function)

在單獨的函式中計算梯形的面積

演算法

步驟1 - 宣告用於儲存平行邊長度、平行邊之間距離和麵積的float64型別變數。

步驟2 - 初始化變數。

步驟3 - 呼叫函式,將梯形的平行邊長度和距離作為引數傳入,並將函式返回的面積儲存起來。

步驟4 - 列印結果。

示例2

在這個例子中,我們將透過定義一個單獨的函式來計算梯形的面積。

package main // fmt package provides the function to print anything import ( "fmt" ) // in this line we have declared the function that have float64 // type parameter and float64 type returntype func areaOfTrapezium(a, b, d float64) float64 { // returning the area by applying the formula return (1.0 / 2) * ((a + b) * d) } func main() { // declaring the floating variables using the var keyword for // storing the length of the parallel sides of the Trapezium, // distance between the parallel sides and also a variable area // to store Area var a, b, d, Area float64 fmt.Println("Program to find the Area of a Trapezium.") // initializing the length of one of the parallel side of a Trapezium a = 10 // initializing the length of another parallel side of a Trapezium b = 8 // initializing the length of distance between parallel sides of a Trapezium d = 4 // finding the Area of a Trapezium by calling the function with the respective parameter Area = areaOfTrapezium(a, b, d) // printing the result fmt.Println("The Area of a Trapezium whose length of the parallel sides and distance between the sides are", a,",", b,",", d, "is", Area, "cm * cm.") fmt.Println("(Finding the Area of a Trapezium in the separate function)") }

輸出

Program to find the Area of a Trapezium.
The Area of a Trapezium whose length of the parallel sides and distance between the sides are 10 , 8 , 4 is 36 cm * cm.
(Finding the Area of a Trapezium in the separate function)

結論

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

更新於:2022年9月2日

149 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.