63 lines
1,023 B
Plaintext
63 lines
1,023 B
Plaintext
|
-- decin/decout - Decimal i/o
|
||
|
|
||
|
proc decin(chan in, var d, c)=
|
||
|
-- Reads a decimal number from input into d. c is used as the first input
|
||
|
-- character, and it contains the character just after the decimal number
|
||
|
-- when decin exits.
|
||
|
|
||
|
var neg:
|
||
|
seq
|
||
|
while (c='*s') or (c='*t') or (c='*n')
|
||
|
in ? c
|
||
|
if
|
||
|
(c='-') or (c='+')
|
||
|
seq
|
||
|
neg:=(c='-')
|
||
|
in ? c
|
||
|
true
|
||
|
neg:=false
|
||
|
d:=0
|
||
|
while ('0'<=c) and (c<='9')
|
||
|
seq
|
||
|
d:=(d*10)+(c-'0')
|
||
|
in ? c
|
||
|
if
|
||
|
neg
|
||
|
d:= -d
|
||
|
:
|
||
|
proc decout(chan out, value d, w)=
|
||
|
-- Sends d to channel out in decimal on a field of w spaces. It is just like
|
||
|
-- fprintf(out, "%*d", w, d);
|
||
|
|
||
|
var dec[BYTE 12], di, dd, d0, neg:
|
||
|
seq
|
||
|
di:=0
|
||
|
if
|
||
|
d<0
|
||
|
seq
|
||
|
neg:=true
|
||
|
dd:= -d
|
||
|
d>=0
|
||
|
seq
|
||
|
neg:=false
|
||
|
dd:=d
|
||
|
d0:=1
|
||
|
while d0<>0
|
||
|
seq
|
||
|
dec[BYTE di]:=(dd\10)+'0'
|
||
|
di:=di+1
|
||
|
dd:=dd/10
|
||
|
d0:=dd
|
||
|
if
|
||
|
neg
|
||
|
seq
|
||
|
dec[BYTE di]:='-'
|
||
|
di:=di+1
|
||
|
seq i=[0 for w-di]
|
||
|
out ! '*s'
|
||
|
while di<>0
|
||
|
seq
|
||
|
di:=di-1
|
||
|
out ! dec[BYTE di]
|
||
|
:
|