资源描述
1 站
Private Sub Command1_Click()
Dim os(1 To 10) As String, ns(1 To 10) As String '数组os是符号栈,'ns是数值栈
Dim topo As Integer, topn As Integer
Dim w, f As Variant, t, q As Variant
Dim wn As Integer, qn As Integer
topo = 1
topn = 1
t = 0
n = 1
os(topo) = ";"
Do While t <> 2
s = Trim(Text1.Text) '去空格,读取表达式
If s = "" Then
s = MsgBox("错误,请输入数据", , "错误提示"): Exit Do
End If
If Mid(s, Len(s), 1) <> ";" Then
s = MsgBox("错误,请在表达式结尾加上分号", , "错误提示"): Exit Do
End If
If t = 0 Then
w = Mid(s, n, 1) '这是本程序的缺陷,只能实现个位数的运算
n = n + 1
End If '实现表达式中字符的逐一分离
If Not IsNumeric(w) Then '识别表达式中的符号
Select Case w
Case ";"
wn = 0
Case "-", "+"
wn = 1
Case "/", "*"
wn = 2
Case "^"
wn = 3
End Select
q = os(topo)
Select Case q '定义符号的优先级
Case ";"
qn = 0
Case "-", "+"
qn = 1
Case "/", "*"
qn = 2
Case "^"
qn = 3
End Select
If wn > qn Then '入栈
topo = topo + 1
os(topo) = w
t = 0
ElseIf q = ";" And w = ";" Then
z = ns(topn)
t = 2
Else
x = Val(ns(topn))
topn = topn - 1
y = Val(ns(topn))
topn = topn - 1
q = os(topo)
topo = topo - 1
Select Case q '出栈运算
Case "-"
f = y - x
Case "+"
f = y + x
Case "*"
f = y * x
Case "/"
f = y / x
Case "^"
f = y ^ x
End Select
topn = topn + 1
ns(topn) = f '运算的临时结果入栈
t = 1
End If
Else
topn = topn + 1
ns(topn) = w
End If
Loop
Text2.Text = z
Text1.SetFocus
End Sub
Private Sub Command2_Click()
Text1 = ""
Text2 = ""
Text1.SetFocus
End Sub
Private Sub Form_Load()
Text1 = ""
Text2 = ""
End Sub
2.冒泡
Option Base 1
Dim a() As Variant, f As Integer
Private Sub Command1_Click()
a = Array(8, 5, 6, 3, 5, 9, 10, 2, 1, 4) '8、5、6、3、5、9、10、2、1、4
Picture1.Print "排列前的数组为:"
For i = 1 To UBound(a)
Picture1.Print a(i);
Next i
f = UBound(a)
Do While f > 0
k = f - 1: f = 0
For j = 1 To k
If a(j) >= a(j + 1) Then
T = a(j): a(j) = a(j + 1): a(j + 1) = T: f = j
End If
Next j
Loop
Picture1.Print
Picture1.Print "排列后的数组为:"
For i = 1 To UBound(a)
Picture1.Print a(i);
Next i
Picture1.Print
End Sub
Private Sub Command2_Click()
Picture1.Cls
End Sub
3.对半
Dim r() As Variant
Private Sub Command1_Click()
Dim i, j, low, high, m As Integer
r = Array(0, 8, 5, 6, 3, 5, 9, 10, 2, 1, 4)
For i = 2 To 10
r(0) = r(i): low = 1: high = i - 1
Do While high >= low
m = (high + low) \ 2
If r(0) < r(m) Then
high = m - 1
Else
low = m + 1
End If
Loop
For j = i - 1 To low Step -1
r(j + 1) = r(j)
Next j
r(low) = r(0)
Next i
For i = 1 To 10
Picture1.Print Str(r(i));
Next i
Picture1.Print
Command2.Visible = True
End Sub
Private Sub Command2_Click()
Dim low, high, m, zhao As Integer, c1 As Boolean, c2 As Boolean
low = 1: high = 10
zhao = Val(InputBox("输入要查找的数", "输入", "", 100, 100))
Do While high > low
m = (high + low) \ 2
If r(m) = zhao Then
Picture1.Print "位置为:" & Str(m)
Picture1.Print
c1 = checkhigh(m): c2 = checklow(m)
If c1 Then
m = m + 1: Call checkhigh(m)
End If
If c2 Then
m = m - 1: Call checklow(m)
End If
Exit Do
ElseIf r(m) < zhao Then
low = m + 1
Else
high = m - 1
End If
Loop
If high <= low Then Picture1.Print "没找到"
End Sub
Private Function checkhigh(ByVal m As Integer) As Boolean
If r(m + 1) = 5 And m + 1 <= 10 Then
Picture1.Print "下个位置为:" & Str(m + 1)
checkhigh = True
Else
checkhigh = False
Exit Function
End If
End Function
Private Function checklow(ByVal m As Integer) As Boolean
If r(m - 1) = 5 And m - 1 > 0 Then
m = m - 1
Picture1.Print Str(m - 1)
checklow = True
Else
checklow = False
Exit Function
End If
End Function
展开阅读全文