-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
54 lines (40 loc) · 1.61 KB
/
main.py
File metadata and controls
54 lines (40 loc) · 1.61 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
import re, fractions
import operation, expression, matrix
TOKEN_OP = r'\(|\)|' + '|'.join(re.escape(op.name) for op in operation.opDict.values())
TOKEN = re.compile(r'[\w\.]+|' + TOKEN_OP)
TOKEN_OP = re.compile(TOKEN_OP)
TOKEN_NUM = re.compile(r'[\d\.]+')
print("Введите выражение:")
s = [m.group() for m in TOKEN.finditer(input())]
print()
d = dict()
for t in s:
if TOKEN_OP.fullmatch(t) is None and t not in d:
if not TOKEN_NUM.fullmatch(t) is None:
d[t] = matrix.Matrix(t)
else:
width = None
elemList = []
while width is None:
print("Ввод " + t + ":")
while True:
try:
elemLine = list(map(fractions.Fraction, input().split()))
except ValueError:
print("Ошибка ввода, введите строку заново:")
continue
if len(elemLine) == 0: break
if len(elemLine) == width or width is None:
width = len(elemLine)
elemList.extend(elemLine)
else:
print("Неверное количесство элементов, введите строку заново:")
m = matrix.Matrix(0, len(elemList) // width, width)
for i in range(len(m)): m[i] = elemList[i]
d[t] = m
try:
result = expression.process(s, d)
print("Результат:")
print(result)
except matrix.MatrixException as e:
print("Ошибка:", e)