当前位置:文档之家› 计算机二级 常考vb程序操作题

计算机二级 常考vb程序操作题

Private Sub Command1_Click()
'Text2.Text = Mid(Text1.Text, Text1.SelStart + 1, Text1.SelLength)
Text2.Text = Text1.SelText
End Sub

Private Sub Command2_Click()
'Text1.Text = Left(Text1.Text, Text1.SelStart) & Right(Text1.Text, Len(Text1.Text) - Text1.SelStart - Text1.SelLength)
Text1.SelText = ""
End Sub













Dim j As Integer
Dim sum As Integer
Dim max As Integer
Dim k As Integer
Dim strarr() As String
strarr = Split(s, "")
For j = 0 To UBound(strarr) - LBound(strarr)
If Trim(strarr(j)) <> "" Then
sum = sum + Len(Trim(strarr(j)))
If max < Len(Trim(strarr(j))) Then
max = Len(Trim(strarr(j)))
End If
Next j
Text1.Text = Round(sum / j)
Text2.Text = max

End Sub




水仙花数
Dim max As Integer, min As Integer
Dim i As Integer
max = a(1)
min = a(1)
For i = 1 To 100
If isnarc(a(i)) Then
If a(i) > max Then
max = a(i)
End If
If a(i) < min Then
min = a(i)
End If
End If
Next i
Text1.Text = max
Text2.Text = min




完全平方数
Private Sub Command2_Click()
Dim i As Integer, j As Integer, num As Integer, max As Integer
num = 0
max = 0
For i = 1 To 100
For j = 1 To Int(Sqr(a(i)))
If a(i) = j * j Then
num = num + 1
If max < a(i) Then max = a(i)
End If
Next j
Next i
Text1.Text = max
Text2.Text = num
End Sub


0~10 随机数 出现次数最多的次数

Private Sub Cmd1_Click()
Text1.Text = "": Text2.Text = "": Text3.Text = ""
For i = 1 To 20
a(i) = Fix(Rnd * 9 + 1)
b(i) = 1
Text1.Text = Text1.Text + Str(a(i)) + Space(2)
Next i
End Sub

Private Sub Cmd2_Click()
fmax = 0
For i = 1 To 20
For j = 1 To i - 1
If a(i) = a(j) Then
b(i) = b(i) + 1
End If
Next j
If b(i) > fmax Then fmax = b(i)
Next i
For i = 1 To 20
If b(i) = fmax Then
Text2.Text = Text2.Text + Str(a(i)) + Space(2)
End If
Next i
Text3.Text = fmax
End Sub

Private Sub Cmd3_Click()
End
End Sub






list1 》list2

修改后的代码如下:
Private Sub List1_DblClick()
Call MoveItem(List1, List2)
'调用MoveItem函数
End Sub
Private Sub Command1_Click()
Call MoveAll
End Sub
Public Sub MoveItem(L1 As ListBox, L2 As ListBox)
L2.AddItem List1.Text
'将List1中是项目添加到L2中
L1.RemoveItem List1.ListIndex
'将List1中指定的项目删除
End Sub
Public Sub MoveAll()
For i = 0 To List1.ListCount - 1
List2.AddItem List1.List(i)
Next
End Sub






生成b数组 从小到大排

Private Sub Command2_Click()
Dim b(100) As Integer
Dim k As Integer, i As Integer, temp As Integer
num = 0

If Len(Text1.Text) = 0 Then
MsgBox "请先执行"读数据"功能!"
Else

For k = 1 To 100
If a(k) Mod 3 = 0 Then
'判断该数能不能被3整除
num = num + 1
'如果该数能被3整除,则将num加1
b(num) = a(k)
'如果该数能被3整除,则将该数存入数组b中
End If
Next k
For k = 1 To num
For i = k + 1 To num
If b(k) > b(i) Then
temp = b(k)
b(k) = b(i)
b(i) = temp
End If
Next i
Next k
'对数组b中的数据进行从小大的排序
For i = 1 To num
Text2.Text = Text2.Text + Str(b(i)) + Space(2)
Next i
End If
End Sub





判断素数 存执 b组

素数"按钮的代码如下:
Private Sub Command2_Click()
Dim b(100) As Integer
Dim k As Integer, i As Integer
num = 0

If Len(Text1.Text) = 0 Then
MsgBox "请先执行"读数据"功能


Else
For k = 1 To 100
For i = 2 To Sqr(a(k))
If a(k) Mod i = 0 Then Exit For
Next i
If i > Sqr(a(k)) Then
num = num + 1
b(num) = a(k)
End If
Next k
For i = 1 To num
Text2.Text = Text2.Text + Str(b(i)) + Space(2)
Next i
End If
End Sub









i j k l m n 编写全程

Option Base 1
Dim a(6) As Integer
Private Sub Command1_Click()
Open App.Path & "\" & "in5.txt" For Input As #1
s = Input(LOF(1), #1)
Text1.Text = s
Close #1
End Sub
Private Sub Command2_Click()
For i = 1 To Len(Text1)
str1 = Mid(Text1, i, 1)
Select Case LCase(str1)
Case Is = "i"
a(1) = a(1) + 1
Case Is = "j"
a(2) = a(2) + 1
Case Is = "k"
a(3) = a(3) + 1
Case Is = "l"
a(4) = a(4) + 1
Case Is = "m"
a(5) = a(5) + 1
Case Is = "n"
a(6) = a(6) + 1
End Select
Next i
End Sub
Private Sub Command3_Click()
Open App.Path & "\" & "out5.txt" For Output As #1
For i = 1 To 6
Print #1, a(i);
Next i
Close #1
End Sub



合并排列好的数组
Private Sub Command2_Click()
Dim i As Integer, j As Integer, k As Integer
i = 1: j = 1: k = 1
Do While i < 31 And j < 31 And k < 61
If a(i) < b(j) And i < 31 Then
c(k) = a(i)
i = i + 1
Else
c(k) = b(j)
j = j + 1
End If
k = k + 1
Loop
Do While k < 61
If i < 30 Then
c(k) = a(i)
i = i + 1
Else
c(k) = b(j)
j = j + 1
End If
k = k + 1
Loop
'以下程序将合并后的数组C显示在Text3中
For k = 1 To 60
Text3 = Text3 + Str(c(k)) + Space(2)
Next k
End Sub





最长单词的长度 以该长度最后一

次出现的

Option Base 1
Dim s As String

Private Sub Command1_Click()
Open App.Path & "\in5.dat" For Input As #1
s = Input(LOF(1), #1)
Close #1
End Sub

Private Sub Command2_Click()
Dim i As Integer
Dim max As Integer
Dim j As Integer
Dim arr
arr = Split(s, " ")
max = 0
For i = 1 To UBound(arr)
If Len(arr(i)) >= max Then
max = Len(arr(i))
j = i
End If
Next
Text1.Text = max
Text2.Text = arr(j)
End Sub

Private Sub Form_Unload(Cancel As Integer)
Open App.Path & "\out5.dat" For Output As #1
Print #1, Text1.Text, Text2.Text
Close #1
End Sub



把大写换成小些
Private Sub Command1_Click()
Dim s As String
CommonDialog1.Filter = "所有文件|*.*|文本文件|*.txt"
CommonDialog1.FilterIndex = 2
On Error GoTo openerr
CommonDialog1.InitDir = App.Path
CommonDialog1.ShowOpen
Open App.Path & "\in5.txt" For Input As #1
Input #1, s
Close #1
Text1.Text = s
openerr:
End Sub

Private Sub Command2_Click()
Dim i As Integer, n As Integer
Dim s As String
s = Text1.Text
n = Len(s)
Text1.Text = ""
For i = 1 To n
If Asc(Mid(s, i, 1)) <= 90 And Asc(Mid(s, i, 1)) >= 65 Then
Text1.Text = Text1.Text & LCase(Mid(s, i, 1))
ElseIf Asc(Mid(s, i, 1)) <= 122 And Asc(Mid(s, i, 1)) >= 97 Then
Text1.Text = Text1.Text & UCase(Mid(s, i, 1))
Else
Text1.Text = Text1.Text & Mid(s, i, 1)
End If
Next i

End Sub

Private Sub Command3_Click()
CommonDialog1.Filter = "文本文件|*.txt|所有文件|*.*"
CommonDialog1.FilterIndex = 1
On Error GoTo openerr
CommonDialog1.FileName = "out5.txt"
CommonDialog1.InitDir = App.Path
CommonDialog1.Action = 2
Open CommonDialog1.FileName For Output As #1
Print #1, Text1
Close #1
openerr:
End Sub




相关主题
文本预览
相关文档 最新文档