-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·194 lines (174 loc) · 5.34 KB
/
main.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python3
# this program divides equation in monomials,
# finds indexes(like a,b,c in quadratic or linear equation),
# adds them up and solves with formulas
import math
import cmath
import sys
symbols = ('+', '-', '=', '^', 'x', '.')
splitsymbols = ('+', '-', '=')
#prints complex num
def print_complex(comp):
comp_print = ''
if comp.real == 0:
comp_print = '{0}i'.format(comp.imag)
else:
comp_print = '{0}{1:+}i'.format(comp.real, comp.imag)
return comp_print
#
def append_index(indl, monomial, isEqPassed):
try:
index = 0.0
arr_num = 0
monomial_def = ''
last_float_pos = 0
# 1 GET INDEX
# if sign is missing, add '+'
if monomial[0] != '+' and monomial[0] != '-':
monomial = '+' + monomial
# if index is missing, index is 1
if not monomial[1].isdigit():
if monomial[0] == '-':
index = -1.0
else:
index = 1.0
monomial_def = monomial[1:]
else:
# get index
count = 0
for ch in monomial:
if ch not in ('+', '-', '.') and not ch.isdigit():
last_float_pos = count
index = float(monomial[:last_float_pos])
monomial_def = monomial[last_float_pos:]
break
if count == len(monomial) - 1:
index = float(monomial)
count += 1
if isEqPassed:
index = -index
# 2 GET A, B OR C IN EQUATION
# get index in array to append
if monomial_def == '':
arr_num = 0
elif monomial_def == 'x':
arr_num = 1
elif monomial_def == 'x^2':
arr_num = 2
else:
print('error with monomial: ', monomial_def)
return []
# 3 APPEND
indl[arr_num] += index
return indl
# 4 if index is incorrect
except ValueError as msg:
print('error: ', msg)
return []
# check if the equation is incorrect
def is_correct_equation(eq):
for ch in eq:
if not ch in symbols and not ch.isdigit():
return False
if '=' not in eq:
return False
return True
# get indexes for every type of monomial
def get_indexes(eq):
indexes = [0.0, 0.0, 0.0, 0.0]
is_eq_pass = False
last_monomial_pos = 0
this_pos = 0
for ch in eq:
mon = ''
if this_pos == len(eq) - 1:
mon = eq[last_monomial_pos:]
indexes = append_index(indexes, mon, is_eq_pass)
if not indexes:
return []
elif ch in splitsymbols:
# if th sign in the begining of equation or after '='
if this_pos == 0 or eq[this_pos-1] == '=':
this_pos += 1
continue
mon = eq[last_monomial_pos:this_pos]
indexes = append_index(indexes, mon, is_eq_pass)
if not indexes:
return []
# append indexes
if ch == '=':
is_eq_pass = True
last_monomial_pos = this_pos + 1
elif ch == '+' or ch == '-':
last_monomial_pos = this_pos
this_pos += 1
return indexes
def solve_linear(indexes):
return (-indexes[0] / indexes[1],)
def solve_quadratic(indexes):
a = indexes[2]
b = indexes[1]
c = indexes[0]
print("D = b\u00b2 - 4ac")
discriminant = b ** 2 - 4 * a * c
print("D = {0}".format(discriminant))
if discriminant == 0:
print('x = -b / 2a')
return [-b / (2 * a)]
elif discriminant > 0:
print("x = (-b - \u221aD) / 2a")
root = math.sqrt(discriminant)
else:
print("x = (-b - i\u221aD) / 2a")
root = cmath.sqrt(discriminant)
return ((-b - root) / (2 * a), (-b + root) / (2 * a))
def solve_equation(eq):
if not is_correct_equation(eq):
print("equation is incorrect")
return ()
indexes = get_indexes(eq)
if not indexes:
return ()
if indexes[1] == 0.0 and indexes[2] == 0.0 and indexes[0] != 0.0:
print('x \u2208 \u2205')
return ()
elif indexes[1] == 0.0 and indexes[2] == 0.0 and indexes[0] == 0.0:
print('x \u2208 \u211d')
return ()
if indexes[2] == 0.0:
return solve_linear(indexes)
else:
return solve_quadratic(indexes)
def print_results(slist):
if not slist:
pass
else:
for solve in slist:
if isinstance(solve, complex):
print("x =", print_complex(solve))
else:
print("x =", solve)
def main():
# header text
print('{0:-^40}'.format("equation_solver"))
print('\n\n{0:-^40}\n\n'.format("by Artiom Podgajskij"))
eq = ''
if len(sys.argv) <= 1:
print("input the equations, or '/end' (Ctrl + D) for finish")
while True:
try:
eq = input(" ")
if eq == '/end':
break
else:
if len(eq.split(' ')) == 1:
solvelist = solve_equation(eq)
print_results(solvelist)
print("\n")
except EOFError:
break
else:
eq = sys.argv[1]
solvelist = solve_equation(eq)
print_results(solvelist)
main()