Private Sub Command1_Click()
’ 读取英文单词和对应中文的字典
Dim fileName As String
Dim fileNumber As Integer
Dim enToCnDict As Object
Set enToCnDict = CreateObject("Scripting.Dictionary")
fileName = "C:\Users\28261\Desktop\翻译程序源代码 - 副本\英汉翻译词典程序源代码\英汉词典zq.txt"
fileNumber = FreeFile
' 打开文本文件以供读取
Open fileName For Input As #fileNumber
' 逐行读取文本文件
Do Until EOF(fileNumber)
Dim line As String
Dim enWord As String
Dim cnWord As String
Line Input #fileNumber, line
' 按逗号分隔英文单词和中文
enWord = Split(line, ",")(0)
cnWord = Split(line, ",")(1)
' 添加到字典
enToCnDict(enWord) = cnWord
Loop
' 关闭文本文件
Close #fileNumber
' 初始化结果字符串
Dim resultStr As String
resultStr = ""
' 遍历输入的英文单词,按照字典中的映射,将每个英文单词翻译为对应的中文,并将翻译结果拼接到结果字符串中
Dim enWords() As String
enWords = Split(Text1.Text, " ")
Dim i As Integer
For i = LBound(enWords) To UBound(enWords)
enWord = enWords(i)
If enToCnDict.Exists(enWord) Then
cnWord = enToCnDict(enWord)
resultStr = resultStr & cnWord & " "
Else
resultStr = resultStr & "unknown" & " "
End If
' 添加分隔符
If i < UBound(enWords) Then
resultStr = resultStr & ""
End If
Next i
' 显示翻译结果
Text2.Text = resultStr
End Sub