Golang 计算长方体的体积、对角线和面积

Golang 计算长方体的体积、对角线和面积

在本教程中,我们将讨论使用长方体的长度、宽度和高度来计算长方体的体积、对角线和面积的方法。

但在编写Golang代码之前,让我们简要讨论一下长方体及其体积、对角线和面积。

长方体

长方体是一个具有六个面和十二条边的三维图形。长方体的长度、宽度和高度都是不同的,不像正方体的所有边都相等。一个矩形盒子是长方体的一个很好的示例。

Golang 计算长方体的体积、对角线和面积

长方体的体积

长方体所占空间的量被称为其体积。计算长方体的体积可以在我们想知道一个房间可以容纳多少空气的情况下非常有益。

Volume = l * b * h

长方体的对角线

长方体的对角线可以定义为连接长方体的两个相对顶点的线段。

长方体的面积

长方体所占据的总面积称为长方体的面积。

Area = 2 * (l * h + l * b + h * b)

示例

  • 长度 = 10,宽度 = 3,高度 = 5

长方体的体积 = 150

长方体的对角线 = 11.575836902790225

长方体的表面积 = 190

解释

\mathrm{长方体的体积 = 长 \times 宽 \times 高}

\mathrm{= 10 \times 3 \times 5}

\mathrm{= 150}

\mathrm{长方体的对角线 = \sqrt({长^2 + 宽^2 + 高^2})}

\mathrm{\sqrt({10^2 + 3^2 + 5^2})}

\mathrm{= 11.575836902790225}

\mathrm{长方体的表面积 = 2 \times (长 \times 高 + 长 \times 宽 + 高 \times 宽)}

\mathrm{= 2 \times (10 \times 5 + 10 \times 3 + 5 \times 3)}

\mathrm{= 190}

计算长方体的体积、对角线和表面积

  • 步骤1 - 声明三个变量用于存储长方体的长度’l’、宽度’b’和高度’h’。

  • 步骤2 - 声明三个变量用于存储长方体的体积’volume’、对角线’diag’和表面积’area’,并将这三个变量初始化为0。

  • 步骤3 - 使用公式计算体积 – 体积 = l * b * h,并将其存储在函数calculateVolumeOfCuboid()中的’volume’变量中。

  • 步骤4 - 使用公式计算对角线 – 对角线 = √(l2 + b2 + h2),并将其存储在函数calculateDiagonalOfCuboid()中的’diag’变量中。

  • 步骤5 - 使用公式计算表面积 – 表面积 = 2 * (l * h + l * b + h * b),并将其存储在函数calculateAreaOfCuboid()中的’area’变量中。

  • 步骤6 - 打印计算得到的长方体的体积、对角线和表面积,即’volume’、’diag’和’area’变量中存储的值。

示例

package main
import (
    "fmt"
    "math"
)
func calculateVolumeOfCuboid(l, b, h float64) float64 {
    var volume float64 = 0
    volume = l * b * h
    return volume
}
func calculateDiagonalOfCuboid(l, b, h float64) float64 {
    var diag float64 = 0
    diag = math.Sqrt((l * l) + (b * b) + (h * h))
    return diag
}
func calculateAreaOfCuboid(l, b, h float64) float64 {
    var area float64 = 0
    area = 2 * ((l * h) + (l * b) + (h * b))
    return area
}
func main() {
   var l, b, h float64 = 10, 3, 5
    var volume, diag, area float64
    fmt.Println("Program to calculate the volume, diagonal and area of the Cuboid \n")
    volume = calculateVolumeOfCuboid(l, b, h)
    diag = calculateDiagonalOfCuboid(l, b, h)
    area = calculateAreaOfCuboid(l, b, h)

    fmt.Println("Length of the cuboid: ", l)
    fmt.Println("Breadth of the cuboid: ", b)
    fmt.Println("Height of the cuboid: ", h)
    fmt.Println("Therefore, Volume of cuboid: ", volume)
    fmt.Println("Diagonal of cuboid: ", diag)
    fmt.Println("Area of cuboid: ", area)
}

输出

Program to calculate the volume, diagonal and area of the Cuboid 

Length of the cuboid:  10
Breadth of the cuboid:  3
Height of the cuboid:  5
Therefore, Volume of cuboid:  150
Diagonal of cuboid:  11.575836902790225
Area of cuboid:  190

代码描述

  • calculateVolumeOfCuboid(l, b, h float64) float64 − 这个函数将计算长方体的体积,返回类型为float64。

  • diag = math.Sqrt((l * l) + (b * b) + (h * h)) − 使用这个公式,我们计算对角线。我们使用math包中的Sqrt函数来计算平方根。

结论

这是使用Go编程计算长方体的体积、对角线和面积的全部内容。我们还通过使用单独的函数来计算面积、对角线和体积来保持代码的模块化,这也增加了代码的可重用性。您可以使用这些教程来进一步了解Golang编程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程