Category:Dc: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Expand the quick reference to cover OpenBSD dc.)
Line 4:
}}dc is the unix desktop calculator. It uses a reverse polish notation and is turing complete.
 
== Quick reference ==
Commands in POSIX[[AT&T dc]]
number : push a number
number : push a number (0-9A-F digits, _ negative, . radix point)
+ - / * % ^ : arithmetic operations: add sub div mul mod exp
sx Sx[...] : pop the stack and save or push it to registera xstring
+ - / * / % ^ : arithmetic operations: add sub div mul div mod exppow
lx Lx : copy | pop the register x to main stack.
av : asciifysquare root
pc : print top of theclear stack
d : duplicate top of the stack
p : print top of the stack
P : pop and print String
f : show stack
qp : exitprint (dropvalue executionwith stacka bynewline, 2)leave it on the stack
QP : dropprint executionstring, stackor byprint topnumber ofas theASCII stack numberstring
x sx : executestore dcto commandsregister on top of the stackx
X lx : replace top of stack byload itsfrom scaleregister factor.x
[...]Sx : stringpush to register notationx
<xLx >x =x : compare 2 elements at top of the stack, and: executepop register x
va b :x : store x[b] := a (x squareis rootregister)
a ;rx : retrieve rx[b]
<x >x =x : compare top 2 values: if true, then execute register x
!>x !<x !=x : (example: 5 2 <A executes register A if 2 < 5)
! : shell command
cq : clearquit, or break 2 execution levels
n Q : break n execution levels
i : set input radix from top of stack
I : pushget current input base, onpush theit top of theon stack.
o : set output radix from top of stack
O : pushget current output base, onpush theit top of theon stack.
k : set scale factor
K : get current scale factor
zx : getexecute thea currentstring stackof level.dc commands
ZX : getcount thedecimal characterdigits lengthafter ofdecimal element at top of stackpoint
?z : readcount inputelements andon execute.the stack
Z : count all decimal digits in number, or characters in string
P? : popread and printexecute Stringone line of input
Y : debug information
a b :r : store r[b] = a (r is register)
a ;r : retrieve r[b]
 
Extensions in both [[GNU dc]] and [[OpenBSD dc]]
GNU Extensions
na : print value without a newASCII linecharacter
n : print value without a newline
~ : divmod: division and remainder
r : reverse (swap) the top two elements
~# : arithmetic - divmodcomment
 
Extension in [[GNU dc]]
n : print value without a new line
~ : arithmetic - divmod
| : arithmetic - modular exponentiation
 
r : swap the top two elements
Extensions in [[OpenBSD dc]]
a : asciify
n J : break n execution levels, then jump to next M
!>r !<r !=r : execute contents of register x based on comparison
XM : pushesmark for the number of fractionalJ digitscommand
#a N : boolean not: commentpush 1 if a == 0, else push 0
lxR Lx : copy | : pop theand registerdiscard xtop toof mainthe stack.
<xey >xey : comparisons with an else branch: if true,
=xey !<xey : then execute register x
!>xey !=xey : else execute register y
a b G : equality: push 1 if b == a, else push 0
a b ( : less than: push 1 if b < a, else push 0

Revision as of 02:41, 8 March 2011

Language
Dc
This programming language may be used to instruct a computer to perform a task.
Execution method: Interpreted
Type checking: Dynamic
See Also:


Listed below are all of the tasks on Rosetta Code which have been solved using Dc.

dc is the unix desktop calculator. It uses a reverse polish notation and is turing complete.

Quick reference

Commands in AT&T dc

number      : push a number (0-9A-F digits, _ negative, . radix point)
[...]       : push a string
+ - * / % ^ : arithmetic operations: add sub mul div mod pow
v           : square root
c           : clear stack
d           : duplicate top of the stack
f           : show stack
p           : print value with a newline, leave it on the stack
P           : print string, or print number as ASCII string
sx          : store to register x
lx          : load from register x
Sx          : push to register x
Lx          : pop register x
a b :x      : store x[b] = a (x is register)
a ;x        : retrieve x[b]
<x >x =x    : compare top 2 values: if true, then execute register x
!>x !<x !=x :   (example: 5 2 <A executes register A if 2 < 5)
!           : shell command
q           : quit, or break 2 execution levels
n Q         : break n execution levels
i           : set input radix from top of stack
I           : get current input base, push it on stack
o           : set output radix from top of stack
O           : get current output base, push it on stack
k           : set scale factor
K           : get current scale factor
x           : execute a string of dc commands
X           : count decimal digits after decimal point
z           : count elements on the stack
Z           : count all decimal digits in number, or characters in string
?           : read and execute one line of input
Y           : debug information

Extensions in both GNU dc and OpenBSD dc

a           : print ASCII character
n           : print value without a newline
~           : divmod: division and remainder
r           : reverse (swap) the top two elements
#           : comment

Extension in GNU dc

|           : arithmetic - modular exponentiation

Extensions in OpenBSD dc

n J         : break n execution levels, then jump to next M
M           : mark for the J command
a N         : boolean not: push 1 if a == 0, else push 0
R           : pop and discard top of the stack
<xey >xey   : comparisons with an else branch: if true,
=xey !<xey  :   then execute register x
!>xey !=xey :   else execute register y
a b G       : equality: push 1 if b == a, else push 0
a b (       : less than: push 1 if b < a, else push 0

Subcategories

This category has the following 3 subcategories, out of 3 total.

Pages in category "Dc"

The following 90 pages are in this category, out of 90 total.