Swift 程式計算圓柱體體積和表面積


本教程將討論如何編寫一個 Swift 程式來計算圓柱體的體積和表面積。

圓柱體是一種三維形狀,它有兩個相同的平行圓形底面,由一個曲面連線。

圓柱體的體積

圓柱體在三維空間中佔據的空間量稱為圓柱體的體積。例如,我們想用洗髮水填充一個圓柱形瓶子,那麼使用體積我們可以計算出所需的洗髮水量。我們可以使用圓柱體的半徑和高度來計算圓柱體的體積。

公式

以下是圓柱體體積的公式:

Volume = πr2h

下面是演示:

輸入

假設我們的給定輸入為:

Radius = 8 
Height = 14

輸出

期望的輸出為:

Volume of the cylinder = 2814.8670176164546

演算法

以下是演算法:

  • 步驟 1 - 宣告兩個雙精度型別變數來儲存圓柱體的高度和半徑:

var cRadius : Double = 5.0 
var cHeight : Double = 15.0

此處這些變數的值可以是使用者定義的或預定義的。

  • 步驟 2 - 宣告一個名為 cVolume 的變數,使用以下公式儲存圓柱體的體積:

var cVolume = Double.pi * cRadius * cRadius * cHeight
  • 步驟 3 - 列印輸出

示例

以下程式展示瞭如何找到圓柱體的體積。

import Foundation import Glibc var cRadius : Double = 5.0 var cHeight : Double = 15.0 // Calculating the volume of the cylinder var cVolume = Double.pi * cRadius * cRadius * cHeight print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the volume of the cylinder is:", cVolume)

輸出

Radius of the cylinder is: 5.0 
Height of the cylinder is: 15.0 
Hence the volume of the cylinder is: 1178.0972450961724

這裡,在上面的程式碼中,我們使用以下數學公式計算圓柱體的體積:

var cVolume = Double.pi * cRadius * cRadius * cHeight

顯示結果 1178.0972450961724(體積 = 3.141592653589793 * 5 * 5 * 15 = 1178.0972450961724)。

圓柱體的表面積

圓柱體在三維空間中覆蓋的總空間或區域稱為圓柱體的表面積。圓柱體有兩種型別的表面積:

  • 曲面面積
  • 總表面積

下面是演示:

輸入

假設我們的給定輸入為:

Radius = 4 
Height = 8

輸出

期望的輸出為

Area = 2814.8670176164546

1. 曲面面積

圓柱體曲面佔據的空間,或者我們可以說兩個平行底面之間佔據的區域稱為圓柱體的曲面面積。它也稱為圓柱體的側面積。

公式

以下是圓柱體曲面面積的公式:

Area = 2πrh

演算法

以下是演算法:

  • 步驟 1 - 宣告兩個雙精度型別變數來儲存圓柱體的高度和半徑:

var cRadius : Double = 8.0 
var cHeight : Double = 14.0

此處這些變數的值可以是使用者定義的或預定義的。

  • 步驟 2 - 宣告一個名為 cArea 的變數,使用以下公式儲存圓柱體的曲面面積:

var cArea = 2 * Double.pi * cRadius * cHeight
  • 步驟 3 - 列印輸出

示例

以下程式展示瞭如何找到圓柱體的曲面面積。

import Foundation import Glibc var cRadius : Double = 8.0 var cHeight : Double = 14.0 // Calculating the curved surface area of the cylinder var cArea = 2 * Double.pi * cRadius * cHeight print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the curved surface area of the cylinder is:", cArea)

輸出

Radius of the cylinder is: 8.0
Height of the cylinder is: 14.0
Hence the curved surface area of the cylinder is: 703.7167544041137

這裡,在上面的程式碼中,我們使用以下數學公式計算圓柱體的曲面面積:

var cArea = 2 * Double.pi * cRadius * cHeight

顯示結果 703.7167544041137 (CSA = 2 * 3.141592653589793 * 8 * 14 = 703.7167544041137)。

2. 總表面積

圓柱體所有表面的面積之和稱為總表面積。或者換句話說,兩個圓形底面面積和曲面面積之和稱為總表面積。

公式

以下是圓柱體總表面積的公式:

Area = 2πr(h+r)

演算法

以下是演算法:

  • 步驟 1 - 宣告兩個雙精度型別變數來儲存圓柱體的高度和半徑:

var cRadius : Double = 4.0 
var cHeight : Double = 8.0

此處這些變數的值可以是使用者定義的或預定義的。

  • 步驟 2 - 宣告一個名為 cArea 的變數,使用以下公式儲存圓柱體的總表面積:

var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius)
  • 步驟 3 - 列印輸出

示例

以下程式展示瞭如何找到圓柱體的總表面積。

import Foundation import Glibc var cRadius : Double = 4.0 var cHeight : Double = 8.0 // Calculating the total surface area of the cylinder var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius) print("Radius of the cylinder is:", cRadius) print("Height of the cylinder is:", cHeight) print("Hence the total surface area of the cylinder is:", cArea)

輸出

Radius of the cylinder is: 4.0
Height of the cylinder is: 8.0
Hence the total surface area of the cylinder is: 301.59289474462014

這裡,在上面的程式碼中,我們使用以下數學公式計算圓柱體的總表面積:

var cArea = 2 * Double.pi * cRadius * (cHeight + cRadius)

顯示結果 301.59289474462014 (TSA = 2 * 3.141592653589793 * 4 * (8 + 4) = 301.59289474462014)。

更新於: 2022年8月25日

325 次檢視

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告