40 lines
1 KiB
Text
40 lines
1 KiB
Text
include "cowgol.coh";
|
|
include "strings.coh";
|
|
|
|
# Given an int32, return decimal string and add an ordinal suffix
|
|
sub ordsfx(n: int32, buf: [uint8]): (out: [uint8]) is
|
|
var sfx: [uint8][] := {"th", "st", "nd", "rd"};
|
|
out := buf;
|
|
buf := IToA(n, 10, buf); # IToA is included in standard library
|
|
|
|
# Since we now already have the digits, we can make the decision
|
|
# without doing any more work.
|
|
if (n > 10 and [@prev @prev buf] == '1')
|
|
or ([@prev buf] > '3') then
|
|
CopyString(sfx[0], buf);
|
|
else
|
|
CopyString(sfx[[@prev buf]-'0'], buf);
|
|
end if;
|
|
end sub;
|
|
|
|
# Print suffixed numerals from start to end inclusive
|
|
sub test(start: int32, end_: int32) is
|
|
var buf: uint8[16]; # buffer
|
|
|
|
var n: uint8 := 10;
|
|
while start <= end_ loop
|
|
print(ordsfx(start, &buf[0]));
|
|
print_char(' ');
|
|
start := start + 1;
|
|
n := n - 1;
|
|
if n == 0 then
|
|
print_nl();
|
|
n := 10;
|
|
end if;
|
|
end loop;
|
|
print_nl();
|
|
end sub;
|
|
|
|
test(0, 25);
|
|
test(250,265);
|
|
test(1000,1025);
|