GNU sed, 65 59 57 bytes
GNU sed, 65 59 57 bytes
Edit: 2 bytes shorter thanks to Toby Speight's comments
s/[+-]/1&/g
s/*/2&/g
s:/:d0>@2&:g
s/.*/dc -e"0[1-]s@&p"/e
s/[+-]/1&/g
s/*/2&/g
s:/:d0>@2&:g
s/.*/dc -e"0[1-]s@&p"/e
###Run:Run:
sed -f simple_calculator.sed <<< "*///*-*+-+"
###Output: -1Output:
-1
The sed
script prepares the input for the dc
shell call at the end, the latter accepting the input in Reverse Polish notation. On division, if the number is negative (d0>
), the [1-]
decrement command stored in register @
is called. Conversion example: + - * /
--> 1+ 1- 2* d0>@2/
.