Swift程式:求長方體的表面積和體積


本教程將討論如何編寫一個Swift程式來求長方體的表面積和體積。

長方體是一個三維立體圖形,具有六個矩形面、八個頂點和十二條邊。它具有三個維度:長、寬和高。如果長方體的邊長都是整數,則該長方體為完美長方體。長方體的相對邊總是平行的,所有頂點形成的角都是90度。


長方體的表面積

長方體的表面積是指長方體佔據的總空間。或者我們可以說,所有表面的總面積就是長方體的表面積。它分為兩種型別:

  • 總表面積 - 長方體所有6個面的面積之和稱為長方體的總表面積。

  • 側表面積 - 除底面和頂面外,長方體所有面的面積之和稱為長方體的側表面積。或者我們可以說,長方體的側表面積是四個垂直面的面積之和。

總表面積

公式

以下是長方體總表面積的公式:

S.A = 2[(length x breadth) + (length x height) + (breadth x height)]

求總表面積的演算法

  • 步驟1 - 定義三個變數(長、寬、高)

  • 步驟2 - 為這些變數賦值

  • 步驟3 - 應用總表面積公式 (2[(長 x 寬) + (長 x 高) + (寬 x 高)])

  • 步驟4 - 列印輸出

示例

下面的程式展示瞭如何計算長方體的總表面積。

import Foundation import Glibc var length = 60 var breadth = 10 var height = 10 var surfaceAreaOfCuboid = 2 * ((length * breadth) + (length * height) + (breadth * height)) print("Length of cuboid is - ", length) print("Breadth of cuboid is - ", breadth) print("Height of cuboid is -", height) print("\nFinal surface area of the cuboid is - ", surfaceAreaOfCuboid)

輸出

Length of cuboid is - 60
Breadth of cuboid is - 10
Height of cuboid is - 10
Final surface area of the cuboid is - 2600

在上面的程式碼中,我們使用如下所示的數學公式來計算長方體的總表面積:

var surfaceAreaOfCuboid = 2 * ((length * breadth) + (length * height) + (breadth * height))

這裡,長方體的長、寬和高分別為60、10和10。所以長方體的總表面積是2600。

側表面積

公式

以下是長方體側表面積的公式:

S.A = 2 x height x [(length + breadth)]

求側表面積的演算法

  • 步驟1 - 定義三個變數(長、寬、高)

  • 步驟2 - 為這些變數賦值

  • 步驟3 - 應用側表面積公式 (2 x 高 x [(長 + 寬)])

  • 步驟4 - 列印輸出

示例

下面的程式展示瞭如何計算長方體的側表面積。

import Foundation import Glibc var length = 50 var breadth = 20 var height = 20 var LateralsurfaceAreaOfCuboid = 2 * height * (length + breadth) print("Length of cuboid is - ", length) print("Breadth of cuboid is - ", breadth) print("Height of cuboid is -", height) print("\nLateral Surface Area is - ", LateralsurfaceAreaOfCuboid)

輸出

Length of cuboid is - 50 
Breadth of cuboid is - 20 
Height of cuboid is - 20
Lateral Surface Area is - 2800

在上面的程式碼中,我們使用如下所示的數學公式來計算長方體的側表面積:

var LateralsurfaceAreaOfCuboid = 2 * height * (length + breadth)

這裡,長方體的長、寬和高分別為50、20和20。所以長方體的側表面積是2800。

長方體的體積

長方體的體積是指長方體佔據的空間量,它取決於長方體的三個維度:長、寬和高。所以長方體的體積是其三個維度(長、寬和高)的乘積。

公式

以下是長方體體積的公式:

V = length x breadth x height

求長方體體積的演算法

  • 步驟1 - 定義三個變數(長、寬、高)

  • 步驟2 - 為這些變數賦值

  • 步驟3 - 應用長方體體積公式 (長 x 寬 x 高)

  • 步驟4 - 列印輸出

示例

下面的程式展示瞭如何計算長方體的體積。

import Foundation import Glibc var length = 70 var breadth = 20 var height = 20 var volumeOfCuboid = length * breadth * height print("Length of cuboid is - ", length) print("Breadth of cuboid is - ", breadth) print("Height of cuboid is -", height) print("Cuboid’s volume - ", volumeOfCuboid)

輸出

Length of cuboid is - 70 
Breadth of cuboid is - 20 
Height of cuboid is - 20
Cuboid’s volume - 28000

在上面的程式碼中,我們使用如下所示的數學公式來計算長方體的體積:

var volumeOfCuboid = length * breadth * height

這裡,長方體的長、寬和高分別為70、20和20。所以長方體的體積是28000。

更新於:2022年8月1日

732 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.