6-1 二維平面上的點操作(Python3)
題目描述
設計一個表示二維平面上點的類 Point
。該類應該包含以下功能:
-
兩個私有屬性
_x
和_y
,分別表示點的橫坐標和縱坐標。 -
一個構造函數
__init__
,用于初始化點的坐標。 -
一個方法
distance_to_origin
,返回該點到原點 (0, 0) 的歐幾幾何距離。 -
一個方法
move_by
,接受兩個參數dx
和dy
,將點的橫坐標和縱坐標分別增加dx
和dy
。 -
一個特殊方法
__str__
,返回點的信息字符串,格式為"Point(x, y)"
。
函數接口定義(你實現)
class Point:def __init__(self, x, y):# 初始化點的坐標passdef distance_to_origin(self):# 返回點到原點的距離passdef move_by(self, dx, dy):# 移動點的位置passdef __str__(self):# 返回點的信息字符串pass
裁判測試程序樣例
def main():# 用戶輸入點的初始坐標 (x, y)# 使用 input() 讀取一行輸入,并用 split() 將其分割成兩個字符串# 使用 map(float, ...) 將這兩個字符串轉換為浮點數x, y = map(float, input("請輸入點的初始坐標 (x y): ").split())# 創建 Point 類的實例,傳入初始坐標 (x, y)point = Point(x, y)# 調用 point 的 distance_to_origin 方法計算點到原點 (0, 0) 的距離# 使用 f-string 格式化輸出,保留兩位小數print(f"Distance to origin: {point.distance_to_origin():.2f}")# 用戶輸入移動量 (dx, dy)# 使用 input() 讀取一行輸入,并用 split() 將其分割成兩個字符串# 使用 map(float, ...) 將這兩個字符串轉換為浮點數dx, dy = map(float, input("請輸入移動量 (dx dy): ").split())# 調用 point 的 move_by 方法,將點的位置按 (dx, dy) 移動point.move_by(dx, dy)# 打印移動后的點的信息# 調用 point 的 __str__ 方法,返回點的字符串表示print(point)#執行 main 函數
if __name__ == "__main__":main()
輸入樣例
3 4
1 2
輸出樣例
Distance to origin: 5.00
Point(4.0, 6.0)
實現提示
-
使用
math.sqrt
來計算歐幾里得距離(開根號)。 -
確保
__str__
方法返回正確的字符串格式。
?代碼
import math class Point: def __init__(self, x, y): # 初始化點的坐標 self._x = x self._y = y def distance_to_origin(self): # 返回點到原點的距離 return math.sqrt(self._x**2 + self._y**2) def move_by(self, dx, dy): # 移動點的位置 self._x += dx self._y += dy def __str__(self): # 返回點的信息字符串 return f"Point({self._x}, {self._y})"
?
?6-2 圖形面積計算(Python3)
?
圖形編輯器
題目描述
設計一個簡單的圖形編輯器系統,該系統包括以下類:
-
Shape
:所有圖形的基類。 -
Circle
:表示圓形,繼承自Shape
。 -
Rectangle
:表示矩形,繼承自Shape
。 -
Triangle
:表示三角形,繼承自Shape
。
每個圖形類都需要實現以下方法:
-
area()
:返回圖形的面積。 -
perimeter()
:返回圖形的周長。
此外,**題目已經編寫好了主程序和測試函數,學生只需實現上述這些方法即可。
函數接口定義(你實現的部分)
class Shape:def area(self):raise NotImplementedError("This method should be overridden by subclasses")def perimeter(self):raise NotImplementedError("This method should be overridden by subclasses")class Circle(Shape):def __init__(self, radius):self.radius = radiusdef area(self):# 計算并返回圓的面積passdef perimeter(self):# 計算并返回圓的周長passclass Rectangle(Shape):def __init__(self, width, height):self.width = widthself.height = heightdef area(self):# 計算并返回矩形的面積passdef perimeter(self):# 計算并返回矩形的周長passclass Triangle(Shape):def __init__(self, a, b, c):self.a = aself.b = bself.c = cdef area(self):# 計算并返回三角形的面積passdef perimeter(self):# 計算并返回三角形的周長pass
?
裁判測試程序樣例(判卷邏輯)
def print_shape_info(shape):# 打印給定形狀的面積和周長print(f"Area: {shape.area():.2f}")print(f"Perimeter: {shape.perimeter():.2f}")def main():# 用戶輸入圖形類型和相應的參數shape_type = input("")if shape_type == 'circle':radius = float(input(""))shape = Circle(radius)elif shape_type == 'rectangle':width, height = map(float, input("").split())shape = Rectangle(width, height)elif shape_type == 'triangle':a, b, c = map(float, input("").split())shape = Triangle(a, b, c)else:print("未知的圖形類型")return# 打印圖形的面積和周長print_shape_info(shape)if __name__ == "__main__":main()
輸入樣例 1
circle
5
輸出樣例 1
Area: 78.54
Perimeter: 31.42
輸入樣例 2
rectangle
4 6
輸出樣例 2
Area: 24.00
Perimeter: 20.00
輸入樣例 3
triangle
3 4 5
輸出樣例 3
Area: 6.00
Perimeter: 12.00
代碼?
import math class Shape: def area(self): raise NotImplementedError("This method should be overridden by subclasses") def perimeter(self): raise NotImplementedError("This method should be overridden by subclasses") class Circle(Shape): def __init__(self, radius): self.radius = radius def area(self): # 計算并返回圓的面積 return math.pi * (self.radius**2) def perimeter(self): # 計算并返回圓的周長 return 2 * math.pi * self.radius class Rectangle(Shape): def __init__(self, width, height): self.width = width self.height = height def area(self): # 計算并返回矩形的面積 return self.width * self.height def perimeter(self): # 計算并返回矩形的周長 return 2 * (self.width + self.height) class Triangle(Shape): def __init__(self, a, b, c): self.a = a self.b = b self.c = c def area(self): # 計算并返回三角形的面積(海倫公式) s = (self.a + self.b + self.c) / 2 return math.sqrt(s * (s - self.a) * (s - self.b) * (s - self.c)) def perimeter(self): # 計算并返回三角形的周長 return self.a + self.b + self.c
?