2

Input:

> printf 1\n\2\n3\n
1
2
3

Desired output:

0
1
1

Typing printf 1\n\2\n3\n | dc -e '-p' the output is:

dc: stack empty
dc: stack empty
ncomputers
  • 1,524
  • 1
  • 11
  • 23

1 Answers1

2

You can pipe the entire set of commands directly to dc.

printf "1 2 3 - - p" | dc

If you'd like to subtract an arbitrary stack you can use a macro.

printf "%s [-z1<r]srz1<rp" "<insert numbers here>" | dc

So for example

printf "%s [-z1<r]srz1<rp" "100 5 2 1" | dc
96

The macro does the following:

  • [ Start macro
  • - Subtract top two numbers and push back to stack
  • z1<r Push stack size to stack and 1, then compare LESSTHAN, if true run r macro
  • ]sr End of macro, store in register r
  • z1<r Check if stack contains elements by testing 1 < Stack Size
  • p Print result.

  • `

jecxjo
  • 506