-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomputor.py
More file actions
154 lines (125 loc) · 4.26 KB
/
computor.py
File metadata and controls
154 lines (125 loc) · 4.26 KB
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import sys
precision = 6
def my_abs(x):
return x if x > 0 else -x
def my_sqrt(n, EPS=1E-10):
x = 1
while 1:
nx = (x + n / x) / 2
if abs(x - nx) < EPS:
break
x = nx
return x
class InputException(Exception):
def __init__(self, equation):
Exception.__init__(self)
self.equation = equation
def get_degree(a, b, c):
if a == 0 and b == 0:
return 0
elif a == 0:
return 1
else:
return 2
def discriminant(a, b, c):
return b * b - 4 * a * c
def zero_degree_solution(c):
if c == 0:
print("The equation has any solution")
else:
print("The equation has no solution")
return
def linear_equation_solution(b, c):
print("The solution is: {}".format(round(- c / b, precision)))
return
def quadratic_equation_solution(a, b, c):
D = discriminant(a, b, c)
if D != 0:
sqrt = my_sqrt(my_abs(D))
first_part = -b / (2 * a)
second_part = sqrt / (2 * a)
if D > 0:
print("Discriminant is strictly positive, the two solutions are:")
print(round(first_part + second_part, precision))
print(round(first_part - second_part, precision))
else:
print("Discriminant less than zero, the two complex solutions are:")
print("{0} + {1} * i".format(round(first_part, precision), round(second_part, precision)))
print("{0} - {1} * i".format(round(first_part, precision), round(second_part, precision)))
else:
print("Discriminant is zero")
print("The solution is: {}".format(round(-b / (2 * a), precision)))
return
def print_info_about_equation(a, b, c, degree):
print("\nPolynomial degree: {}".format(degree))
print("Reduced form: {0} * X^2 + {1} * X^1 + {2} * X^0 = 0".format(a, b, c))
return
def get_coefficients(part):
a = 0.0
b = 0.0
c = 0.0
for elem in part:
if elem == '0' or elem == '':
continue
if "*" not in elem:
sys.exit("Incorrect input")
coefficient, var = elem.split('*')
var, degree = var.split('^')
if var != 'X' and var != 'x':
raise InputException('The variable can only be X or x')
try:
if int(degree) == 2:
a += float(coefficient)
elif int(degree) == 1:
b += float(coefficient)
elif int(degree) == 0:
c += float(coefficient)
else:
print("\nPolynomial degree: {0}".format(int(degree)))
sys.exit("The polynomial degree is strictly greater than 2, I can't solve.")
except ValueError:
raise InputException('Incorrect degree. Must be greater then 0')
return a, b, c
def parseEquation(equation):
equation = equation.replace(' ', '').replace('-', '+-').split('=')
parts = [equation[i].split('+') for i in range(len(equation))]
if len(parts) != 2:
raise InputException("Incorrect input. An equation has more than one equal sign or none at all")
return parts
def calculateCoefficients(equation):
parts = parseEquation(equation)
a = 0
b = 0
c = 0
for index, part in enumerate(parts):
a_tmp, b_tmp, c_tmp = get_coefficients(part)
if index == 0:
a += a_tmp
b += b_tmp
c += c_tmp
else:
a -= a_tmp
b -= b_tmp
c -= c_tmp
return a, b, c
def handle_degree_solution_route(a, b, c, degree):
if degree == 2:
quadratic_equation_solution(a, b, c)
elif degree == 1:
linear_equation_solution(b, c)
else:
zero_degree_solution(c)
return
def solve(equation):
a, b, c = calculateCoefficients(equation)
degree = get_degree(a, b, c)
print_info_about_equation(a, b, c, degree)
handle_degree_solution_route(a, b, c, degree)
return a, b, c
if __name__ == "__main__":
print("Equation format: A * X^2 + B * X^1 + C * X^0 = D * X^0.")
equation = input("Input equation: ")
try:
solve(equation)
except InputException as ex:
print("Incorrect Input")