
请根据算法将下列程序补充完整。
Function prime(a As Integer) As Boolean
Dim j As Integer
prime = True
j = 2
Do While prime And j <= a \ 2
If a Mod j = 0 Then
prime = False
Exit Do
End If
Loop
End Function
Private Sub Command1_Click( )
Dim i As Integer, a As Integer, b As Integer
Dim count As Integer
a = Val(Text1.Text)
b = Val(Text2.Text)
For i = a To b
If = True Then
List1.AddItem Str(i)
count = count + 1
End If
Next i
Label1.Caption = "素数共有" + Str(count) + "个!"
End Sub
程序代码如下,为实现上述功能,请在画线处填写合适的代码。
Dim bh(10 000)As String ‘存储学生编号
Dim jf(10 000)As Integer ‘存储学生获得的积分值
Dim n As Integer ‘存储学生总人数
Function Search(x As Integer)As Integer
Search=0
i=1:j=n:flag=True
Do While i<=j And flag=True
m=Int((i+i)/2)
If x=jf(m) Then
flag=False
①
Elself ② Then
i=m+1
Else
j=m-1
EndIf
Loop
End Function
Private Sub Form_Load()
‘从数据库中读取学生编号和积分,分别存储到bh数组和jf数组中,并按积分值从小到大排序,显示在列表框List1中
End Sub
Private Sub Command1_Click()
Dim Top As Integer,Bottom As Integer,Topxb As Integer,Bottomxb
As Integer
List2.Clear
Top=Val(Text1.Text)
Bottom=Val(Text2.Text)
If Top>Bottom Then t=Top:Top=Bottom:Bottom=t
If Top>jf(n)Or Bottom<jf(1)Then Exit Sub
Do While Search(Top)=0
Top=Top+1
Loop
Do While Search(Bottom)=0
Bottom=Bottom-1
Loop
Topxb=Search(Top)
Do While jf(Topxb)=jf(Topxb-1)And Topxb>l
Topxb=Topxb-1
Loop
Bottomxb=Search(Bottom)
Do While jf(Bottomxb)=jf(Bottomxb+1)And Bottomxb<n
Bottomxb=Bottomxb+1
Loop
For i= ③
List2.AddItem bh(i)&" "&jf(i)
Next i
End Sub
① ② ③
删数的算法如下:
⑴如果k>0,则从前往后检测相邻字符,否则,转⑶;
⑵①若所有相邻字符都已非降序,则将串尾k个字符删去,k值置0,转⑴;
②若相邻两数存在逆序(即前一个数>后一个数),则将前一个数删除,k值变化,然后回到⑴;
⑶去掉串首的0,输出结果。
按照上述算法思路,编写了VB程序,功能如下:在文本框Text1中输入数字串,在文本框Text2中输入删数的个数,单击“处理”按钮Command1,在文本框Text3中显示最小的整数。程序运行界面如图所示。
delete函数说明:delete(st,x,y)为自定义函数,功能为在字符串st中删除x位置开始的y长度的子串。
Private Sub Command1_Click()
Dim s As String, k As Integer, i As Integer, j As Integer, n As Integer
s = Text1.Text
k = Val(Text2.Text)
n = Len(s)
Do While k > 0
i = 1
Do While i < n And
i = i + 1
Loop
If i = n Then
n = n - k
k = 0
Else
s = delete(s, i, 1)
n = n - 1
End If
Loop
i = 1
Do While n > 1 And Mid(s, 1, 1) = "0"
s = delete(s, 1, 1)
i = i + 1
n = n - 1
Loop
Text3.Text = s
End Sub
Function delete(st As String, x As Integer, y As Integer) As String
delete = Mid(st, 1, x - 1) + Mid(st, x + y) 'mid函数第3个参数省略,则截取从开始位置向右到字符串结尾的所有字符
End Function
Function fact(n As Integer) As String
If n = 1 Then
fact = fact & 1
Else
fact = fact(n \ 2) & n Mod 2
End If
End Function
Private Sub Command1_Click()
Dim x As String, i As Integer, r As Integer
x = Text1.Text
For i = 1 To Len(Text1.Text)
If Mid(x, i, 1) = "." Then Exit For Next i
r = Val(Mid(x, i + 1, Len(Text1.Text) - i)) Text2.Text = fact(r)
End Sub
在文本框Text1中输入2019.70,则执行程序段,在文本框Text2中显示的内容是( )
Private Sub Command1_Click()
Dim i As Integer,pos As Integer
Dim s As String,c As String,result As String
Dim s1 As Single,s2 As Single
result=" ": i=1
s=Text 1.Text
c=Mid(s,i,1)
Do While c<>"."
i=i+1
c=Mid(s,i,1)
Loop
pos=i
①
s2 = Val("0."+Mid(s,pos +1))
result=convert(s1)&"." & convert(s2)
Text2.Text=result
End Sub
Function convert(x As Single)As String
Dim temp As Integer,k As lnteger
If x>=1 Then '整数转化为二进制
temp=x Mod 2
result=temp & result
x=x\2
Do While x <>0
temp=x Mod 2
result=temp & result
x=x\2
Loop
Else '小数转化为二进制,保留3位小数
k=1
Do While ②
temp=Int(x*2)
result=result & temp
③
k=k +1
Loop
End If
convert=result
End Function
①②③
如10名运动员1号到10号的成绩分别是“13,6,9,8,10,11,10,14,16,13”,从高到低排序后,第一组的成绩依次是“16,13,10,10,9”,第二组的成绩依次是“14,13,11,8,6”。第1位的成绩分别是16和14,则第一组获胜得1分,第二组扣1分,再比较两个分组第2位的成绩13和13,则两组均不得分。依次处理,比较完剩余队员的成绩,可得第一组得分为2,第二组得分为-2。
编写一个VB程序,实现如下功能:在文本框Text1中依次输入成绩(偶数个整数,用逗号分隔并以逗号结尾),单击“确定”按钮Command1后,在列表框List1中显示对阵编号、对阵成绩及两个分组的最后得分。程序运行界面如图所示。
Private Sub Command1_Click()
Dim i As Integer, j As Integer, k As Integer
Dim s As String, ch As String, sum1 As Integer, sum2 As Integer
Dim bh (1 To 50) As Integer, cj(1 To 50) As Integer
s= Text1.Text
j= 1: k=0
For i= 1 To ①
ch = Mid(s, i, 1)
If Not (ch>= "0" And ch <= "9") Then
k=k+1
bh(k)=k
cj(k) = Val(Mid(s, j, i-j))
②
End If
Next i
For i= 1 To k-2 Step 2
For j= k To ③ Step -1
If cj(j)> cj(j-2) Then
t= cj(j): cj(j)=cj(j-2): cj(j-2)=t
t= bh(j): bh(j)= bh(j-2): bh(j-2)=t
End If
Next j
Next i
sum1 = 0: sum2= 0
List1 .AddItem "对阵编号 对阵成绩"
For i=1 To k- 1 Step 2
If cj(i)> cj(i+1) Then
sum1 = sum1 + 1: sum2 = sum2- 1
sum1=sum1-1: sum2=sum2+1
End If
List1.AddItem adj(bh(i)) + "<-->" + adj(bh(i+1)) + adj(cj(i)) + "<-->" + adj(cj(i+1)
Next i
List1.AddItem"第一组得分: "+ adj(sum1)
List1.AddItem"第二组得分: " + adj(sum2)
End Sub
Function adj(x As Integer) As String
‘函数功能:将数值x转换成字符串,并在字符串的左侧添加若干空格。代码略
End Function
① ② ③
⑴每个回文都有一个“中心”,当回文字符数为奇数时,中间的那个字符就是回文中心;但是当回文的字符数为偶数时,回文的中心是最中间的那两个字符,且这两个字符相同。
⑵对任意一个字符或者相同的两个连续字符,我们都可以假设它为回文的“中心”,向它的 左右两边扩展出尽可能长的回文。对于每种假设,我们都能得到一个回文,而最长回文必定由 其中的某个假设中得到!
现编写一个程序,在 Text1 中输入一串字符,单击“统计”按钮,在 Text2 中显示该字 符串中的最大的回文子串(长度相同时,输出最左边的子串)。请回答下列问题:
Dim n As Integer
Dim a(0 To 100) As String Private Sub Command1( Click)() Dim s As String
Dim left As Integer, right As Integer, i As Integer
Dim max As Integer, m As Integer, b1 As Integer
'变量b1用于记录回文子串的左端起点
Text2.Text = ""
s = Text1.Text n = Len(s)
For i = 1 To n
a(i) = Mid(s, i, 1) Next i
max = 0: left = 0: right = 0
For i = 1 To n left = i
right = i
m = longest(left, right) If m > max Then
b1 = i - m \ 2 max = m
End If left = i
right = i + 1
If a(left) = a(right) Then
m = longest(left, right) + 1
If m > max Then
①
max = m
End If
End If
Next i
For i = b1 To ②
Text2.Text = Text2.Text + a(i) Next i
End Sub
Function longest(left As Integer, right As Integer) As Integer
Dim p As Integer p = 1
Do While left > 1 And right < n And ③
left = left - 1 right = right + 1 p = p + 2
Loop
longest = p
End Function
① ② ③
编写VB程序,功能如下:程序运行时,在文本框Text1中输入序列,单击“计算”按钮Command11,在文本框Text2中输出逆序数,并在 Label3中输出判断结果(偶排列或者奇排列)。程序运行界面如图所示。

Function NiXuShu(s As String) As Integer '逆序数计算
Dim i As Integer, j As Integer, c As Integer , n(1 To 100) As Integer
c=0
For i= 1 To Len(s)
n(i) =Val(Mid(s, i, 1))
For j=1 To i- 1
ifThen
c=c+ 1
End If
Next j
Next i
End Function
Private Sub Command1_ Click()
Dim x As String, y As Integer
x = Text1.Text
Text2.Text = Str(y)
If y Mod 2= 0 Then Label3.Caption = "偶排列" Else Label3.Caption = "奇排列"
End Sub
1)找出等式中单个数字的最大值;
2)从最大值加1的数制开始,按该数制的运算规则,把等式中所有数字转换成十进制数;
3)如果等式两边的结论是成立的,那么该数制就是符合上述等式的最小数制,否则从下一个数制重新计算,直到十六进制为止。
小明为此编写了一个VB程序,程序运行时,在文本框Text1中输入一个等式,单击“计算”按钮Command1,在文本框Text2中显示该等式中数字所属的数制。程序运行界面如下图所示。
Private Sub Command1_Click()
Dim s1 As String, ysf As String, t1 As String, t2 As String, t3 As String
Dim ch As String, t As String, max As Integer, jz As Integer, i As Integer
s1=Text1.Text
t1=0: max=0
For i=1 To Len(s1)
ch=Mid(s1, i, 1)
If ch=“+” Or ch=“-” Or ch=“*” Or ch=“\\” Then
ysf=ch: t1=t: t=“ ”
ElseIf ch=“=” Then
t2=t: t=“ ”
ElseIf ch >= “A” And ch <= “F” Then
t=t+ch
temp=
If max < temp Then max=temp
Else
t=t+ch
If max < Val(ch) Then max=Val(ch)
End If
Next i
t3=t: jz=0
Do While i <= 16
If ysf=“+” Then
If XtoD(i, t1)+XtoD(i, t2)=XtoD(i, t3) Then jz=i: Exit Do
ElseIf ysf=“-” Then
If XtoD(i, t1) - XtoD(i, t2)=XtoD(i, t3) Then jz=i: Exit Do
ElseIf ysf=“*” Then
If XtoD(i, t1) * XtoD(i, t2)=XtoD(i, t3) Then jz=i: Exit Do
Else
If XtoD(i, t1) / XtoD(i, t2)=XtoD(i, t3) Then jz=i: Exit Do
End If
i=i+1
Loop
If jz <> 0 Then
Text2.Text=“该数属于” +Str(jz)+ “进制”
Else
Text2.Text=“没有找到合适的进制”
End If
End Sub
Function XtoD(x As Integer, s As String) As Integer
Dim j As Integer
j=0
For i=Len(s) To 1 Step -1
XtoD=XtoD+Val(Mid(s, i, 1)) * x ^ j
Next i
End Function
screen.fill((255,255,0)) #窗口填充黄色*
( ) #加载背景图像
( ) #将背景图画在窗口
pygame.display.update() #窗口刷新
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
1)初始时,建立一个空字典,把26个小写字母加入字典中。26个小写英文字母编码为1-26,字母“a”的编码为1,字母“b”的编码为2,其它字母的编码依次增加。
2)从字符串第一个位置开始扫描,若遇到空格(空格用“*”表示),则得到一个单词,截取该单词并存入变量中。
3)若该单词在字典中,则取出该单词在字典中的编码值;否则,依次取出该单词中各字母在字典中的编码值,(编码值之间用一个空格分隔),同时产生该单词的编码(编码为字典中的最大编码值加1),加入字典中。
4)继续扫描字符串,截取单词,并按照步骤(3)的方法进行处理,直至整个字符串编码完毕。字符串“abab”LZW编码过程如图所示。
Dim pos As Integer, a(1 To 100)As String
Private Sub Form_Load( )
'将字母“a”至“z”依次存入数组元素a(1)到a(26)
End Sub
Function judge(ss As String) As Integer '判断字典中是否有ss这个单词
Dim i As Integer judge = 0
For i = 27 To pos
If a(i) =Then judge = i
Exit For
End If
Next i
End Function
Private Sub Command1_Click( )
Dim s As String, i As Integer, j As Integer, k As Integer, ch As String, word As String, result As String, Dim id As Integer, idletter As Integer
s = Text1.Text pos = 26: i = 1
Do While i <= Len(s)
j = i
Do While Mid(s, j, 1) <> " " j = j + 1
If j > Len(s) Then Exit Do
Loop
word = Mid(s, i, j - i)
If Len(word) = 1 Then '单个字母的单词
result = result + Str(Asc(word) -Asc("a") + 1)
Else
id = judge(word)
If id = 0 Then '新的单词
For k = 1 To Len(word) '依次处理该单词中的各个字母编码
ch = Mid(word, k, 1)
Next k
pos = pos + 1 '对该单词编码
a(pos) = word
Else '词典中存在该单词
result = result + Str(id) End If
End If
If j < Len(s) Then result = result + " *"
Loop
Text2.Text = result
End Sub
s = "p2y0t2H3on1*c"
t =""
for ch in s:
if "A”<= ch <="Z":
ch = chr (ord(ch) + 32)
if "a”<= ch <="z”:
t=t+ch
print (t)
运行该程序段,输出结果是
|
<html><head><title>输入账号密码</title></head> <body> <form action="/deal_request" method="get"> 请输入账号:<input type="text" name="usr" ><br> 请输入密码:<input type="password" name="psd"><br> <input type="submit" value="提交" /> </form> </body></html> |
|
<html><head><title>Welcome</title></head> <body> <h1>登录成功!</h1><br> <h1>欢迎你: ① </h1><br> </body></html> |
编写的 Python 程序如下:
from flask import Flask, render_template, request
import sqlite3
② = Flask(_name_)
@app.route('/')
def input():
return render_template('input.html')
@app.route('/deal_request', methods = ['GET'])
def deal_request():
get_usr = request.args.get('usr')
get_psd = request.args.get('psd')
if check(get_usr, get_psd):
return render_template('succes.html', name=get_usr)
else:
return '用户名或密码错误!'
def check(name, psd):
db=sqlite3.connect('login.db')
cur= ③ #创建游标对象
cur.execute('select * from users')
data=cur.fetchall()
for rec in data: #比对用户名与密码
if rec[ 1]==name and rec[2]==psd:
return True
else:
return False
if _name_ == '_main_':
app.run(host=' 127.0.0. 1 ', port=5000, debug=False)
请完成下列题目:
②③