-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcomputor.py
202 lines (176 loc) · 5.94 KB
/
computor.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
195
196
197
198
199
200
201
202
# **************************************************************************** #
# #
# ::: :::::::: #
# computor.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: obelouch <obelouch@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2020/11/07 19:37:47 by obelouch #+# #+# #
# Updated: 2020/11/07 20:11:18 by obelouch ### ########.fr #
# #
# **************************************************************************** #
#!/usr/bin/env python3
from srcs.resolver import resolve_deg_2, resolve_deg_1, resolve_deg_0
from srcs.polynome import dict_polynome
import sys
# Errors numbers
ERROR_NO_ARG = -1
ERROR_ARG_NBR = -2
ERROR_FLAG_NBR = -3
ERROR_FLAG = -4
ERROR_SYNTAX_EQUAL = -5
ERROR_SYNTAX_POLY_1 = -6
ERROR_SYNTAX_POLY_2 = -7
# Plot graph flag: -v
is_plot = False
# Show steps in details: -d
is_detail = False
# Dictionary that contain the polynome
polynome = {}
def fill_polynome(p1, p2):
'''
fill the dictionary polynome with p1 - p2
'''
for degree in p1:
if degree in polynome:
polynome[int(degree)] += float(p1[degree])
else:
polynome[int(degree)] = float(p1[degree])
for degree in p2:
if degree in polynome:
polynome[int(degree)] -= float(p2[degree])
else:
polynome[int(degree)] = -float(p2[degree])
def get_max_degree():
'''
Get max degree
'''
max_degree = 0
for degree in polynome:
if max_degree < degree and polynome[degree] != 0:
max_degree = degree
return max_degree
def print_simple_morph(val, degree):
'''
Print simple morph for the simple format
'''
if val != 0 or degree == 0:
n = val if val % 1 else int(val)
if degree == 0:
print(f'{n}', end="")
else:
if (val != 1):
print(f'{n} * ', end="")
if (degree != 1):
print(f'X^{degree}', end="")
else:
print(f'X', end="")
def print_reduced_format(max_degree):
'''
Print the full + simple reduced format
'''
poly = dict(sorted(polynome.items()))
# Reduced form
print("Reduced form: ", end="")
for degree in range(max_degree + 1):
n = poly[degree] if poly[degree] % 1 else int(poly[degree])
print(f'{n} * X^{degree}', end="")
if degree < max_degree:
print(" + ", end="")
print(" = 0")
# Simple form
if is_detail:
print("Simple form: ", end="")
for degree in range(max_degree + 1):
print_simple_morph(poly[degree], degree)
if degree < max_degree and poly[degree] != 0:
print(" + ", end="")
print(" = 0")
def print_result(max_degree):
'''
Print the result of the equation
'''
print_reduced_format(max_degree)
print(f"Polynomial degree: {max_degree}")
if max_degree > 2:
print("The polynomial degree is stricly greater than 2, I can't solve.")
elif max_degree == 2:
resolve_deg_2(polynome,is_detail,is_plot)
elif max_degree == 1:
resolve_deg_1(polynome,is_detail,is_plot)
else:
resolve_deg_0(polynome)
def dict_rebuild(max_degree):
'''
Rebuild the dictionary by filling the missing fields with 0
'''
for i in range(max_degree + 1):
if i not in polynome:
polynome[i] = 0
def get_flags_args(argv):
'''
Check & Get the flags, argument from argv
'''
global is_plot
global is_detail
flags = [arg for arg in argv if len(arg) == 2 and arg[0] == '-']
args = [arg for arg in argv if arg not in flags]
if (len(flags) > 2):
return ERROR_FLAG_NBR
for flag in flags:
if (flag == '-v'):
is_plot = True
elif (flag == '-d'):
is_detail = True
else:
return ERROR_FLAG
if (len(args) == 1):
return ERROR_NO_ARG
if (len(args) != 2):
return ERROR_ARG_NBR
return args[1]
def exit_usage(error):
'''
Print the usage & msg error then exit
'''
if (error == ERROR_NO_ARG):
print('Error: No argument!')
elif (error == ERROR_ARG_NBR):
print('Error: Wrong number of arguments!')
elif (error == ERROR_FLAG):
print('Error: Wrong used flag!')
elif (error == ERROR_FLAG_NBR):
print('Error: More than 2 flags are used')
elif (error == ERROR_SYNTAX_EQUAL):
print('Error: Equal sign doesn\'t exist in the eqution!')
elif (error == ERROR_SYNTAX_POLY_1):
print('Error: Syntax Error in the first polynome!')
elif (error == ERROR_SYNTAX_POLY_2):
print('Error: Syntax Error in the second polynome!')
else:
print('Syntax Error!')
print('Usage: ./computor [-v][-d] < equation >')
print(' equation: < polynome1 > = < polynome2 >')
print(' -v: Plot the result when it make sense')
print(' -d: Print Steps in details')
exit(1)
def comp_v1():
arg = get_flags_args(sys.argv)
# Check the arg returned that can contain the argument or an error number
if (type(arg) != str):
exit_usage(arg)
str_polys = arg.split('=')
if (len(str_polys) != 2):
exit_usage(ERROR_SYNTAX_EQUAL)
p1 = dict_polynome(str_polys[0])
p2 = dict_polynome(str_polys[1])
# Check if the polynomes are correct:
if (not p1):
exit_usage(ERROR_SYNTAX_POLY_1)
if (not p2):
exit_usage(ERROR_SYNTAX_POLY_2)
fill_polynome(p1, p2)
max_degree = get_max_degree()
dict_rebuild(max_degree)
print_result(max_degree)
comp_v1()