上机操作练习题
1.某数组有20个元素,其值为随机产生的两位整数。现要求将前10个元素与后10个元素对换。即第1个元素与第20个元素互换,第2个数与第19个元素互换,…,第10个元素与第11个元素互换.在窗体上输出数组原来各元素的值与对换后各元素的值。
Option explicit
Dim a(1 to 20) as integer, I as integer, j as integer, t as integer
Private Sub Form_Load()
Show
Print "原始数组是:”
Randomize
For i = 1 To 20
  a(i) = Int(Rnd * 90) + 10
  Print a(i);
If i Mod 10 = 0 Then Print
Next i
Print
Print ”现在数组是:”
For i = 1 To 10
  j = 21 - i
  t = a(i): a(i) = a(j): a(j) = t
Next i
  For i = 1 To 20
  Print a(i);
  If i Mod 10 = 0 Then Print
  Next i
End Sub
2.从数组中删除一个数。先定义一个一维数组,大小、数组的值自定。从键盘上输入一个数,若这个数在数组当中,就把它删除;若这个数不在数组中,提示“该数不在数组中"
注意:1)如果数组中存在多个与该数相同的元素,每一个都能删除;
2)能实现多次删除
Private Sub Form_Load()
Show
ReDim a(9) As Integer
m = 1
Print ”原来数组为:”
For i = 0 To 9
  a(i) = InputBox(”请输入第” & m & "个数:”)
  Print a(i);
  m = m + 1
Next i
Print
n = InputBox(”查的数是:")
i = 0: flag = False
Do
If a(i) = n Then
    j = i: flag = True
    For k = j To UBound(a) - 1
    a(k) = a(k + 1)
    Next k
    ReDim Preserve a(UBound(a) - 1)
    i = i - 1
End If
i = i + 1
Loop While (i <= UBound(a))
If flag = False Then
MsgBox n & "不在该数组中”
Else
Print ”删除” & n & "后的数组是:”
For i = 0 To UBound(a)
Print a(i);
Next i
End If
End Sub
3.数组a中存放着升序排列的数据,使用array赋值,将inputbox输入的新数插入到数组中,插入后数组仍有序。
注意:要求利用动态数组,能够实现多次插入
Private Sub Form_Load()
Show
n = 8: ReDim a(n)
a = Array(5, 15, 23, 52, 68, 88, 90, 99)
n = n + 1: ReDim Preserve a(n)
m = InputBox("请输入要插入的数据:")
For i = 1 To n - 1
  If m < a(i) Then Exit For
Next i
For k = n To i + 1 Step —1
  a(k) = a(k — 1)
Next k
a(k) = m
For i = 1 To n
  Print a(i);
Next i
Print
End Sub
4.6个评委为4位歌手打分,分数介于1—100之间。去掉一个最高分,一个最低分,求出每位选手的最后得分。将4位歌手得分从高到低排列输出,同时输出该选手的编号。
如:  歌手3  95分 歌手 1  87分 歌手 4  80分 歌手 2  70分
Option Base 1
Private Type geshou
  xuhao As String
  fenshu(6) As Integer
  score As Integer
  pingjun As Integer
End Type
Dim singer(1 To 4) As geshou
Private Sub Command1_Click()
For i = 1 To 4
For j = 1 To 6
  singer(i).fenshu(j) = Int(Rnd * 100)
  singer(i).xuhao = "歌手” & i
Next
Next
Call paixu
End Sub
Private Sub paixu()
Dim p As geshou
For i = 1 To 4
  For j = 1 To 5
  For k = j To 6
    If singer(i).fenshu(j) > singer(i)。fenshu(k) Then
    t = singer(i)。fenshu(j)
    singer(i).fenshu(j) = singer(i).fenshu(k)
    singer(i).fenshu(k) = t
    End If
  Next
  Next
Next
For i = 1 To 4
For j = 2 To 5
  singer(i).score = singer(i)。score + singer(i)。fenshu(j)
Next
singer(i)。pingjun = singer(i).score / 4
Next
For i = 1 To 3
For j = i To 4
  If singer(i).score 〈 singer(j).score Then
  p = singer(i): singer(i) = singer(j): singer(j) = p
  End If
Next
Next
For i = 1 To 4
Print singer(i)。xuhao & ”得分:” & singer(i)。pingjun
Next
End Sub
Private Sub Form_Load()
vb 字符串转数组
End Sub
10个评委给10个选手打分
Private Sub Form_Load()
Show
n = UBound(b)
For i = 1 To 10
  For j = 1 To 10
      a(i, j) = Val(InputBox(”请第” & j & ”位评委给第” & i & ”位选手打分:"))
Next j
Next i
For i = 1 To 10
    max = a(i, 1): sum = 0
    min = a(i, 1): ave = 0
  For j = 1 To 10
      If a(i, j) > max Then max = a(i, j)
      If a(i, j) 〈 min Then min = a(i, j)
      sum = sum + a(i, j)
  Next j
  ave = (sum - max — min) / (n — 2)
  b(i) = ave
Next i
For i = 1 To n - 1
    For j = i + 1 To n
      If b(i) 〈 b(j) Then
      t = b(i): b(i) = b(j): b(j) = t
      End If
    Next j
Next i
Print ”选手的得分由高到低为:"
For i = 1 To 10
    Print b(i);
Next i
End Sub
5.编写函数求一元二次方程ax2+bx+c=0的解。
注意:要求考虑方程有两相等实根、两不等实根和两个虚根的情况。
Dim a As Single, b As Single, c As Single
Dim s As Single
a = InputBox("请输入a")
b = InputBox(”请输入b”)
c = InputBox("请输入c”)
s = b ^ 2 - 4 * a * c
If s 〈 0 Then
MsgBox (”方程无解”)
ElseIf s = 0 Then
Print —b / 2 * a
Else
Print ((—b) + Sqr(s)) / 2 * a & ”  " & ((—b) — Sqr(s)) / 2 * a
End If
End Sub
6任意输入一组字符,统计出大写字母多少个,小写字母多少个,数字多少个?要求用过程实现。
Call tongji(a, n, m, o)
Print "数字:" & n & Chr(13) & "小写:" & m & Chr(13) & ”大写:" & o
End Sub
Private Sub tongji(a As String, n As Integer, m As Integer, p As Integer)
For i = 1 To Len(a)
If Asc(Mid(a, i, 1)) > 47 And Asc(Mid(a, i, 1)) 〈 58 Then
  n = n + 1
ElseIf Asc(Mid(a, i, 1)) 〉 96 And Asc(Mid(a, i, 1)) < 123 Then
  m = m + 1
ElseIf Asc(Mid(a, i, 1)) 〉 64 And Asc(Mid(a, i, 1)) < 91 Then
  p = p + 1
End If
Next
End Sub
7.随机产生一个5*5的矩阵,将主对角线和次对角线元素都置为1,并求出矩阵的四周元素之和,显示在列表框中
Option Base 1
Private Sub Form_Load()
Dim a(5, 5) As Integer

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。