Golang 如何查找梯形的面积

Golang 如何查找梯形的面积

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

如何用Golang查找梯形的面积?

公式

梯形的面积 – ½ * (b1 + b2) * h
b1 – 梯形的一条平行边的长度
b2 – 梯形的另一平行边的长度
h – 梯形的平行边之间的距离。

例如,梯形的一条平行边的长度是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步 – 以梯形的平行边长度和the作为参数调用函数,并存储函数返回的面积。

第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中寻找梯形面积的两种方法。第二种方法在模块化和代码重用性方面要好得多,因为我们可以在项目的任何地方调用这个函数。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程