Golang 如何找到梯形的面积

Golang 如何找到梯形的面积

在本教程中,我们将看到在Golang中找到梯形面积的程序。 面积是任何闭合图形覆盖的总空间。

Golang 如何找到梯形的面积

公式

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步 - 声明用于平行边的长度、平行边之间的距离和浮点数类型的面积变量。

第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)

结论

这是在Golang中找到梯形面积的两种方法。第二种方法在模块化和代码可重用性方面要好得多,因为我们可以在项目中的任何地方调用该函数。要了解更多关于 go 的知识,您可以探索这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程