-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquadratic_OOP.py
79 lines (64 loc) · 2.25 KB
/
quadratic_OOP.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from re import findall
from contracts import contract, new_contract
import stack
import doctest
def is_str_empty(my_string):
if len(my_string) == 0:
error_message = 'Введи, что-то долбаеб'
raise ValueError(error_message)
def get_user_equation():
user_task = input('Input your equation ')
return user_task
def get_kind_of_solving():
my_equation = get_user_equation()
my_kind = input('Укажите метод решения: (1 - Теорема Виетта, 2 - Дискриминант) ')
if my_kind == '1':
my_solving = SolvingQuadraticEquation(my_equation)
my_res = my_solving.solving_by_viett()
elif my_kind == '2':
my_solving = SolvingQuadraticEquation(my_equation)
my_res = my_solving.solving_by_discriminant()
else:
raise ValueError("Ты кто такой, чтобы это делать?")
return my_res
class SolvingQuadraticEquation:
def __init__(self, user_equation):
self.user_equation = user_equation
def get_coeff(self):
user_task = '5 4 3'
return findall('\d+', self.user_equation)
def solving_by_discriminant(self):
user_coeff = SolvingQuadraticEquation.get_coeff(self)
a = int(user_coeff[0])
b = int(user_coeff[1])
c = int(user_coeff[2])
d = (b * b) - (4 * a * c)
if d < 0:
ValueError("No solutions!")
x1 = (-b - d ** 1 / 2) // (2 * a)
x2 = (-b + d ** 1 / 2) // (2 * a)
res = (x1, x2)
return res
def solving_by_viett(self):
user_coeff = SolvingQuadraticEquation.get_coeff(self)
a = int(user_coeff[0])
b = int(user_coeff[1])
c = int(user_coeff[2])
D = (b ** 2 - 4 * a * c)
x1 = x2 = 0
points = [i for i in range(-100, 100)]
if D >= 0:
for i in points:
x1 = i
for j in points:
x2 = j
if x1 + x2 == -b / a and x1 * x2 == c / a:
res = (x1, x2)
return res
elif D < 0:
return ValueError("No solutions!")
def main():
my_solving = get_kind_of_solving()
print(my_solving)
if __name__ == '__main__':
main()