# 从前缀表示法转换为前缀表示法

12

``````a ^ (2 / 3) * 9 * 3 - 4 * 6
``````

``````(- (* (* (^ a (/ 2 3)) 9) 3) (* 4 6))
``````

1
+和-是相同的优先级，还是+高于-？即，是`3+4-5+6 = (((3+4)-5)+6)`还是`((3+4)-(5+6))`

PhiNotPi 2012年

Ali1S232

@KeithRandall `*`和amd `/`具有相同的优先级。`+``-`

@Gajet不，不是。

13

## 红宝石1.9-134

``````%w[** / * + -].map{|o|String.send(:define_method,o){|n|"(#{o=='**'??^:o} #{self} #{n})"}}
puts eval gets.gsub(/\w/,'?\0').gsub ?^,'**'
``````

``````\$ echo 'a ^ (2 / 3) * 9 * 3 - 4 * 6' | ruby sol.rb
(- (* (* (^ a (/ 2 3)) 9) 3) (* 4 6))
``````

3

## Python，222个字符

``````class A:
def __init__(s,x):s.v=x
import re
print eval(re.sub('(\\w)','A("\\1")',raw_input().replace('^','**'))).v
``````

2

## Perl 6（146 | 150）

``````sub infix:«+»   (\$a,\$b) { "(+ \$a \$b)" }
sub infix:«-»   (\$a,\$b) { "(- \$a \$b)" }
sub infix:«*»   (\$a,\$b) { "(* \$a \$b)" }
sub infix:['/'] (\$a,\$b) { "(/ \$a \$b)" } # stupid highlighter
sub infix:«**»  (\$a,\$b) { "(^ \$a \$b)" }

# currently there seems to be a bug that
# prevents this from modifying the parser correctly
# probably because there is already a different operator with this name
# which has nothing to do with exponentiation
my &infix:«^» := &[**];

say 'a' ** (2 / 3) * 9 * 3 - 4 * 6;
# (- (* (* (^ a (/ 2 3)) 9) 3) (* 4 6))␤``````

``````sub infix:<+>{"(+ \$^a \$^b)"}␤  #   29
sub infix:<->{"(- \$^a \$^b)"}␤  # + 29
sub infix:<*>{"(* \$^a \$^b)"}␤  # + 29
sub infix:<**>{"(^ \$^a \$^b)"}␤ # + 30
sub infix:</>{"(/ \$^a \$^b)"}␤  # + 29``````

146个字节，尽管在Perl 6中对字素进行计数更有意义。

0

## Unix TMG，189字节

``````p:ignore(<< >>)parse(e);e:q(t,a);t:q(x,m);x:q(r,h);q:proc(x,y)x k:y/d x={<(>2 3 1<)>}b\k;r:o(!<<+-*/^()>>)|<(>e<)>;a:o(<<+->>);m:o(<<*/>>);h:o(<<^>>);o:proc(n)smark any(n)scopy;d:;b:bundle;
``````

``````prog:  ignore(<< >>) parse(expr);