Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
86
Task/Four-is-magic/FreeBASIC/four-is-magic.basic
Normal file
86
Task/Four-is-magic/FreeBASIC/four-is-magic.basic
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#define floor(x) ((x*2.0-0.5) Shr 1)
|
||||
|
||||
Dim Shared veintes(1 To 20) 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 decenas(1 To 8) As String*7 => _
|
||||
{"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"}
|
||||
|
||||
Type myorder
|
||||
var1 As Double
|
||||
var2 As String*8
|
||||
End Type
|
||||
Dim Shared orders(1 To 4) As myorder => _
|
||||
{(10^12,"trillion"), (10^9,"billion"), (10^6,"million"), (10^3,"thousand")}
|
||||
|
||||
Function centenas(n As Integer) As String
|
||||
If n < 20 Then
|
||||
Return veintes((n Mod 20)+1)
|
||||
Elseif (n Mod 10) = 0 Then
|
||||
Return decenas((floor(n/10) Mod 10)-1)
|
||||
End If
|
||||
Return decenas((floor(n/10) Mod 10)-1) & "-" & veintes((n Mod 10)+1)
|
||||
End Function
|
||||
|
||||
Function miles(n As Integer) As String
|
||||
If n < 100 Then
|
||||
Return centenas(n)
|
||||
Elseif (n Mod 100) = 0 Then
|
||||
Return veintes((floor(n/100) Mod 20)+1) & " centenas"
|
||||
End If
|
||||
Return veintes((floor(n/100) Mod 20)+1) & " centenas " & centenas(n Mod 100)
|
||||
End Function
|
||||
|
||||
Function triplet(n As Integer) As String
|
||||
Dim As Integer order, high, low
|
||||
Dim As String nombre, res = ""
|
||||
For i As Integer = 1 To Ubound(orders)
|
||||
order = orders(i).var1
|
||||
nombre = orders(i).var2
|
||||
high = floor(n/order)
|
||||
low = (n Mod order)
|
||||
If high <> 0 Then res &= miles(high) & " " & nombre
|
||||
n = low
|
||||
If low = 0 Then Exit For : End If
|
||||
If Len(res) And high <> 0 Then res &= " "
|
||||
Next i
|
||||
If n <> 0 Or res="" Then
|
||||
res &= miles(floor(n))
|
||||
End If
|
||||
Return res
|
||||
End Function
|
||||
|
||||
Function deletrear(n As Integer) As String
|
||||
Dim As String res = ""
|
||||
If n < 0 Then
|
||||
res = "negative "
|
||||
n = -n
|
||||
End If
|
||||
res &= triplet(n)
|
||||
|
||||
Return res
|
||||
End Function
|
||||
|
||||
Function fourIsMagic(n As Integer) As String
|
||||
Dim As String s = deletrear(n)
|
||||
s = Mid(Ucase(s), 1, 1) & Mid(s, 2, Len(s))
|
||||
Dim As String t = s
|
||||
While n <> 4
|
||||
n = Len(s)
|
||||
s = deletrear(n)
|
||||
t &= " is " & s & ", " & s
|
||||
Wend
|
||||
t &= " is magic."
|
||||
Return t
|
||||
End Function
|
||||
|
||||
Dim As Longint tests(1 To 21) = _
|
||||
{-21, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 12, _
|
||||
34, 123, 456, 1024, 1234, 12345, 123456, 1010101}
|
||||
|
||||
For i As Integer = 1 To Ubound(tests)
|
||||
Print Using "#######: &"; tests(i); fourIsMagic(tests(i))
|
||||
Next i
|
||||
Sleep
|
||||
Loading…
Add table
Add a link
Reference in a new issue