当前位置: 高中信息技术 / 综合题
  • 1. (2020高二上·上海开学考) 用VB编写的英文单词字频统计程序如图所示,在文本框Text1中输入文章,单击“统计”按钮Command1,在列表框List1输出单词与频次,在标签Label2中输出频次最高的单词,如频次最高的单词有多个,则输出时用逗号分隔。算法如下:

    1)将文本框Text1中的文章保存到字符串变量s中。

    2)从左往右扫描字符串s,用字符串word存储文章中依次出现的单词,将新出现的单词自动加入单词队列dic(i)中,单词出现的频次记录到对应的num(i)中。

    如下方法扫描:

    ①从左往右扫描,当扫描到第i个字符时,如果是字母,继续扫描,否则,跳转到②,直至扫描结束;

    ②将b到i-1组成一个单词word,查询单词字典dic判断是否存在,如果不存在,将新单词插入单词字字典尾部dic(k),并且记录单词频次num(k)为1,如果dic字典中存在单词word,那么该单词对应频次增加1,再跳转到①。

    1. (1) 在列表框List1中依次输出单词与频次,在标签Label2中输出频次最高的单词。实现上述功能的VB程序如下,请在划线处填入合适的代码。

      Private Sub Command1_Click()

          Dim dic(1 To 10000) As String    '存储自建单词字典

          Dim num(1 To 10000) As Integer    '存储单词出现的频率

          Dim word As String, c As String, s As String, slen As Integer

          Dim i As Integer, j As Integer, di As Integer, k As Integer

          Dim nummax As Integer     '记录最高的单词频率

          Dim result As String     '存储出现频率最高的单词串

          s = Text1.Text: slen = Len(s)

          j = 0    '存储每个单词的长度

          k = 1    'k-1 为当前单词字典长度

          nummax = 0

          For i = 1 To 10000    '初始化单词字典频次

              num(i) = 0

          Next i

          i = 1

          Do While i <= slen

              

              If c >= "a" And c <= "z" Or c >= "A" And c <= "Z" Then j = j + 1

              Else

                  If j <> 0 Then

                      word = LCase(Mid(s, i - j, j))    'LCase()函数功能:单词统一为小写

                      di = 1

                  Do While word <> dic(di) And di < k

                      di = di + 1

                  Loop

                  If di = k Then    '单词字典插入新单词

                      dic(k) = word

                      num(k) = 1

                       

                  Else

                       

                  End If

                  '单词在单词字典中已存在,对应频次加1

                  If num(di) > nummax Then nummax = num(di)

                  j = 0

                  End If

              End If

              i = i + 1

          Loop

          result = ""

          For j = 1 To k - 1

              List1.AddItem dic(j) + "" + Str(num(j))

              If  Then

                  If result = "" Then

                      result = result + dic(j) Else

                      result = result + "," + dic(j)    '如出现频率最高的单词有多个,则用逗号分隔

                  End If

              End If

          Next j

      Label2.Caption = "出现频率最高的单词是:" + result

      End Sub

    2. (2) 根据本题算法如图所示,若在Text1中将文章最后一行“so you must know how to use it.” 改为“so you must know how to use it3.”,单词统计结果(选填:会/不会)产生变化。

微信扫码预览、分享更方便