--Basic Information Engineer Examination Herbst 2018 Nachmittag Frage 8
practice.py
Expression = []
formula = input("Bitte geben Sie die Formel ein:")
for j in range(len(formula)):
Expression.append(formula[j])
Value = []
Operator = []
Priority = []
def calculation():
OpCnt = 0
Value.append(0)
nest = 0
#Analyseteil
for i in range(len(Expression)):
chr = Expression[i]
if chr >= '0' and chr <= '9':
Value[OpCnt] = 10 * Value[OpCnt] + int(chr)
if chr == '+' or chr == '-' or chr == '*' or chr == '/':
Operator.append(chr)
if chr == '+' or chr == '-':
Priority.append(nest + 1)
else:
Priority.append(nest + 2)
OpCnt = OpCnt + 1
Value.append(0)
if chr == '(':
nest = nest + 10
if chr == ')':
nest = nest - 10
#Berechnungsteil
while OpCnt > 0:
ip = 0
i = 1
while i < OpCnt:
if Priority[ip] < Priority[i]:
ip = i
i = i + 1
chr = Operator[ip]
if chr == '+':
Value[ip] = Value[ip] + Value[ip + 1]
if chr == '-':
Value[ip] = Value[ip] - Value[ip + 1]
if chr == '*':
Value[ip] = Value[ip] * Value[ip + 1]
if chr == '/':
Value[ip] = Value[ip] / Value[ip + 1]
i = ip + 1
while i < OpCnt:
Value[i] = Value[i + 1]
Operator[i - 1] = Operator[i]
Priority[i - 1] = Priority[i]
i = i + 1
OpCnt = OpCnt - 1
return Value[0]
practice.py
Expression = []
formula = input("Bitte geben Sie die Formel ein:")
for j in range(len(formula)):
Expression.append(formula[j])
print(Expression)
Konsole
['2', '*', '(', '3', '4', '-', '(', '5', '+', '6', '7', ')', '/', '8', ')']
practice.py
Value = []
Operator = []
Priority = []
practice.py
def calculation():
OpCnt = 0
Value.append(0)
nest = 0
#Analyseverarbeitungsteil
for i in range(len(Expression)):
chr = Expression[i]
if chr >= '0' and chr <= '9':
Value[OpCnt] = 10 * Value[OpCnt] + int(chr)
if chr == '+' or chr == '-' or chr == '*' or chr == '/':
Operator.append(chr)
if chr == '+' or chr == '-':
Priority.append(nest + 1)
else:
Priority.append(nest + 2)
OpCnt = OpCnt + 1
Value.append(0)
if chr == '(':
nest = nest + 10
if chr == ')':
nest = nest - 10
#Berechnungsverarbeitungsteil
while OpCnt > 0:
ip = 0
i = 1
while i < OpCnt:
if Priority[ip] < Priority[i]:
ip = i
i = i + 1
chr = Operator[ip]
if chr == '+':
Value[ip] = Value[ip] + Value[ip + 1]
if chr == '-':
Value[ip] = Value[ip] - Value[ip + 1]
if chr == '*':
Value[ip] = Value[ip] * Value[ip + 1]
if chr == '/':
Value[ip] = Value[ip] / Value[ip + 1]
i = ip + 1
while i < OpCnt:
Value[i] = Value[i + 1]
Operator[i - 1] = Operator[i]
Priority[i - 1] = Priority[i]
i = i + 1
OpCnt = OpCnt - 1
return Value[0]
Value = [2, 34, 5, 67, 8]
Operator = ['*', '-', '+', '/']
Priority =[2, 11, 21, 12]
--Berechnen Sie die Reihenfolge anhand der Werte in der Prioritätsliste
――Es hat Spaß gemacht ^^
Recommended Posts