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 - 打印输出

示例

以下程序展示了如何找到圆柱体的体积。

Open Compiler
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

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

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 - 打印输出

示例

以下程序展示了如何找到圆柱体的曲面面积。

Open Compiler
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 - 打印输出

示例

以下程序展示了如何找到圆柱体的总表面积。

Open Compiler
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 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告