RosettaCodeData/Task/Intersecting-number-wheels/FreeBASIC/intersecting-number-wheels....

104 lines
2.4 KiB
Plaintext

#define isNumeric(ch) (ch >= "0" And ch <= "9")
Type Rueda
nombre As String * 1 ' Fixed-length string for better performance
valor As String
index As Integer
End Type
Function Girar(Byref wheel As Rueda, dato() As Rueda) As String
wheel.index = (wheel.index + 1) Mod Len(wheel.valor)
Dim As String c = Mid(wheel.valor, wheel.index + 1, 1)
If IsNumeric(c) Then Return c
For i As Integer = 0 To Ubound(dato)
If dato(i).nombre = c Then Return Girar(dato(i), dato())
Next
Return ""
End Function
Function GirarRuedas(wheels() As Rueda) As String Static
Static As String result
Static As Integer cnt, maxCnt = 20
Static dato() As Rueda
result = ""
Redim dato(Ubound(wheels))
For i As Integer = 0 To Ubound(wheels)
dato(i) = wheels(i)
dato(i).index = -1 'Initialize to -1 so first increment gives 0
Next i
For cnt = 0 To maxCnt - 1
result &= Girar(dato(0), dato())
Next
Return result
End Function
Sub Mostrar(Byref secuencia As String)
For i As Integer = 1 To Len(secuencia)
Print Mid(secuencia, i, 1);
If i < Len(secuencia) Then Print " ";
Next i
Print "..."
End Sub
' Test cases initialization helper
Sub InitWheel(Byref w As Rueda, n As String, v As String)
w.nombre = n
w.valor = v
w.index = 0
End Sub
' Main program
Print "Intersecting Number Wheel group:"
' First test case
Dim wheels1(0) As Rueda
InitWheel(wheels1(0), "A", "123")
Print " A: [1 2 3]"
Print " Generates:"
Print " ";
Mostrar GirarRuedas(wheels1())
Print !"\nIntersecting Number Wheel group:"
' Second test case
Dim wheels2(1) As Rueda
InitWheel(wheels2(0), "A", "1B2")
InitWheel(wheels2(1), "B", "34")
Print " A: [1 B 2]"
Print " B: [3 4]"
Print " Generates:"
Print " ";
Mostrar GirarRuedas(wheels2())
Print !"\nIntersecting Number Wheel group:"
' Third test case
Dim wheels3(1) As Rueda
InitWheel(wheels3(0), "A", "1DD")
InitWheel(wheels3(1), "D", "678")
Print " A: [1 D D]"
Print " D: [6 7 8]"
Print " Generates:"
Print " ";
Mostrar GirarRuedas(wheels3())
Print !"\nIntersecting Number Wheel group:"
' Fourth test case
Dim wheels4(2) As Rueda
InitWheel(wheels4(0), "A", "1BC")
InitWheel(wheels4(1), "B", "34")
InitWheel(wheels4(2), "C", "5B")
Print " A: [1 B C]"
Print " B: [3 4]"
Print " C: [5 B]"
Print " Generates:"
Print " ";
Mostrar GirarRuedas(wheels4())
Sleep