کلاس مکعب مستطیل
کلاسی بنویسید که طول، عرض و ارتفاع مکعب مستطیل را دریافت کند و مساحت و حجم آن را با استفاده از متدهای مختلف محاسبه کند
👨💻 10 ساعت قبل کاربر ناشناس این تمرین رو مشاهده کرد
کلاسی بنویسید که طول، عرض و ارتفاع مکعب مستطیل را دریافت کند و مساحت و حجم آن را با استفاده از متدهای مختلف محاسبه کند
class RectangularPrism:
@staticmethod
def calculate_surface_area(length, width, height):
return 2 * (length * width + length * height + width * height)
@staticmethod
def calculate_volume(length, width, height):
return length * width * height
if __name__ == "__main__":
while True:
try:
length = float(input("لطفاً طول مکعب مستطیل را وارد کنید: "))
width = float(input("لطفاً عرض مکعب مستطیل را وارد کنید: "))
height = float(input("لطفاً ارتفاع مکعب مستطیل را وارد کنید: "))
break
except ValueError:
print("لطفاً یک عدد صحیح یا اعشاری وارد کنید.")
surface_area = RectangularPrism.calculate_surface_area(length, width, height)
volume = RectangularPrism.calculate_volume(length, width, height)
print(f"مساحت سطح مکعب مستطیل: {surface_area} واحد مربع")
print(f"حجم مکعب مستطیل: {volume} واحد مکعب")
hight = int(input("The height of the rectangle:"))
lenght = int(input("The length of the rectangle:"))
width = int(input("The width of the rectangle:"))
print("The volume of the rectangle is ", hight * lenght * width)
print("The area of the rectangle is ", 2 *
(hight * lenght)+(hight * width)+(lenght * width))
class Cube: #تعریف کلاس و پراپرتی ها
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
def area(self): #متد مساحت
A = (self.width * self.height + self.length *
self.height + self.length * self.width) * 2
return f"\narea = {A}\n"
def volume(self): #متد حجم
V = self.height * self.width * self.length
return f"\nvolume = {V}\n"
A_and_V = int(input("\narea ---> 1 & volume ---> 2 : "))
#یک یا دو باشد مساحت یا حجم نمایش داده میشود A_and_V اگر
if A_and_V == 1 or A_and_V == 2:
#دریافت طول، عرض و ارتفاع
l = float(input("\nlength : "))
w = float(input("\nwidth : "))
h = float(input("\nheight : "))
cobe = Cube(l, w, h) #object
if A_and_V == 1: #اگر 1 باشد مساحت نمایش داده میشود
print(cobe.area())
elif A_and_V == 2: #اگر 2 باشد حجم نمایش داده میشود
print(cobe.volume())
#در غیر این صورت متن زیر چاپ میشود
else:
print("\nThere are only 2 options !! (1 and 2)\n")
length= int(input("what's your number?"))
width= int(input("what's your number?"))
height= int(input("what's your number?"))
area= length*width
volume= length*width*height
print(f"area:{area}__volume:{volume}")
class RectangularCube:
def __init__(self,width_rectangular,length_rectangular,height_rectangular):
self.width_rectangular=width_rectangular
self.length_rectangular=length_rectangular
self.height_rectangular=height_rectangular
def volume_rectangular(self):
return self.width_rectangular*self.length_rectangular*self.height_rectangular
def area_rectangular(self):
return 2*(self.width_rectangular*self.length_rectangular+self.length_rectangular*self.height_rectangular+self.width_rectangular*self.height_rectangular)
while True:
a=float(input("The width of the rectangular cube:\n"))
b=float(input("Length of rectangular cube:\n"))
c=float(input("Height of rectangular cube:\n"))
if a<0 or b<0 or c<0:
print("The input number is negative, please enter it again")
continue
else:
break
u=RectangularCube(a,b,c)
while True:
e=input("choose: 1.Volume 2.Area\n")
if e=="1":
print(f"Volume of a rectangular cube : {u.volume_rectangular()}")
break
elif e=="2":
print(f"Area of rectangular cube : {u.area_rectangular()}")
break
else:
print("The entered number is incorrect. Please try again")
continue
class Hajml_Masahat:
def __init__(self,tool,arz,ertefah):
self.tool = tool
self.arz = arz
self.ertefah = ertefah
def masahat(self):
s = self.tool * self.arz
return f"masahat mostatil = {s}"
def hajm(self):
haj = self.tool * self.arz * self.ertefah
return f"hajm mostatil = {haj}"
tool = int(input("enter your tool ="))
arz = int(input("enter your arz ="))
ertefah = int(input("enter your ertefah = "))
print(Hajml_Masahat(tool,arz,ertefah).hajm())
print(Hajml_Masahat(tool,arz,ertefah).masahat())
# Rectangular Cuboid Class
class Cuboid:
def __init__(self, length, width, height):
self.length = length
self.width = width
self.height = height
# Calculate surface area of the cuboid
def area(self):
return 2 * (self.length * self.width + self.length * self.height + self.width * self.height)
# Calculate volume of the cuboid
def volume(self):
return self.length * self.width * self.height
# Ask the user to choose between area or volume
choice = int(input("Select an option:\n1. Calculate Area\n2. Calculate Volume\nYour choice: "))
if choice in [1, 2]:
# Get the dimensions from the user
l = float(input("Enter the length: "))
w = float(input("Enter the width: "))
h = float(input("Enter the height: "))
# Create a Cuboid object
shape = Cuboid(l, w, h)
# Print the result based on the user's choice
if choice == 1:
print(f"\nSurface Area: {shape.area():.2f}")
else:
print(f"\nVolume: {shape.volume():.2f}")
else:
print("\nInvalid option. Please enter 1 or 2 only.")
class x:
def __init__(self , tool , arz , artefa):
self.tool=tool
self.arz=arz
self.artefa=artefa
def masahat(self):
print(self.tool*self.arz*6)
def hajm(self):
print(self.tool*self.arz*self.artefa)
while True:
k=input("1.adame 2.laghv :")
if k=="2":
break
elif k=="1":
t=float(input("tool:"))
a=float(input("arz:"))
ar=float(input("artefa:"))
m=x(t , a , ar)
i=input("1.masahat 2.hajm :")
if i=="1":
m.masahat()
elif i=="2":
m.hajm()
else:
print("error")
else:
print("error")
class RectangularPrism :
def __init__(self , length , width , high):
self.length = length
self.width = width
self.high = high
def area(self):
total_area = 2 * (self.length*self.width + self.high*self.length +self.high*self.width )
return f'Total area : {total_area} '
def volume (self):
return f'volume : {self.high * self.length * self.width}'
while 1 :
try :
length = float(input("length: \n "))
width = float (input('Width: \n '))
high = float(input('high: \n '))
break
except ValueError:
print('Enter an integer or a decimal number.')
c = RectangularPrism (length ,width , high)
print(c.area())
print(c.volume())
class maht:
def __init__(self,a,b,c):
self.a=a
self.b=b
self.c=c
def math_h(self):
return(f'The volume of a rectangular cube is equal to:{self.a*self.b*self.c}')
a1=int(input('enter length:'))
b1=int(input('enter width:'))
c1=int(input('enter height:'))
ob =maht(a1,b1,c1)
print(ob.math_h())
جوابت را با Markdown بنویس و ثبت کن
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نامتوضیح کوتاهی بنویسید تا تیم بررسی کند.