-
Notifications
You must be signed in to change notification settings - Fork 1
/
kalkulator.py
125 lines (87 loc) · 2.79 KB
/
kalkulator.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
import re
regnestykke = "10/5*2+5-3"
operators = "[+-/*]"
splited = re.split(operators, regnestykke)
for i, ele in enumerate(splited):
splited[i] = float(ele)
print(splited)
operatorlist = re.findall(operators, regnestykke)
print(operatorlist)
charrekk = ["*", "/", "+", "-"]
gangeind = ""
deleind = ""
while (charrekk[0] in operatorlist) or (charrekk[1] in operatorlist):
if (charrekk[0] in operatorlist):
gangeind = operatorlist.index("*")
else:
gangeind = ""
if (charrekk[1] in operatorlist):
deleind = operatorlist.index("/")
else:
deleind=""
if (gangeind != "") and (deleind != ""):
if gangeind > deleind:
splited[deleind] = splited[deleind] / splited[deleind + 1]
splited.pop(deleind + 1)
operatorlist.remove("/")
else:
splited[gangeind] = splited[gangeind] * splited[gangeind + 1]
splited.pop(gangeind + 1)
operatorlist.remove("*")
elif gangeind != "":
splited[gangeind] = splited[gangeind] * splited[gangeind + 1]
splited.pop(gangeind + 1)
operatorlist.remove("*")
else:
splited[deleind] = splited[deleind] / splited[deleind + 1]
splited.pop(deleind + 1)
operatorlist.remove("/")
plussind = ""
deleind = ""
while (charrekk[2] in operatorlist) or (charrekk[3] in operatorlist):
if (charrekk[2] in operatorlist):
plussind = operatorlist.index("+")
else:
plussind = ""
if (charrekk[3] in operatorlist):
minusind = operatorlist.index("-")
else:
minusind = ""
if (plussind != "") and (minusind != ""):
if plussind > minusind:
splited[minusind] = splited[minusind] - splited[minusind + 1]
splited.pop(minusind + 1)
operatorlist.remove("-")
else:
splited[plussind] = splited[plussind] + splited[plussind + 1]
splited.pop(plussind + 1)
operatorlist.remove("+")
elif plussind != "":
splited[plussind] = splited[plussind] + splited[plussind + 1]
splited.pop(plussind + 1)
operatorlist.remove("+")
else:
splited[minusind] = splited[minusind] - splited[minusind + 1]
splited.pop(minusind + 1)
operatorlist.remove("-")
print(operatorlist,splited)
print(f' Tallet er {splited[0]}')
"""
for char of charrekk
while charrekk[0] in operatorlist:
ind = operatorlist.index("*")
i
"""
"""
character = ["+", "-", "*", "/"]
foundChar = []
for cha in character:
try:
regnestykke.index(cha)
except ValueError:
continue
else:
foundChar.append(cha)
splregn = ""
if * in
"""