Advertisement
JmihPodvalbniy

Untitled

Oct 18th, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.42 KB | Software | 0 0
  1. ### Дз от 14.10.2024г.
  2.  
  3. 1)
  4. import math
  5. class Kvadr_urav:
  6.     def __init__(self, a, b, c):
  7.         self.a = a
  8.         self.b = b
  9.         self.c = c
  10.  
  11.     def solve(self):
  12.         delta = (self.b**2) - 4 * (self.a * self.c)
  13.  
  14.         if delta > 0:  # Случай, когда корень из дискриминанта: > 0.
  15.             x1 = (-self.b - delta**0.5) / (2 * self.a)
  16.             x2 = (-self.b + delta**0.5) / (2 * self.a)
  17.             return x1, x2
  18.  
  19.         elif delta == 0:  # Случай, когда корень из дискриминанта: = 0.
  20.             x = (-self.b - delta**0.5) / (2 * self.a)
  21.             return x
  22.  
  23.         elif delta < 0:  # Случай, когда корень из дискриминанта: < 0.
  24.             return 'Корней нет'
  25.  
  26. """Пример использования"""
  27. equation1 = Kvadr_urav(1, -3, 2)
  28. solution1 = equation1.solve()
  29. print(f'Уравнение: {equation1.a}x^2 + {equation1.b}x + {equation1.c} = 0, корни(ень) уравнения: {solution1}')
  30.  
  31. equation2 = Kvadr_urav(1, 4, 5)
  32. solution2 = equation2.solve()
  33. print(f'Уравнение: {equation2.a}x^2 + {equation2.b}x + {equation2.c} = 0, корни(ень) уравнения: {solution2}')
  34.  
  35. equation3 = Kvadr_urav(1, 2, 1)
  36. solution3 = equation3.solve()
  37. print(f'Уравнение: {equation3.a}x^2 + {equation3.b}x + {equation3.c} = 0, корни(ень) уравнения: {solution3}')
  38.  
  39. 2)
  40. import math
  41.  
  42. class Shape:
  43.  
  44.     def __init__(self, color, border_thickness):
  45.         self.color = color
  46.         self.border_thickness = border_thickness
  47.  
  48.     def area(self):
  49.         raise NotImplementedError()
  50.  
  51. class Circle(Shape):
  52.  
  53.     def __init__(self, color, border_thickness, radius):
  54.         super().__init__(color, border_thickness)
  55.         self.radius = radius
  56.  
  57.     def area(self):
  58.         return math.pi * (self.radius ** 2)
  59.  
  60.  
  61. class Rectangle(Shape):
  62.  
  63.     def __init__(self, color, border_thickness, width, height):
  64.         super().__init__(color, border_thickness)
  65.         self.width = width
  66.         self.height = height
  67.  
  68.     def area(self):
  69.         return self.width * self.height
  70.    
  71. """Пример использования"""
  72. circle1 = Circle("red", 2, 5)
  73. print(f"Площадь круга: {circle1.area()}")
  74. print(f"Цвет круга: {circle1.color}, Толщина границы: {circle1.border_thickness}, Радиус: {circle1.radius}")
  75.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement