vb如何编程
答案:6 悬赏:60 手机版
解决时间 2021-03-26 10:32
- 提问者网友:心如荒岛囚我终老
- 2021-03-25 23:01
vb如何编程
最佳答案
- 五星知识达人网友:从此江山别
- 2021-03-25 23:45
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Dim S As String, S1 As String, S2 As String
Dim I As Integer, J As Integer
Dim G As Integer, Hw As Boolean, Find As Boolean
S = Text1.Text
Text1.Text = Text1 + vbCrLf
S1 = S
G = 0
Do While S1 <> ""
Find = False
For I = 1 To Len(S1)
If Mid(S1, I, 1) = " " Or I = Len(S1) Then
Find = True
S2 = Trim(Left(S1, I))
If I = Len(S1) Then S1 = "" Else S1 = Right(S1, Len(S1) - I)
For J = 1 To Len(S2) \ 2
If Mid(S2, J, 1) <> Mid(S2, Len(S2) - J + 1, 1) Then
Hw = False
Exit For
End If
Hw = True
Next
If Hw Then
G = G + 1
Text1.Text = Text1 + " " + S2
End If
End If
If Find Then Exit For
Next
Loop
Text1.Text = Text1.Text + vbCrLf + "共有" + Str(G) + "个回文串"
End If
End Sub
已经运行过了,没问题!即使串与串之间多输入几个空格也不影响查找!
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Dim S As String, S1 As String, S2 As String
Dim I As Integer, J As Integer
Dim G As Integer, Hw As Boolean, Find As Boolean
S = Text1.Text
Text1.Text = Text1 + vbCrLf
S1 = S
G = 0
Do While S1 <> ""
Find = False
For I = 1 To Len(S1)
If Mid(S1, I, 1) = " " Or I = Len(S1) Then
Find = True
S2 = Trim(Left(S1, I))
If I = Len(S1) Then S1 = "" Else S1 = Right(S1, Len(S1) - I)
For J = 1 To Len(S2) \ 2
If Mid(S2, J, 1) <> Mid(S2, Len(S2) - J + 1, 1) Then
Hw = False
Exit For
End If
Hw = True
Next
If Hw Then
G = G + 1
Text1.Text = Text1 + " " + S2
End If
End If
If Find Then Exit For
Next
Loop
Text1.Text = Text1.Text + vbCrLf + "共有" + Str(G) + "个回文串"
End If
End Sub
已经运行过了,没问题!即使串与串之间多输入几个空格也不影响查找!
全部回答
- 1楼网友:由着我着迷
- 2021-03-26 02:59
Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strArry As Variant
Dim i As Integer, revCount As Integer
If KeyAscii <> 13 Then Exit Sub
KeyAscii = 0
Text1.Text = Trim(Text1.Text)
strArry = Split(Text1.Text, " ") '以空格为分割符,对text进行分割,成为字符串数组
For i = 0 To UBound(strArry)
If IsRevStr(strArry(i)) = True Then
revCount = revCount + 1
End If
Next i
MsgBox "回文串数目:" & revCount
End Sub
Private Function IsRevStr(ByVal str As String)'检查一个字符串是否属于回文串
Dim i As Integer
Dim nLen As Integer
nLen = Len(str)
For i = 1 To nLen / 2
If Mid(str, i, 1) <> Mid(str, nLen - i + 1, 1) Then Exit Function '分别从两边开始逐个字符对比,如有不同,则判定不是回文串. 循环只要进行到字符的一半即可.
Next i
IsRevStr = True
End Function
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim strArry As Variant
Dim i As Integer, revCount As Integer
If KeyAscii <> 13 Then Exit Sub
KeyAscii = 0
Text1.Text = Trim(Text1.Text)
strArry = Split(Text1.Text, " ") '以空格为分割符,对text进行分割,成为字符串数组
For i = 0 To UBound(strArry)
If IsRevStr(strArry(i)) = True Then
revCount = revCount + 1
End If
Next i
MsgBox "回文串数目:" & revCount
End Sub
Private Function IsRevStr(ByVal str As String)'检查一个字符串是否属于回文串
Dim i As Integer
Dim nLen As Integer
nLen = Len(str)
For i = 1 To nLen / 2
If Mid(str, i, 1) <> Mid(str, nLen - i + 1, 1) Then Exit Function '分别从两边开始逐个字符对比,如有不同,则判定不是回文串. 循环只要进行到字符的一半即可.
Next i
IsRevStr = True
End Function
- 2楼网友:人间朝暮
- 2021-03-26 02:29
拉1个文本和1个label,名字分别叫text1和label1,然后输入以下代码:
Dim a As Integer
Dim k As Integer
Dim i As Integer
Private Sub Form_Load()
k = 0
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Trim (a)
For i = 0 To Len(a) Step 1
If Left(a, i) = Right(a, i) Then
k = k + 1
End If
Next i
Label1.Caption = "回文串有" & k & "个"
End If
End Sub
其中运用到的几个函数我说明下:
Trim 清除2边空白字符
left 返回左边第多少个字符
right 返回右边第多少个字符
len 计算字符长度
我试过可以了,希望你也可以!
Dim a As Integer
Dim k As Integer
Dim i As Integer
Private Sub Form_Load()
k = 0
End Sub
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyReturn Then
Trim (a)
For i = 0 To Len(a) Step 1
If Left(a, i) = Right(a, i) Then
k = k + 1
End If
Next i
Label1.Caption = "回文串有" & k & "个"
End If
End Sub
其中运用到的几个函数我说明下:
Trim 清除2边空白字符
left 返回左边第多少个字符
right 返回右边第多少个字符
len 计算字符长度
我试过可以了,希望你也可以!
- 3楼网友:老鼠爱大米
- 2021-03-26 00:46
添加一个Text和Command控件,名称默认,代码如下
先在文本框内输入字符串,再点击Command1
Private Sub Command1_Click()
Dim tStr$(), MaxN%, Cyc%, NCyc%, RecNum%
tStr = Split(Text1.Text, " ")
MaxN = UBound(tStr)
For Cyc = 0 To MaxN - 1
For NCyc = Cyc + 1 To MaxN
If tStr(Cyc) = StrReverse(tStr(NCyc)) Then RecNum = RecNum + 1
Next
Next
MsgBox "回文数量共:" & RecNum & "个"
End Sub
先在文本框内输入字符串,再点击Command1
Private Sub Command1_Click()
Dim tStr$(), MaxN%, Cyc%, NCyc%, RecNum%
tStr = Split(Text1.Text, " ")
MaxN = UBound(tStr)
For Cyc = 0 To MaxN - 1
For NCyc = Cyc + 1 To MaxN
If tStr(Cyc) = StrReverse(tStr(NCyc)) Then RecNum = RecNum + 1
Next
Next
MsgBox "回文数量共:" & RecNum & "个"
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯