Spelling of ordinal numbers: Difference between revisions
Content added Content deleted
(Add C# implementation) |
(Added FreeBASIC) |
||
Line 762: | Line 762: | ||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<syntaxhighlight lang="vbnet">Dim Shared small(19) As String*9 => { _ |
|||
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", _ |
|||
"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", _ |
|||
"sixteen", "seventeen", "eighteen", "nineteen" } |
|||
Dim Shared tens(9) As String*7 => { "", "", _ |
|||
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety" } |
|||
Dim Shared illions(6) As String*12 => {"", _ |
|||
" thousand", " million", " billion"," trillion", " quadrillion", " quintillion" } |
|||
Dim Shared irregularOrdinals(7, 1) As String*7 = { _ |
|||
{"one", "first"}, {"two", "second"}, {"three", "third"}, {"five", "fifth"}, _ |
|||
{"eight", "eighth"}, {"nine", "ninth"}, {"twelve", "twelfth"} } |
|||
Function spell(n As Integer) As String |
|||
Dim As String sx, ix, t = "" |
|||
Dim As Integer s, i, p |
|||
If n < 0 Then |
|||
t = "negative " |
|||
n = -n |
|||
End If |
|||
If n < 20 Then |
|||
t &= small(n) |
|||
Elseif n < 100 Then |
|||
t &= tens(n \ 10) |
|||
s = n Mod 10 |
|||
If s > 0 Then t &= "-" & small(s) |
|||
Elseif n < 1000 Then |
|||
t &= small(n \ 100) & " hundred" |
|||
s = n Mod 100 |
|||
If s > 0 Then t &= " " & spell(s) |
|||
Else |
|||
sx = "" |
|||
i = 0 |
|||
While n > 0 |
|||
p = n Mod 1000 |
|||
n \= 1000 |
|||
If p > 0 Then |
|||
ix = spell(p) & illions(i) |
|||
If sx <> "" Then ix &= " " & sx |
|||
sx = ix |
|||
End If |
|||
i += 1 |
|||
Wend |
|||
t &= sx |
|||
End If |
|||
Return t |
|||
End Function |
|||
Function sayOrdinal(n As Integer) As String |
|||
Dim As String s = spell(n) |
|||
Dim As String lastWord = "" |
|||
Dim As Integer j, i = Len(s) |
|||
While i > 0 And Mid(s, i, 1) <> " " And Mid(s, i, 1) <> "-" |
|||
lastWord = Mid(s, i, 1) + lastWord |
|||
i -= 1 |
|||
Wend |
|||
For j = 0 To Ubound(irregularOrdinals, 1) |
|||
If irregularOrdinals(j, 0) = lastWord Then |
|||
Return Left(s, i) + irregularOrdinals(j, 1) |
|||
End If |
|||
Next j |
|||
If Right(s, 1) = "y" Then |
|||
Return Left(s, Len(s) - 1) + "ieth" |
|||
Else |
|||
Return s + "th" |
|||
End If |
|||
End Function |
|||
Dim As Integer t(0 To ...) = { 1, 2, 3, 4, 5, 11, 65, 100, 101, 272, _ |
|||
23456, 8007006005004003, 123, 00123.0, 1.23E2 } |
|||
For n As Integer = 0 To Ubound(t) |
|||
Print sayOrdinal(t(n)) |
|||
Next n |
|||
Sleep</syntaxhighlight> |
|||
{{out}} |
|||
<pre>first |
|||
second |
|||
third |
|||
fourth |
|||
fifth |
|||
eleventh |
|||
sixty-fifth |
|||
one hundredth |
|||
one hundred first |
|||
two hundred seventy-second |
|||
twenty-three thousand four hundred fifty-sixth |
|||
eight quadrillion seven trillion six billion five million four thousand third |
|||
one hundred twenty-third |
|||
one hundred twenty-third |
|||
one hundred twenty-third</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |