RosettaCodeData/Task/Textonyms/FreeBASIC/textonyms.basic

79 lines
2.5 KiB
Plaintext

Type KeyValuePair
As String key
As String value
End Type
' Simulate a dictionary with an array
Dim Shared keyMap(0 To 7) As KeyValuePair
keyMap(0).key = "ABC" : keyMap(0).value = "2"
keyMap(1).key = "DEF" : keyMap(1).value = "3"
keyMap(2).key = "GHI" : keyMap(2).value = "4"
keyMap(3).key = "JKL" : keyMap(3).value = "5"
keyMap(4).key = "MNO" : keyMap(4).value = "6"
keyMap(5).key = "PQRS" : keyMap(5).value = "7"
keyMap(6).key = "TUV" : keyMap(6).value = "8"
keyMap(7).key = "WXYZ" : keyMap(7).value = "9"
Function GetKeyMapValue(char As String) As String
For i As Integer = Lbound(keyMap) To Ubound(keyMap)
If Instr(keyMap(i).key, Ucase(char)) > 0 Then Return keyMap(i).value
Next
Return ""
End Function
Function ArrayExists(arr() As String, value As String) As Boolean
For i As Integer = Lbound(arr) To Ubound(arr)
If arr(i) = value Then Return True
Next
Return False
End Function
Dim As Integer TotalWords = 0
Dim As Integer UniqueCombinations = 0
Dim As String uniqueWords(), moreThanOneWord()
Dim As String inputFile = "unixdict.txt"
Dim As Integer ff = Freefile()
Open inputFile For Input As #ff
If Err <> 0 Then Print "Error: Unable to open file '" & inputFile & "'": End 1
Dim As String linea, num, char, digit
Dim As Integer c, i
Do Until Eof(ff)
Line Input #ff, linea
If Len(linea) > 0 Then
num = ""
c = 0
For i = 1 To Len(linea)
char = Mid(linea, i, 1)
digit = GetKeyMapValue(char)
If digit <> "" Then
num &= digit
c += 1
End If
Next i
If c = Len(linea) Then
TotalWords += 1
If Ubound(uniqueWords) = -1 Orelse Not ArrayExists(uniqueWords(), num) Then
Redim Preserve uniqueWords(0 To Ubound(uniqueWords) + 1)
uniqueWords(Ubound(uniqueWords)) = num
UniqueCombinations += 1
Else
If Ubound(moreThanOneWord) = -1 Orelse Not ArrayExists(moreThanOneWord(), num) Then
Redim Preserve moreThanOneWord(0 To Ubound(moreThanOneWord) + 1)
moreThanOneWord(Ubound(moreThanOneWord)) = num
End If
End If
End If
End If
Loop
Close #ff
Print "There are " & TotalWords & " words in ""unixdict.txt"" which can be represented by the digit key mapping."
Print "They require " & UniqueCombinations & " digit combinations to represent them."
Print Ubound(moreThanOneWord) + 1 & " digit combinations represent Textonyms."
Sleep