Skip to main content
better formatting
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30

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/.

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

###Run:

sed -f simple_calculator.sed <<< "*///*-*+-+"

###Output: -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/.

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

Run:

sed -f simple_calculator.sed <<< "*///*-*+-+"

Output:

-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/.

deleted 3 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30

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

###Run:

sed -f simple_calculator.sed <<< "\*"*///\**-\*+*+-+"

###Output: -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/.

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

###Run:

sed -f simple_calculator.sed <<< "\*///\*-\*+-+"

###Output: -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/.

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

###Run:

sed -f simple_calculator.sed <<< "*///*-*+-+"

###Output: -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/.

Spelling fix, and markdown tweaks
Source Link
Toby Speight
  • 6.5k
  • 1
  • 24
  • 41

GNU sed, 65 59 57 bytes

Edit: 2 bytes shorter thanks to Toby SpeightToby 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: sed -f simple_calculator.sed <<< "*///*-*+-+" ###Run:

sed -f simple_calculator.sed <<< "\*///\*-\*+-+"

Output:
###Output: -1-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 registryregister @ is called. Conversion example: + - * / --> 1+ 1- 2* d0>@2/.

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

Run: sed -f simple_calculator.sed <<< "*///*-*+-+"

Output:
-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 registry @ is called. Conversion example: + - * / --> 1+ 1- 2* d0>@2/.

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

###Run:

sed -f simple_calculator.sed <<< "\*///\*-\*+-+"

###Output: -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/.

added 92 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading
added 85 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading
added 45 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading
deleted 4 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading
deleted 2 characters in body
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading
Source Link
seshoumara
  • 3.7k
  • 1
  • 13
  • 30
Loading