RosettaCodeData/Task/Nth/BCPL/nth.bcpl

41 lines
920 B
Plaintext

get "libhdr"
// Generate ASCII string of number n with ordinal suffix
// The string is stored at v.
let nth(n, v) = valof
$( let sfx = "thstndrd"
let c, s = 0, ?
// Generate digits
$( c := c + 1
v%c := n rem 10 + '0'
n := n / 10
$) repeatuntil n = 0
// Put digits in correct order
for i = 1 to c/2 do
$( let d = v%i
v%i := v%(c+1-i)
v%(c+1-i) := d
$)
// The length of the string is the amount of digits + 2
v%0 := c+2;
// Figure out the proper suffix from the last two digits
test v%(c-1)='1' | v%c>'3'
then s := 0
else s := 2*(v%c - '0')
v%(c+1) := sfx%(s+1)
v%(c+2) := sfx%(s+2)
resultis v
$)
let start() be
$( let buf = vec 10
for i = 0 to 25 do writef("%S*N", nth(i, buf))
for i = 250 to 265 do writef("%S*N", nth(i, buf))
for i = 1000 to 1025 do writef("%S*N", nth(i, buf))
$)