vb典型程序示例
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
Text1 = Text1 & Str(Element) Next Element End Sub 13、编写程序实现二分查找功能。
If sort(I) > sort(j) Then temp = sort(I) sort(I) = sort(j) sort(j) = temp
End If Next j Text2 = Text2 & Str(sort(I)) Next I Text2 = Text2 & Str(sort(I)) End Sub 12、编写程序实现顺序查找的功能。
Option Explicit Option Base 1 Dim search As Variant Private Sub Command2_Click() Dim I As Integer, find As Integer Text2 = "" find = InputBox("输入要查找的数") For I = 1 To UBound(search)
Do c = (a + b) / 2 If f(a) * f(c) > 0 Then a=c Else b=c End If
Loop Until Abs(b - c) < 0.000001 And f(b) * f(c) < 0 End If Print c End Sub 9、找出100以内的所有素数,存放在数组Prime 中,并将所找到的素数按每行10个 的形式显示在窗体上。
End If Next I Text2.Text = source Text3.Text = source D = "1" For I = Len(source) To 2 Step -1
If Mid(source, I, 1) = "1" And D = "1" Then Mid(source, I, 1) = "0" D = "1"
Text2.Text = source Text3.Text = source Else For I = Len(source) To 2 Step -1
If Mid(source, I, 1) = "1" Then Mid(source, IHale Waihona Puke Baidu 1) = "0"
Else Mid(source, I, 1) = "1"
s = CStr(t Mod 2) + s t=t\2 Loop Text2.Text = s End Sub 7、编写程序,求出100之内的所有勾股数。所谓勾股数,是指满足条件a^2+b^2=c^2 (a <>b)的自然数。 Private Sub Command1_Click() Dim a As Integer Dim b As Integer Dim c As Integer For a = 1 To 100 For b = a + 1 To 100
Option Explicit Private Sub command1_click() Dim source As String, I As Integer Dim D As String * 1 source = Text1.Text If Mid(source, 1, 1) <> "1" Then
Else Do r = m Mod n m=n n=r Loop Until r = 0 Text3.Text = CStr(m)
End If End Sub
2、从由字母、数字组成的字符串中找出所有大写字母并逆序输出的程序 Private Sub Command1_Click() Dim s As String, d As String, t As String Dim i As Integer Text1.SetFocus s = Text1.Text For i = 1 To Len(s) If Mid(s, i, 1) >= "A" And Mid(s, i, 1) <= "Z" Then t = t & Mid(s, i, 1) End If Next i For i = Len(t) To 1 Step -1 d = d & Mid(t, i, 1) Next i Text2.Text = d End Sub 3、编写程序,找出所有三位水仙花数。所谓水仙花,是指各位数字的立方和等于该 数本身的数。例如,153=1^3+5^3+3^3,,所以153是一个水仙花数。 Option Explicit Private Sub command1_click() Dim I As Integer, a As Integer, b As Integer, c As Integer Dim st As String For a = 1 To 9 For b = 0 To 9 For c = 0 To 9
Next j End Sub 10、随即生成10个两两互质的数,并按从小到大的顺序存放在listbox中.。 Option Explicit Private Sub command1_click() Dim p As Integer, I As Integer, idx As Integer Dim j As Integer List1.List(0) = Int(Rnd * (9999 - 1000)) + 1000 Do
For k = 2 To Sqr(I) If I Mod k = 0 Then Exit For
Next k If k > Sqr(I) Then
m=m+1 prime(m) = I End If Next I k=0
For j = 1 To m k=k+1 Print prime(j); If k Mod 10 = 0 Then Print
ElseIf Mid(source, I, 1) = "0" And D = "1" Then Mid(source, I, 1) = "1" D = "0"
End If Next I Text3.Text = source End If End Sub 5、编写程序,随机生成100个两位整数,并统计出其中小于等于40、大于40小于等 于70及大于70的数据个数。 Private Sub command1_click() Dim I As Integer Dim s As Integer Dim s1 As Integer Dim s2 As Integer
1、设计采用欧几里德算法求解两个自然数的最大公约数的程序。
Private Sub command1_click() Dim m As Long, n As Long
Dim r As Long m = Val(Text1.Text) n = Val(Text2.Text) If m < 1 Or n < 1 Then Text3.Text = "数据错误"
For c = b + 1 To 100 If a ^ 2 + b ^ 2 = c ^ 2 Then List1.AddItem CStr(c) & "^2=" & CStr(a) & "^2+" & CStr(b) &
"^2" End If
Next c Next b
Next a End Sub 8、设计一个用二分法求方程x^3-x^4+4x^2-1=0在区间【0.,1】上的一个实根。算法 提示:若方程f(x)=0在区间【a,b】上有一个实根,则f(a)与f(b)必然异号,即f(a)*f(b)<0; 设c= (a+b)/2,若f(a)*f(c)>0,则令a=c,否则令b=c。当b-c的绝对值小于或等于给定误差 要求时,则c就是要求的根。
p = Int(Rnd * (9999 - 1000)) + 1000 For I = 0 To List1.ListCount - 1
For j = 2 To p If p Mod j = 0 And List1.List(I) Mod j = 0 Then Exit For End If
Next j If j <= p Then Exit For Next I If I > List1.ListCount - 1 Then idx = 0 Do While p < List1.List(idx)
idx = idx + 1 If idx > List1.ListCount - 1 Then Exit Do Loop List1.AddItem p, idx End If Loop Until List1.ListCount = 10 End Sub 11、随机生成10个1-99的整数,用选择法对10个数进行排序。 Option Explicit Option Base 1 Private Sub cmdsort_click() Dim sort(10) As Integer, temp As Integer Dim I As Integer, j As Integer Randomize For I = 1 To 10 sort(I) = Int(Rnd * (100 - 1)) + 1 Text1 = Text1 & Str(sort(I)) Next I For I = 1 To 9 For j = I + 1 To 10
If search(I) = find Then Exit For Next I If I <= UBound(search) Then
Text2 = "要查找的数" & Str(search(I)) & " 是 search(" & Str(I) & ")" Else
Text2 = "在数列中没有找到" & Str(find) End If End Sub Private Sub Command1_Click() Dim I As Integer, Element As Variant search = Array(34, 12, 56, 81, 74, 59, 83, 91, 26) For Each Element In search
Dim s3 As Integer For I = 1 To 100
s = Int(Rnd * 90 + 10) Select Case s
Case Is <= 40 s1 = s1 + 1
Case Is <= 70 s2 = s2 + 1
Case Else s3 = s3 + 1
End Select Next I Text1.Text = CStr(s1) Text2.Text = CStr(s2) Text3.Text = CStr(s3) End Sub 6、编写程序,输入正整数n, 求其对应的二进制数。 Private Sub command1_click() Dim s As String Dim t As Integer t = Val(Text1.Text) Do While t <> 0
I = a * 100 + b * 10 + c If I = a ^ 3 + b ^ 3 + c ^ 3 Then
st = I & "=" & a & "^3+" & b & "^3+" & c & "^3"
List1.AddItem st End If Next c Next b Next a End Sub 4、将一个二进制数原码转换成补码。
Private Function f(x As Single) As Double f=x^3-x^4+4*x^2-1 End Function Private Sub Command1_Click() Dim a As Single ' Dim b As Single Dim c As Single a = 0: b = 1 If f(a) * f(b) < 0 Then
Option Base 1 Option Explicit Private Sub form_click() Dim prime(50) As Integer, I As Integer Dim k As Integer, m As Integer, j As Integer prime(1) = 2 m=1 For I = 3 To 99 Step 2
If sort(I) > sort(j) Then temp = sort(I) sort(I) = sort(j) sort(j) = temp
End If Next j Text2 = Text2 & Str(sort(I)) Next I Text2 = Text2 & Str(sort(I)) End Sub 12、编写程序实现顺序查找的功能。
Option Explicit Option Base 1 Dim search As Variant Private Sub Command2_Click() Dim I As Integer, find As Integer Text2 = "" find = InputBox("输入要查找的数") For I = 1 To UBound(search)
Do c = (a + b) / 2 If f(a) * f(c) > 0 Then a=c Else b=c End If
Loop Until Abs(b - c) < 0.000001 And f(b) * f(c) < 0 End If Print c End Sub 9、找出100以内的所有素数,存放在数组Prime 中,并将所找到的素数按每行10个 的形式显示在窗体上。
End If Next I Text2.Text = source Text3.Text = source D = "1" For I = Len(source) To 2 Step -1
If Mid(source, I, 1) = "1" And D = "1" Then Mid(source, I, 1) = "0" D = "1"
Text2.Text = source Text3.Text = source Else For I = Len(source) To 2 Step -1
If Mid(source, I, 1) = "1" Then Mid(source, IHale Waihona Puke Baidu 1) = "0"
Else Mid(source, I, 1) = "1"
s = CStr(t Mod 2) + s t=t\2 Loop Text2.Text = s End Sub 7、编写程序,求出100之内的所有勾股数。所谓勾股数,是指满足条件a^2+b^2=c^2 (a <>b)的自然数。 Private Sub Command1_Click() Dim a As Integer Dim b As Integer Dim c As Integer For a = 1 To 100 For b = a + 1 To 100
Option Explicit Private Sub command1_click() Dim source As String, I As Integer Dim D As String * 1 source = Text1.Text If Mid(source, 1, 1) <> "1" Then
Else Do r = m Mod n m=n n=r Loop Until r = 0 Text3.Text = CStr(m)
End If End Sub
2、从由字母、数字组成的字符串中找出所有大写字母并逆序输出的程序 Private Sub Command1_Click() Dim s As String, d As String, t As String Dim i As Integer Text1.SetFocus s = Text1.Text For i = 1 To Len(s) If Mid(s, i, 1) >= "A" And Mid(s, i, 1) <= "Z" Then t = t & Mid(s, i, 1) End If Next i For i = Len(t) To 1 Step -1 d = d & Mid(t, i, 1) Next i Text2.Text = d End Sub 3、编写程序,找出所有三位水仙花数。所谓水仙花,是指各位数字的立方和等于该 数本身的数。例如,153=1^3+5^3+3^3,,所以153是一个水仙花数。 Option Explicit Private Sub command1_click() Dim I As Integer, a As Integer, b As Integer, c As Integer Dim st As String For a = 1 To 9 For b = 0 To 9 For c = 0 To 9
Next j End Sub 10、随即生成10个两两互质的数,并按从小到大的顺序存放在listbox中.。 Option Explicit Private Sub command1_click() Dim p As Integer, I As Integer, idx As Integer Dim j As Integer List1.List(0) = Int(Rnd * (9999 - 1000)) + 1000 Do
For k = 2 To Sqr(I) If I Mod k = 0 Then Exit For
Next k If k > Sqr(I) Then
m=m+1 prime(m) = I End If Next I k=0
For j = 1 To m k=k+1 Print prime(j); If k Mod 10 = 0 Then Print
ElseIf Mid(source, I, 1) = "0" And D = "1" Then Mid(source, I, 1) = "1" D = "0"
End If Next I Text3.Text = source End If End Sub 5、编写程序,随机生成100个两位整数,并统计出其中小于等于40、大于40小于等 于70及大于70的数据个数。 Private Sub command1_click() Dim I As Integer Dim s As Integer Dim s1 As Integer Dim s2 As Integer
1、设计采用欧几里德算法求解两个自然数的最大公约数的程序。
Private Sub command1_click() Dim m As Long, n As Long
Dim r As Long m = Val(Text1.Text) n = Val(Text2.Text) If m < 1 Or n < 1 Then Text3.Text = "数据错误"
For c = b + 1 To 100 If a ^ 2 + b ^ 2 = c ^ 2 Then List1.AddItem CStr(c) & "^2=" & CStr(a) & "^2+" & CStr(b) &
"^2" End If
Next c Next b
Next a End Sub 8、设计一个用二分法求方程x^3-x^4+4x^2-1=0在区间【0.,1】上的一个实根。算法 提示:若方程f(x)=0在区间【a,b】上有一个实根,则f(a)与f(b)必然异号,即f(a)*f(b)<0; 设c= (a+b)/2,若f(a)*f(c)>0,则令a=c,否则令b=c。当b-c的绝对值小于或等于给定误差 要求时,则c就是要求的根。
p = Int(Rnd * (9999 - 1000)) + 1000 For I = 0 To List1.ListCount - 1
For j = 2 To p If p Mod j = 0 And List1.List(I) Mod j = 0 Then Exit For End If
Next j If j <= p Then Exit For Next I If I > List1.ListCount - 1 Then idx = 0 Do While p < List1.List(idx)
idx = idx + 1 If idx > List1.ListCount - 1 Then Exit Do Loop List1.AddItem p, idx End If Loop Until List1.ListCount = 10 End Sub 11、随机生成10个1-99的整数,用选择法对10个数进行排序。 Option Explicit Option Base 1 Private Sub cmdsort_click() Dim sort(10) As Integer, temp As Integer Dim I As Integer, j As Integer Randomize For I = 1 To 10 sort(I) = Int(Rnd * (100 - 1)) + 1 Text1 = Text1 & Str(sort(I)) Next I For I = 1 To 9 For j = I + 1 To 10
If search(I) = find Then Exit For Next I If I <= UBound(search) Then
Text2 = "要查找的数" & Str(search(I)) & " 是 search(" & Str(I) & ")" Else
Text2 = "在数列中没有找到" & Str(find) End If End Sub Private Sub Command1_Click() Dim I As Integer, Element As Variant search = Array(34, 12, 56, 81, 74, 59, 83, 91, 26) For Each Element In search
Dim s3 As Integer For I = 1 To 100
s = Int(Rnd * 90 + 10) Select Case s
Case Is <= 40 s1 = s1 + 1
Case Is <= 70 s2 = s2 + 1
Case Else s3 = s3 + 1
End Select Next I Text1.Text = CStr(s1) Text2.Text = CStr(s2) Text3.Text = CStr(s3) End Sub 6、编写程序,输入正整数n, 求其对应的二进制数。 Private Sub command1_click() Dim s As String Dim t As Integer t = Val(Text1.Text) Do While t <> 0
I = a * 100 + b * 10 + c If I = a ^ 3 + b ^ 3 + c ^ 3 Then
st = I & "=" & a & "^3+" & b & "^3+" & c & "^3"
List1.AddItem st End If Next c Next b Next a End Sub 4、将一个二进制数原码转换成补码。
Private Function f(x As Single) As Double f=x^3-x^4+4*x^2-1 End Function Private Sub Command1_Click() Dim a As Single ' Dim b As Single Dim c As Single a = 0: b = 1 If f(a) * f(b) < 0 Then
Option Base 1 Option Explicit Private Sub form_click() Dim prime(50) As Integer, I As Integer Dim k As Integer, m As Integer, j As Integer prime(1) = 2 m=1 For I = 3 To 99 Step 2