程序界面如图所示,左边列表框List1中显示的是部分职工的卡号和余额,在文本框Text1中输入职工的IC卡号,单击“查询余额”按钮(Command1)后,如果找到此卡号,则在标签Lab3中显示“此卡号余额为”和对应的余额值,如果未找到则显示“找不到此卡号,请重新输入”。
解决此问题的部分程序段如下:
Const n=500' 设卡号总数为500
Dim zg(1 To n) As Long
Dim ye(1 To n) As Single
Private Sub Form1_Load()
' 此过程用于对数组zg和数组ye进行初始赋值,代码略
End Sub
Private Sub Command1_Click()
Dim x As Long, i As Long, j As Long, m As Long, find As Boolean
x=Val(Text1.Text)
i = 1 : j = n : find = False
Do While ‘改错
m = Int((i + j) / 2
If x = zg(m) Then
①
Else If ② Then
j = m - 1
Else
i = m + 1
End If
Loop
If find = true Then
Lab3.Caption=″此卡号余额为″+Str(ye(m))+″元″
Else
Lab3.Caption=″找不到此卡号,请重新输入″
End If
End Sub
程序中①画线处应填入。
程序中②画线处应填入。
Const n=10
Dim a (1 To n) As Integer, y (1 To n) As Integer, i As Integer, j As Integer
k=Val(Text1.Text)
For i=1 To n
y(i)=1
Next i
For i=1 To n
For j=1 To n
If a(i) < a(j) Then y(i) = y(i)+1
Next j
If y(i) = k Then List1.Addltem Str(a(i))
Next i
数组a的各元素的值为“3,4,3,1,4,4,5,5,6,2,0”,在文本框中输入4,执行该段程序后List1中显示的值为( )
算法分析:要使得到的5位数字最小,首先需保证能取到5个数字,且首位应尽量小,那么取首位数的范围应保证余下有4个数字,即在前4位中选最小非0的数字。
程序运行时,在文本框Text1中输入一个数字字符串,在文本框Text2中输入要移除的数字个数,单击“运行”按钮CmdRun后,在标签Labell显示得到的最小新数,程序运行界面如图b所示。
Private Sub CmdRun_ Click()
Dim Num As String,New_num As String ' Num为输入的数字字符串
Dim i As Integer, j As Integer, m As Integer,
Dim As Integer, s As Integer, k As Integer
Dim a(30) As String, b(30) As Integer
Num = Text1. Text: s = Len(Num)
k = Val (Text2. Text) 'k为删除数字个数
For i=1 To s
a(i) = Mid(Num,i,1)
Next i
j =1
m=1
n=
For i=m+1 To n '取第一个非零最小数字
If Then m=i '加框处程序代码有误,请改正
Next i
b(j)= m
Do While '取其他最小数字
m=m+1
n=n+1
For i=m+1 To n
If a(i) < a(m) Then m= i
Next i
j =j+1:b(j)=m
Loop
For i= m+1 To s '最后直接取的数字
j =j+1: b(j)=i
Next i
For j=1 To s- k '输出最小数
New_ num =
Next j
Labell. Caption = New_num
End Sub
Private Sub Cmd_Click( )
Dim i As Integer, j As Integer, k As Integer
Dim x1 As Single, y1 As Single, x2 As Single, y2 As Single
Dim d As Single, Totald As Single, v As Single, s As String
s= ①
x1=0:y1=0 ‘出发点为坐标原点
k=1:i=1:Totald=0
For i=1 To Len(s)
If Mid(s,i,1)=","Then
v=Val(Mid(s,j,i-j)) ‘提取坐标值,保存在变量v中
j=i+1
If ② Then
x2=v
Else
y2=v
d=Sqr((x2-x1)-2+(y2-y1)-2)
Totald=Totald +d
x1=x2:y1=y2
End If
k=k+1
End If
Next i
Label1.Caption=Str(Totald)
End Sub
①②
⒈牛奶[数量:3;价格:8]
⒉苏打水[数量:5;价格:6]
⒊瓶装水[数量:2;价格:5]
顾客需要先依次输入购买商品的id和数量,再投币购买。
①若投币金额超出饮品总价格,则显示“购买成功,退还*元!”。
②若投币金额不足,则显示“金额不足!”。
③若购买数量过多,则显示“商品库存不足!”。
④若输入的商品id不存在,则显示“商品未找到!”。
程序运行结果如图所示,请编写Python程序实现上述功能。
if (a%3=0) Or (b%3=0) :
c = a * b
else:
c = a + b
执行后变量c的值为( )
1)将明文中的字母E替换为A,F替换为B,G替换为C,…A替换为W,B替换为X,C替换为Y,…,小写字符的替换方法同理。
如:明文:I Love China 加密为:E Hkra Ydejw
2)将加密后的英文字母,按顺序依次放入每个字符串段落中,第1个加密字母E放在第1个段落,第2个字母H放在第2个段落,依次类推(如下图),段落间用逗号分隔并以逗号结尾。当遇到空格,在段落末尾“,”前,插入“+”号作为标记。
第1段 | 第2段 | 第3段 | 第4段 | 第5段 | 第6段 | 第7段 | 第8段 | 第9段 | 第10段 |
E+, | H, | k, | r, | a+, | Y, | d, | e, | j, | w+, |
3)每个段落中,随机插入字母形成新的字母序列段,当段号小于8时加密字母在字母序列
中存放位置刚好是该段落号,位置以8为周期,第9段时,加密字母存放位置回到字母序列的第1位,第10段加密字母存放位置回到第2位,依次类推。(位置号<=8)
第1段 | 第2段 | 第3段 | 第4段 | 第5段 |
Etc+, | mHfryh, | oTkrestf, | rfir, | Ccfta+, |
第6段 | 第7段 | 第8段 | 第9段 | 第10段 |
iynhyYf, | tqswjyd, | nrtesiueh, | jnytq, | Uwccdb+, |
4)在每一段中随机插入除英文字母之外的ASCII字符码,形成一串密文。如图所示:
则根据加密算法,解密后的明文为
Private Sub Command1_Click()
Dim i As Integer, k As Integer
Dim s As String, c As String, ch As String
Dim letter As String, ret As String 'letter 用于存放字母序列s = Text1.Text
k = 1
For i = 1 To Len(s)
c = Mid(s, i, 1)
If c >= "A" And c <= "Z" Or c >= "a" And c <= "z" Then letter = letter + c
ElseIf
c = ","
Then
ch = Chr(Asc(ch) + 4)
If Not (ch >= "A" And ch <= "Z" Or ch >= "a" And ch <= "z") Then ch = Chr(Asc(ch) - 26)
End If
IfThen ret = ret + ch + " "
Else
ret = ret + ch
End If
k = k + 1
End If
Next i
Text 2.Text = ret
End Sub
1)操作1(单词的删除):在Text2中输入一个单词s1,在字符串s中找到左边第一个出现的相同单词(区分大小写),将其删除,并在Label1中显示删除单词的位置pos;若s中不存在单词s1,则字符串s不变。
例如:s=“Go with your your passion.”s1=“your” 点击“删除”按钮后,s=“Go with your passion.”pos=“9”若s1=“Your”,则s不变。
2)操作2(单词的插入):在Text3中输入一个单词s2,并在Text4中输入插入位置w,将s2插入到经过删除操作的s中以w开始的位置。
例如:s=“Go with your passion.”s2=“absolutely” w=21
点击“插入”按钮后,s=“Go with your passion absolutely.”
程序运行界面如下图所示。
实现上述功能的VB程序如下。请回答下列问题:
Dim s As String
Private Sub Command1_Click( ) '实现删除单词命令
Dim s1 As String, t As String, i As Integer, pos As String, result As String Dim begin As Integer, word As String
s = Text1.Text s1 = Text2.Text begin = 1
For i = 1 To Len(s)
t =
If Not (t >= "a" And t <= "z" Or t >= "A" And t <= "Z") Then
If i > begin Then
word =
If word = s1 Then ‘找到与s1相等的单词,则将该单词删除
s = Mid(s, 1, begin - 1) + Mid(s, i+1, Len(s) - i)
pos =
Exit For
End If
End If
begin = i + 1
End If
Next i
Text5|.Text = s
Label1.Caption = pos End Sub
Private Sub Command2_Click() '实现插入单词命令
Dim w As Integer, result As String, s2 As String s2 = Text3.Text
result = + " " + s2 + " " + Mid(s, w, Len(s) - w + 1) Text5.Text = result
End Sub
Private Sub Command1_Click()
Dim s As String, s1 As String, c As String
s = Text1.Text
For i = 1 To Len(s)
c = Mid(s, i, 1)
s1 = ⑴
For j = ⑵
If c <> Mid(s, j, 1) Then
s1 = s1 + Mid(s, j, 1)
End If
Next j
⑶
Next i
Text2.Text = s
End Sub
x=3 If x>5 Then y=x*x Else y=4*x End If |
该程序段运行后,变量y的值是( )。
①生成n*n的矩阵,其中计划埋雷m个,n和m的数据分别从text1和text2中获取(n<=10,m<=n)
②随机位置埋好地雷后(埋雷位置用X表示且埋雷位置不重复),未埋雷的位置显示该位置周围一圈地雷的数量。
程序代码如下:
Dim flag(0 To 200) As Boolean, a(0 To 200) As String
Dim n As Integer, m As Integer
Private Sub Command1_Click( )
Dim s As String, x As Integer, k As Integer
Dim i As Integer, j As Integer
Randomize
n = Val(Text1.Text): m = Val(Text2.Text)
‘初始化数组flag中各元素均为False,数组a中各元素值为字符"0",代码略
For i = 1 To m
x = Int(Rnd * (n ^ 2)) + 1
If flag(x) = True Then i = i - 1 Else flag(x) = True
Next i
For k = 1 To n * n
If flag(k) = True Then
a(k) = "X"
Else
For i = (k - 1) \ n To (k - 1) \ n + 2
For j = ①
If i = 0 Then Exit For
If flag((i - 1) * n + j) = True And j >= 1 And j <= n Then
a(k) = ②
End If
Next j
Next i
End If
Next k
For i = 1 To n * n ‘在列表框list1中输出矩阵
s = s + " " + a(i)
If ③ Then
List1.AddItem s
s = ""
End If
Next i
End Sub
根据上述代码,完成下列问题:
① ② ③
a=2 If a*a>3 Then a=a-2 End If Print a |
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中显示的内容是( )
if k<=10 and k >0:
if k >5:
if k>8:
x=0
else:
x=1
else:
if k>2:
x=3
else:
x=4
其中k取哪组值时,x=3。( )
i = 1 : n = 5
Do While i <= n
x = Int(Rnd * 9) + 1
If x Mod 2 = 1 Then
a(i) = x
Else
a(n) = x : n = n - 1
End If
i = i + 1
Loop
已知数组a中各元素初始值均为0,执行该程序段后,数组a(1)至a(5)各元素值不可能的是( )
if b > maxv:
maxv = b
if c>maxv:
maxv = c
B . if a>b and a>c:maxv = a
elif b>c:
maxv = b
else:
maxv = c
C . if a>b:maxv = a
else:
maxv = b
if c > maxv:
maxv=c
D . If a >b and a >c:maxv = a
if b>a and b > c:
maxv = b
ifc>a and c>b:
maxv = c
print("请输入3个数:")
x=int(input("x="))
y=int(input("y="))
z=int(input("z="))
temp=0
if x>y:
temp=y
y=x
x=temp
if x>z:
temp=z
z=x
x=temp
if y>z:
temp=z
z=y
y=temp
print("这3个数新序列是:",x,y,z)
x = 5: y = -20
If Not x > 0 Then x = y - 3 Else y = x + 3
Print x - y; y – x
程序运行后,依次输入:12 35 9 11 23 88 13 19 29 55
运行结果是: