Go語言程式:求矩形的面積


本教程將討論如何使用兩種方法在Go語言程式設計中求矩形的面積:

  • 使用長和寬計算矩形的面積

  • 使用對角線和寬計算矩形的面積

矩形

矩形是一個二維圖形,具有四條邊。矩形的對邊相等,所有角都為90°。矩形的另一個特性是其對邊彼此平行。

矩形的面積

矩形邊界內封閉的總空間稱為矩形的面積。


其面積可以透過將矩形的長和寬相乘來計算。

使用長和寬計算矩形的面積

給定矩形的長'l'和寬'b',我們需要求出矩形的面積。

公式

$$\mathrm{面積 \, =\, 長度 \times 寬度}$$

示例

  • 長度= 5

    寬度= 2

    面積= 5 * 2 = 10

    由於面積是透過將長度和寬度相乘來計算的,因此我們將5和2相乘,得到面積為10。

  • 長度= 25.5

    寬度= 5.0

    面積= 127.5

    面積是透過將長度和寬度相乘來計算的,因此:

    面積= 25.5 * 5.0

    = 127.5

演算法

步驟 1 - 宣告兩個變數:一個用於儲存矩形的長度'l',另一個用於儲存矩形的寬度'b'。

步驟 2 - 宣告一個變數用於儲存矩形的面積-'area'。

步驟 3 - 透過將矩形的長度'l'和寬度'b'相乘來計算面積,並將結果儲存在'area'變數中。

步驟 4 - 列印計算出的面積,即儲存在變數'area'中的值。

使用整數變數

示例

package main // fmt package allows us to print formatted strings import "fmt" // function to calculate area of the rectangle using int variable func calculateAreaOfRectangle(l, b int) { // integer type variable ‘area’ to store area of the rectangle var area int // calculating area by multiplying length ‘l’ and breadth ‘b’ // of the rectangle area = l * b fmt.Println("Length ‘l’ =",l) fmt.Println("Breadth ‘b’ =",b) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing length ‘l’ and breadth ‘b’ var l, b = 3, 4 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for length and breadth as parameters calculateAreaOfRectangle(l, b) }

輸出

Length ‘l’ = 3
Breadth ‘b’ = 4
Therefore, Area of Rectangle :  12

解釋

在上面的程式碼中,面積是使用此公式透過將矩形的長度和寬度相乘來計算的:

面積 = l * b

因此,面積= 3 * 4

面積= 12

使用浮點變數

示例

package main // fmt package allows us to print formatted strings import "fmt" // function to calculate the area of the rectangle using float variable func calculateAreaOfRectangle(l, b float32) { // float type variable ‘area’ to store area of the rectangle // float32 variable stores smaller value // float64 variable stores larger value var area float32 // calculating area by multiplying length ‘l’ and breadth ‘b’ // of the rectangle area = l * b fmt.Println("Length ‘l’ =",l) fmt.Println("Breadth ‘b’ =",b) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing length ‘l’ and breadth ‘b’ var l, b float32 = 5.5, 8.5 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for length and breadth as parameters calculateAreaOfRectangle(l, b) }

輸出

Length ‘l’ = 5.5
Breadth ‘b’ = 8.5
Therefore, Area of Rectangle :  46.75

解釋

面積= 5.5 * 8.5

= 46.75

使用對角線和寬計算矩形的面積

矩形有兩個對角線,且長度相等。可以使用對角線和寬計算矩形的面積。


公式

$$\mathrm{面積 = 寬度 \times (\sqrt{((對角線)^{2} - (寬度)^{2}))}}$$

示例

  • 對角線= 30.0

    寬度= 10.0

    面積= 282.842712474619 為了計算面積,我們在這個公式中使用對角線和寬的值:

    面積 = 寬度 * (√((對角線)2 - (寬度)2))

    因此,面積 = 10 * (√((30)2 - (10)2))

    面積 = 282.842712474619

示例

package main // fmt package allows us to print formatted strings // math package allows us to perform various mathematical // operations import ( "fmt" "math" ) // function to calculate area of rectangle using float variables func calculateAreaOfRectangle(d, b float64) { // float64 type variable ‘area’ to store area of rectangle var area float64 // calculating area of the rectangle // math.Sqrt function calculates square root // math.Pow function calculates the power fmt.Println("Diagonal ‘d’ =", d) fmt.Println("Breadth ‘b’ =", b) // calculating area area = b * math.Sqrt((math.Pow(d, 2) - math.Pow(b, 2))) // printing area of the rectangle fmt.Println("Therefore, Area of Rectangle : ", area) } // driver function func main() { // declaring variables for storing diagonal ‘d’ and breadth ‘b’ var d, b float64 = 40.0, 20.0 // calling the calculateAreaOfRectangle function for // calculating the area of the rectangle and passing values // for diagonal and breadth as parameters calculateAreaOfRectangle(d, b) }

輸出

Diagonal ‘d’ = 40
Breadth ‘b’ = 20
Therefore, Area of Rectangle :  692.820323027551

解釋

為了計算面積,我們在這個公式中使用對角線和寬的值:

面積 = 寬度 * (√((對角線)2 - (寬度)2))

因此,面積 = 20 * (√((40)2 - (20)2))

面積 = 692.820323027551

結論

這就是使用兩種方法計算矩形面積的全部內容:長和寬,以及對角線和寬。我們還在本文中討論了使用兩種不同資料型別作為輸入,即整數和浮點數。

您可以使用這些教程進一步瞭解Go語言程式設計。

更新於:2022年11月21日

瀏覽量:588

開啟你的職業生涯

完成課程獲得認證

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