Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Nth/Cowgol/nth.cowgol
Normal file
40
Task/Nth/Cowgol/nth.cowgol
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
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);
|
||||
Loading…
Add table
Add a link
Reference in a new issue