长度小于等于 100),在列表框 List1 里输出所有配对括号在表达式中的位置。例如在文本框 Text1 里入:“12*(3-(1+2))*3”,程序运行效果如图所示。
实现上述功能的 VB 代码如下,在划线的地方填入合适代码,完善程序。Private Sub Command1_Click()
Dim s As String
Dim a(1 To 100) As Integer
|
i = 1 top = 0
Do While i <= Len(s)
If Mid(s, i, 1) = "(" Then
top = top + 1
End If
If Mid(s, i, 1) = ")" Then
List1.AddItem Str(a(top)) + " " + Str(i)
End If
i=i+1 Loop
End Sub
flag = True : i = 1
Do While i <= 6
x = Int(Rnd * 100) + 1
If flag Or x >50 Then
a(i)=x
flag = Not flag
i = i + 1
End if
Loop
执行该程序段后,在下列选项中,a(1)~a(6)各元素值不可能的是( )
i = 1: j = 10: temp = a(5)
Do While a(j) > temp j = j - 1
Loop
Do While a(i) < temp i = i + 1
Loop
temp = a(i) :a(i) = a(j):a(j) = temp
数组元素a(1)到a(10)的值依次为“36 91 36 17 93 99 2 9 97 97”,执行该程序段后,a(5)的值为( )
Private Sub Command1_Click()
Dim n As Integer, L As Integer, t As Integer
Dim i As Integer, j As Integer, k As Integer
Dim s As String, result As String
Dim a(1 To 50) As Integer, dir(1 To 50) As Integer
L = Val(Text1.Text): n = Val(Text2.Text): t = Val(Text3.Text) s = Text4.Text
s = s + ".": tmp = 0: k=0
For i = 1 To Len(s)
ch = Mid(s, i, 1)
If ch >= "0" And ch <= "9" Then
tmp = tmp * 10 + Val(ch)
Else
k = k + 1
a(k) = tmp: dir(k) = 1
tmp = 0
End If
Next i
For i = 1 To t
For j = 1 To n
IfThen dir(j) = -dir(j)
Else
For c = 1 To n
IfThen dir(c) = (-1) * dir(c)
dir(j) = (-1) * dir(j)
End If
Next c
End If
Next j
Next i
For i = 1 To n
result = result + Str(a(i))
Next i
Label5.Caption = Str(t) + "秒后小球的位置为:" + result
End Sub
Private Sub Command1_Click()
Dim a(1 To 6) As Integer, b(1 To 6) As String, i as integer, j as integer
Dim c As String, k As Integer, tmp1 As Integer, tmp2 As String
s=text1.text
i = 1: k = 1: tmp1 = 0: tmp2 = ""
Do While i <= Len(s)
c = Mid(s, i, 1)
If c = "," Then
a(k) = tmp1: b(k) = tmp2
tmp1 = 0: tmp2 = ""
①
Else
If Then
tmp2 = tmp2 + c
Else
tmp1=tmp1*10+val(c)
End If
End If
i = i + 1
Loop
For i = 1 To 5
For j = 1 To 6 - i
If a(j) > a(j + 1) Then tmp1 = a(j): a(j) = a(j + 1): a(j + 1) = tmp1
If Len(b(j)) > Len(b(j + 1)) Or ② Then
tmp2 = b(j): b(j) = b(j + 1): b(j + 1) = tmp2
End If
Next j
Next i
For i = 1 To 6
List1.AddItem Str(a(i)) + b(i)
Next i
End Sub
① ②
For i = 1 To 6
a(i)= Int(Rnd * 10)+ 1
Next i
For i = 1 To 5
If i Mod 2 = 1 And a(i) > a(i + 1) Then
t = a(i): a(i) = a(i + 1): a(i + 1)= t
Else
a(i) = a(i) + 1
End If
Next i
执行该程序段后,a(1)~a(6)各元素可能的值是( )
For i=2 To 5
j = i
k = a(j)
Do While a (j-1) < k And j > 1
a(j) = a (j-1)
Loop
a (j) = k
Next i
数组元素a(1)~a(5)的值依次为79,20,34,5661,该程序段运行后,数组元素a(1)~a(5)的值依次是( )
某同学设计了以下程序用来输出杨辉三角。在文本框Text1里输入需要输出的杨辉三角的行数n,单击“打印”按钮Command1后,在列表框List1中输出n行的杨辉三角。
Private Sub Command1 _ Click( )
Dim n As String, i As Integer, j As Integer
Dim a(0 To 30) As Long
Dim s As String
List1. Clear
①
a(1)=1
List. AddItem “ 1”
For 1=2 To n
a(1)=1
s = “”
For j=i To 1 Step-1
②
s=Str(a(3)) +”” + s
Next j
③
Next i
End sub
① ② ③
数组元素 |
数组元素的含义 |
a(1) |
存储班级数n |
a(2) |
从a(2)到a(n+1)依次存储1、2、…、n班学生的人数 |
… |
|
a(n+1) |
|
a(n+2) |
从a(n+2)开始依次存储1班每个学生的单科成绩,2班每个学生的单科成绩、……、n班每个学生的单科成绩 |
… |
|
… |
张老师依据数组a中的相关数据编写了VB程序来统计各班平均分。
程序功能如下:在文本框Text1中输入科目名称,单击“读取数据库”按钮Command1程序从数据库读取数据;单击“开始统计”按钮Command2程序进行统计处理,结果输出在列表框List1中。程序运行界面如图所示。
Dim a (1 To 600) As Integer数组大小满足处理要求 Private Sub Command1_Click ( )
‘本过程从数据库读取指定科目的各相关数据,存储在数组a中
‘本过程语句略
End Sub
Private Sub Command2_Click ( )
‘依据数组a中的相关数据统计各班级平均分
Dim i As Integer, j As Integer, n As Integer
Dim p As Integer, sum As Integer, aver As Single
①
p=n+2
For i =1 To n
sum= 0
For j=1 To a (i+1)
②
p=p+1
Next j
aver= sum/a (i +1)
List1 AddItem Str(i)+ “” n+ str(a(i+1)) + “” + Str(aver)
Next i
End Sub
① ②
′待排序数据已存储在数组a中(a(1)~a(n))
For i = 1 To n - 2
k = i
For j = ′①
If a(j) > a(k) Then k = j
Next j
If k <> i Then t = a(i): a(i) = a(k): a(k) = t
Next i
For i = 1 To n \ 2
j = 2 * i - 1
If a(j) < a(j + 1) Then t = a(j): a(j) = a(j + 1): a(j + 1) = t
Next i
For i = ′②
t = a(i): j = i - 1
Do While t > a(j)
a(j + 1) = a(j): j = j - 1
Loop
′③
Next i
上述程序段中 3 处方框处的代码分别为 ( )
Dim a(1 To 10) As Integer
Dim i, j As Integer
For i = 1 To 10
j = i
Do While j <= 10
If j Mod i = 0 Then
a(j) = a(j) + 1
End If
j = j + 1
Loop
Next i
Text1.Text = Str(a(3))
数组a各元素初值为0,执行上述程序段后,文本框Text1中显示的内容为( )
Dim n As Integer
Dim st As String
Dim a(1 To 100) As Integer
Dim b(1 To 100) As Integer
Private Sub Command1_Click()
'产生矩阵,代码略
End Sub
Private Sub Command2_Click() '旋转矩阵
For i=1 To n '行
st= “”
For j= 1 To n '列
b((i-1)*n+j)=
st=st+adj(Str(b((i-1)*n+j)),3)
Next j
List2.AddItem st
Next i
End Sub
Function adj(a As String, n As Integer) As String
'在字符串a前加入适当的空格,使a的长度为n位,代码略
End Function
Dim a(1 To 6) As Integer
Dim d(1 To 6) As Integer
a(1) = 10: a(2) = 21: a(3) = 28: a(4) = 11: a(5) = 12: a(6) = 30
Const n = 6
For i = n To 1 Step -1
d(i) = 0
For j = n To i +1 Step -1
If a(j) > a(i) Then d(i) = d(j) + 1
Next j
If d(i) > Max Then Max = d(i)
Next i
Text1.Text = Str(Max)
执行该程序段,文本框Text1中显示的是( )
age=float(input('请输入age=')) #输入年龄
=float(input('请输入HRrest=')) #输入安静心率
low=(220-age-HRrest)*0.6+ HRrest #计算最适宜运动心率低值
high=(220-age-HRrest)*0.8+HRrest #计算最适宜运动心率高值
print("最适宜的心率是:",low,"~",high)
input("运行完毕,请按回车键退出...")
Dim i As Integer, Sum As Integer
Dim a(1 To 11) As Integer
Sum=6
a(11) = 49
For i=10 To 1 Step-1
a(i)=a(i+1)-1
If a(i)Mod 3=0 Then Sum=Sum+a(i)
Next i
Text1. Text = Str( Sum)
该程序段运行后,文本框text1中显示的内容是( )