如何在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.