Python程式用於計算長方體的體積、表面積和空間對角線


本文將討論如何計算長方體的體積、表面積和空間對角線。長方體是一種3D幾何形狀,類似於一個矩形盒。它也被稱為矩形稜柱。長方體有六個矩形面和十二條邊。長方體的長、寬、高各不相同,但在立方體的情況下,所有邊都相等。本文說明了兩個示例。第一個示例使用了使用者定義函式的概念,第二個示例使用了定義類的引數化建構函式來計算長方體的體積、表面積及其空間對角線。

示例 1:利用使用者定義函式。(編碼風格 1)

程式碼解釋和設計步驟

步驟 1:在Anaconda提示符中開啟Jupyter Notebook,並在其單元格中開始編寫程式碼。

步驟 2:使用三個函式:‘cal_volume’,‘cal_surface_area’,‘cal_spac_diagonal’。

步驟 3:函式將長方體的長、寬、高作為輸入引數,表示長方體的尺寸。

步驟 4:使用體積公式:長 (l) * 寬 (b) * 高 (h)。

步驟 5:使用表面積公式:2*( 長 (l) * 寬 (b)+ 寬 (b) * 高 (h) + 高 (h) * 長 (l)。

步驟 6:透過將長方體的每一側乘以二,最後將所有側面的面積相加來計算表面積。它將返回計算出的表面積值。

步驟 7:使用空間對角線公式:空間對角線 = [( 長 (l)2 *寬 (b)2 * 長 (l)2] 1/2。這用於使用勾股定理計算長方體的空間對角線。

步驟 8:返回計算出的值,例如體積、表面積和空間對角線。

步驟 9:檢查結果。

示例

import math
#User-defined function to calculate the volume
def calculate_volume(length, breadth, height):
    volume = length * breadth * height
    return volume
#Evaluate the Surface area
def calculate_surface_area(length, breadth, height):
    surface_area = 2 * (length * breadth + breadth * height + height * length)
    return surface_area
#Evaluate space diagonal of a cuboid
def calculate_space_diagonal(length, breadth, height):
    space_diagonal = math.sqrt(length ** 2 + breadth ** 2 + height ** 2)
    return space_diagonal

# Example 
length = 8
breadth = 5
height = 6

volume = calculate_volume(length, breadth, height)
surface_area = calculate_surface_area(length, breadth, height)
space_diagonal = calculate_space_diagonal(length, breadth, height)

print("Volume cuboid:", volume)
print("Surface Area cuboid:", surface_area)
print("Space Diagonal cuboid:", space_diagonal)

輸出

Volume cuboid: 240
Surface Area cuboid: 236
Space Diagonal cuboid: 11.180339887498949

示例 2:使用建構函式。

程式碼解釋和設計步驟

步驟 1:在Anaconda提示符中開啟Jupyter Notebook,並在其單元格中開始編寫程式碼。

步驟 2:使用名為‘Cuboid’的類。

步驟 3:‘def __init__(self, length, width, height)’: 這是‘Cuboid’類的建構函式方法。

步驟 4:‘init’方法用於在建立‘Cuboid’物件時初始化其屬性。

步驟 5:‘self’指的是類的例項。

步驟 6:‘init’方法接受四個引數:‘self’,‘length’,‘breadth’和‘height’。

步驟 7:使用三個函式:‘cal_volume’,‘cal_surface_area’,‘cal_spac_diagonal’

步驟 8:函式將長方體的長、寬、高作為輸入引數,表示長方體的尺寸。

步驟 9:使用體積公式:長 (l) * 寬 (b) * 高 (h)

步驟 10:使用表面積公式:2*( 長 (l) * 寬 (b)+ 寬 (b) * 高 (h) + 高 (h) * 長 (l)。它透過將長方體的每一側乘以二,最後將所有側面的面積相加來計算表面積。它將返回計算出的表面積值。

步驟 11:空間對角線用於使用勾股定理計算長方體的空間對角線。

步驟 12:返回計算出的值,例如體積、表面積和空間對角線。

步驟 13:檢查結果。

示例

import math
#define class
class Cuboid:
    def __init__(self, length, width, height):
        self.length = length
        self.width = width
        self.height = height
   
    def cal_volume(self):
        return self.length * self.width * self.height
   
    def cal_surface_area(self):
        return 2 * (self.length * self.width + self.width * self.height + self.height * self.length)
   
    def cal_space_diagonal(self):
        return math.sqrt(self.length ** 2 + self.width ** 2 + self.height ** 2)

# Example
length = 11
width = 4
height = 7

cuboid = Cuboid(length, width, height)

volume = cuboid.cal_volume()
surface_area = cuboid.cal_surface_area()
space_diagonal = cuboid.cal_space_diagonal()

print("Volume cuboid:", volume)
print("Surface Area cuboid:", surface_area)
print("Space Diagonal cuboid:", space_diagonal)

輸出

Volume cuboid: 308
Surface Area cuboid: 298
Space Diagonal cuboid: 13.638181696985855

在編碼風格 2 中,我們可以定義一個“Cuboid”類來封裝長方體的長、寬、高。根據長方體的屬性,我們使用類的這些方法獲得結果。

本文透過示例中不同的函式,展示瞭如何求長方體的體積、表面積和空間對角線。它在各種場景中都很有用,例如建築和施工、包裝和物流、3D 建模和計算機圖形學、數學和教育以及科學研究。

更新時間: 2023年8月28日

3K+ 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告