现要求根据输入的字符串(长度在1000 以内),全部由数字和字母组成,得到大小写不敏感压缩后的结果(即所有小写字母均视为相应的大写字母)。如:原字符串为aAABBbBCCCaaaaa,压缩后的结果(A,3)(B,4)(C,3)(A,5)。
实现这一功能的程序代码如下:
Private Sub Command1_Click()
Dim s As String, s1 As String
Dim tmp As String, ans As String
Dim i As Integer, j As Integer
s = Text1.Text
s1 = ""
For i = 1 To Len(s)
s1 = s1 & ToUpcase( ① )
Next i
i = 1
Do While i <= Len(s1)
tmp = Mid(s1, i, 1)
ans = ans & "(" & tmp & ","
j = i + 1
Do While j <= Len(s1) And Mid(s1, j, 1) = tmp
j = j + 1
Loop
ans = ans & ② & ")"
③
Loop
Text2.Text = ans
End Sub
Function ToUpcase(c As String) As String
If c >= "a" And c <= "z" Then
ToUpcase = Chr(Asc(c) - 32)
Else
ToUpcase = c
End If
End Function
在画线处填入适当的语句或表达式,将程序补充完整。
①
相似题推荐
(1)答题卡通常使用红色,如使用黑色印刷,对答案识别
(2)请在划线处填入合适代码。①
x_start =11 #起始点坐标
y_start =92
fill_width =24 #信息点宽度
fill_height =10 #信息点高度
space_width =15 #间隔宽度
space_height=12 #间隔高度
ans_cnt =5 #题目个数
def bw_judge(R,G,B): #bw_judge用于判断一个像素的填涂情况
Gray sca1e=0.299*R+0.587*G+0.114*B
return Gray_scale 132
def fill_judge(x,y): #fil1_judge用于判断信息点的填涂情况count =0
for i in range(x,x+fill_width):
for j in range( ① ):
R,G,B=pixels[i,j]
if bw_judge(R,G,B)==True:
count =count+1
if count >fill_width * fill_height * 0.64:
return True
total_width = fill _width + space_widthtotal_height = fill_height+space_height
image=Image.open("card.bmp")
pixels=image.load()
ans = ""
item=['A','B','C','D']
list=[]
for col in range(ans_cnt):
x= ②
for row in range(4):
y=y_start + total_height * row
if fill_judge(x,y)=True:
③
list.append(ans)
ans=""
print(list)分析:在数学运算表达式中,运算符总是置于与之相关的两个运算对象之间,在计算结果时,要考虑括号、运算符号的优先性。为了程序实现的方便,波兰逻辑学家J.Lukasiewicz提出了另 一种表示法,将运算符置于其运算对象之后,没有括号,不用考虑运算符号的优先性。这种表达 式称为后缀表达式,又叫逆波兰表达式。
如表达式“682-2*3÷+”是四则运算式“6+ (8-2) *2÷3”的逆波兰表达式。为了处理方便, 规定表达式中的数均为小于10的正整数,运算符为+ - * ÷。
(1) 抽象建模
设计两个栈bds、fh,栈bds用来存放表达式,栈fh用来暂时存放运算符。从左往右扫描四则运算式,遇到数字时,入栈bds;遇到运算符号时,根据运算符号的优先级设计进栈与出栈。
四则运算式“6+ (8-2) *2÷3”转换规则的模拟过程如表 1 所示:
表1
结合表1的操作过程,用栈bds和栈fh记录每个操作后的栈内情况(见图),那么在操作2中栈fh里有内容为(请从栈底到栈顶顺序书写)(2)设计算法
基于问题的抽象与建模,解决该问题的主要算法描述如下:
从左往右遍历四则运算式s(设中间变量为 ch):
1) 当ch是数字,直接入栈bds;
2) 当ch是运算符:
a.若ch为左括号时,直接入栈fh;
b.若ch为右括号时, 则将栈fh元素弹出,压入栈bds,直到遇到左括号(左括号只弹出,不压入栈bds);
c.若ch为其它运算符时, 如果运算符ch优先级大于栈fh中栈顶元素的优先级(或栈fh为空),直接入栈fh;否则,将栈fh元素依次弹出,并压入栈bds,直到运算符ch优先级大于栈fh中栈顶元素的优先级(或栈fh为空);
3)将栈bds中元素依次出栈,即为该四则运算s的后缀表达式。
(3) 编写程序
实现上述功能的Python代码如下,请在划线处填入合适代码。
yxj = {"+":1,"-":1,"*":2,"÷":2} #运算规则的优先级
s = input("请输入四则运算式: ")
fh = [""]*100 #存储运算符
topfh = -1
bds = [""]*100 #存储表达式
top=-1
for ch in s:
if ch.isdigit(): #字符串只包含数字则返回 True 否则返回 False
top+=1
bds[top]=ch
elif ch == "(":
topfh +=1
fh[topfh]=ch
elif ch == ")":
while True:
tmp = fh[topfh]
topfh-=1
if tmp=="(":
top+=1
bds[top]=tmp
elif ch in yxj:
if topfh==-1 or fh[topfh]=="(":
topfh += 1
fh[topfh]=ch
elif
topfh+=1
fh[topfh]=ch
else:
while fh[topfh]!="(" and topfh!=-1:
if yxj[fh[topfh]]>=yxj[ch]:
top+=1
bds[top]=fh[topfh]
topfh-=1
else:
break
topfh+=1
top+=1
bds[top]=fh[topfh]
topfh-=1
print("后缀表达式:","".join(bds[:top+1]))(2)实现上述功能的 VB 程序如下,请在划线处填入合适代码。
Function calculate(x As Integer, y As Integer, op As String) As Integer If op = "+" Then calculate = x + y
If op = "-" Then calculate = x - y If op = "*" Then calculate = x * y If op = "\" Then calculate = x \ y
End Function
Function state(op As String) As Integer If op = "#" Then state = -1
If op = "=" Then state = 1
If op = "+" Then state = 1
If op = "-" Then state = 1
'①
If op = "*" Then state = 3
End Function
Private Sub Command1_Click()
Dim s As String, length As Integer, t As String Dim k As Integer, q As Integer
Dim opt(0 To 3) As String Dim i As Integer
Dim s1 As String
Dim a(1 To 3) As Integer s = Text1.Text
length = Len(s)
t = "": k = 0: q = 0: opt(q) = "#"
For i = 1 To length s1 = Mid(s, i, 1)
If s1 >= "0" And s1 <= "9" Then
t = t + s1
Else
k = k + 1 a(k) = Val(t) t = ""
Do While '②
a(k - 1) = calculate(a(k - 1), a(k), opt(q))
k = k - 1
q= q - 1
Loop
q = q + 1
'③
If s1 = "=" Then Label1.Caption = Str(a(1))
End If
Next i
End Sub