如何在 Golang 中求长方体的表面积和体积?


在本教程中,我们将学习 Golang 程序,用于计算长方体的表面积和体积。面积是指任何封闭图形所覆盖的总空间。体积是指容器内部容纳东西的能力。


公式

l - 长方体的长度

h - 长方体的高度

w - 长方体的宽度。

长方体的表面积 - 2*l*w + 2*w*h + 2*l*h

例如,长方体的长度为 10 厘米,高度为 5 厘米,宽度为 8 厘米,则长方体的表面积为 -

l = 10 cm

h = 5 cm

w = 4 cm

面积 = 2*l*w + 2*w*h + 2*l*h

= 2*10*4 + 2*4*5 + 2*5*10

= 80 + 40 + 100

= 220 cm^2

长方体的体积 - l * b * h

例如,长方体的长度为 10 厘米,高度为 5 厘米,宽度为 8 厘米,则长方体的体积为 -

l = 10 cm

h = 5 cm

w = 4 cm

体积 = l * b * h

= 10 * 5 * 4

= 200

算法

步骤 1 - 声明用于长度、宽度、高度、体积和面积的 float64 数据类型变量。

步骤 2 - 初始化变量。

步骤 3 - 使用上述公式在函数中计算表面积和体积。

步骤 4 - 打印结果。

示例

在此示例中,我们将通过函数计算长方体的表面积和体积。

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 Cuboid,
   
   // distance between the parallel sides and also a variable area
   
   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")
   
   // initializing the length of a Cuboid
   l = 10
   
   // initializing the width of a Cuboid
   w = 8
   
   // initializing the height of a Cuboid
   h = 4
   
   // finding the surface Area of a Cuboid
   surfaceArea = 2*l*w + 2*w*h + 2*l*h
   
   // finding the volume of a Cuboid
   volume = l * w * h
   
   // printing the result
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid within the function)")
}

输出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid within the function)

算法

步骤 1 - 声明用于长度、宽度、高度、体积和面积的 float64 数据类型变量。

步骤 2 - 初始化变量。

步骤 3 - 使用长方体的长度、宽度和高度作为参数调用函数,并将函数返回的面积存储起来。

步骤 4 - 使用长方体的长度、宽度和高度作为参数调用函数,并将函数返回的体积存储起来。

步骤 5 - 打印结果。

示例

在此示例中,我们将通过定义单独的函数来计算长方体的面积。

package main

// fmt package provides the function to print anything
import ( 
   "fmt"
)

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func areaOfCuboid(l, w, h float64) float64 {
   
   // returning the area by applying the formula
   return 2*l*w + 2*w*h + 2*l*h
}

// in this line we have declared the function that has float64

// type parameter and float64 type returntype
func volumeOfCuboid(l, w, h float64) float64 {
   
   // returning the volume by applying the formula
   return l * w * h
}
func main() {
   
   // declaring the floating variables using the var keyword for
   
   // storing the length of the parallel sides of the Cuboid,
   
   // distance between the parallel sides and also a variable area
   
   // to store Area
   var l, w, h, surfaceArea, volume float64
   fmt.Println("Program to find the Surface Area and volume of a Cuboid.")
   
   // initializing the length of a Cuboid
   l = 10
   
   // initializing the width of a Cuboid
   w = 8
   
   // initializing the height of a Cuboid
   h = 4
   
   // finding the surface Area of a Cuboid by calling the function
   surfaceArea = areaOfCuboid(l, w, h)
   
   // finding the volume of a Cuboid
   volume = volumeOfCuboid(l, w, h)
   
   // printing the result 
   fmt.Println("The Surface Area of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", surfaceArea, "cm * cm.")
   fmt.Println("The volume of a Cuboid whose length , width, and height are", l, ",", w, ",", h, "is", volume, "cm * cm * cm.")
   fmt.Println("(Finding the Surface Area and volume of a Cuboid in the seperate function)")
} 

输出

Program to find the Surface Area and volume of a Cuboid.
The Surface Area of a Cuboid whose length , width, and height are 10 , 8 , 4 is 304 cm * cm.
The volume of a Cuboid whose length , width, and height are 10 , 8 , 4 is 320 cm * cm * cm.
(Finding the Surface Area and volume of a Cuboid in the seperate function)

结论

以上是两种在 Golang 中计算长方体面积和体积的方法。从模块化和代码重用性的角度来看,第二种方法更好,因为我们可以在项目的任何地方调用该函数。要了解更多关于 Go 的知识,您可以浏览这些 教程。

更新于: 2022-11-29

133 次浏览

开启您的 职业生涯

通过完成课程获得认证

开始学习
广告