Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,47 @@
|
|||
-V ops = ‘-+/*^’
|
||||
|
||||
F postfix_to_infix(postfix)
|
||||
T Expression
|
||||
String op, ex
|
||||
prec = 3
|
||||
|
||||
F (String e1, e2 = ‘’, o = ‘’)
|
||||
I o == ‘’
|
||||
.ex = e1
|
||||
E
|
||||
.ex = e1‘ ’o‘ ’e2
|
||||
.op = o
|
||||
.prec = :ops.index(o) I/ 2
|
||||
|
||||
F String()
|
||||
R .ex
|
||||
|
||||
[Expression] expr
|
||||
|
||||
L(token) postfix.split(re:‘\s+’)
|
||||
V c = token[0]
|
||||
V? idx = :ops.find(c)
|
||||
I idx != N
|
||||
V r = expr.pop()
|
||||
V l = expr.pop()
|
||||
V opPrec = idx I/ 2
|
||||
|
||||
I l.prec < opPrec | (l.prec == opPrec & c == ‘^’)
|
||||
l.ex = ‘(’l.ex‘)’
|
||||
|
||||
I r.prec < opPrec | (r.prec == opPrec & c != ‘^’)
|
||||
r.ex = ‘(’r.ex‘)’
|
||||
expr.append(Expression(l.ex, r.ex, token))
|
||||
E
|
||||
expr.append(Expression(token))
|
||||
|
||||
print(token‘ -> ’expr)
|
||||
|
||||
assert(expr.len == 1)
|
||||
R expr[0].ex
|
||||
|
||||
L(e) [‘3 4 2 * 1 5 - 2 3 ^ ^ / +’,
|
||||
‘1 2 + 3 4 + ^ 5 6 + ^’]
|
||||
print(‘Postfix : ’e)
|
||||
print(‘Infix : ’postfix_to_infix(e))
|
||||
print()
|
||||
Loading…
Add table
Add a link
Reference in a new issue