版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精品文档Visual Basic快速入门什么是Sub 过程同用对话框 n = Text2 Text3 = gcd(m, n) End Sub 方法二: Private Sub Command2_Click() '最小公倍数 Dim m As Integer, n As Integer m = Text1 n = Text2 Text4 = m * n / (gcd(m, n) End Sub Private Sub Command3_Click() Text1.Text = "" Text2.Text = "" Text3.Text = &quo
2、t;" Text4.Text = "" End Sub Private Sub Command4_Click() End End Sub Function gcd(m As Integer, n As Integer) '此过程并非递归,递归解法见课本P162 Dim k As Integer Do k = m Mod n m = n n = k Loop Until k = 0 gcd = m End Function 3 冒泡排序过程;书P107 Dim a(10) As Integer Private Sub sort(a() As Integer,
3、 n As Integer) Dim i As Integer, temp As Integer Dim switch As Boolean switch = True Do While switch switch = False n = n - 1 For i = 1 To n If a(i) > a(i + 1) Then switch = True temp = a(i) a(i) = a(i + 1) a(i + 1) = temp End If Next i Loop End Sub Private Sub Command1_Click() Dim i As Integer F
4、or i = 1 To 10 a(i) = Int(90 * Rnd) + 10 Text1 = Text1 & a(i) & " " Next i End Sub Private Sub Command2_Click() Dim i As Integer Call sort(a, 10) For i = 1 To 10 Text2 = Text2 & a(i) & " " Next i End Sub 4 顺序查找过程;书P146 1顺序查找法在一列数中查找某数x 根本思想:一列数放在数组a(1)-a(n)中,待查找的数
5、放在x 中,把x与a数组中的元素从头到尾一一进行比拟查找。用变量p表示a数组元素下标,p初值为1,使x与a(p)比拟,如果x不等于a(p),那么使p=p+1,不断重复这个过程;一旦x等于a(p)那么退出循环;另外,如果p大于数组长度,循环也应该停止。这个过程可由下语句实现 p = 1 Do While x <> a(p) And p < =n p = p + 1 Loop 下面写一查找函数Find,假设找到那么返回下标值,找不到返回0 Option Base 1 Private Function Find( a( ) As Single,x As Single) As Int
6、eger Dim n%,p% n=Ubound( a ) p = 1 Do While p < n And x <> a(p) p = p + 1 Loop If If p = n And a(p) <> x Then p = 0 Find=p End Function 方法二: Private Sub Command1_Click() Dim b(), k%, n% b = Array(1, 3, 5, 7, 9, 2, 4) k = Val(InputBox("输入要查找的关键值") Call search(b(), k, n) If n
7、>= 0 Then MsgBox ("找到的位置为" & n) Else MsgBox ("找不到") End Sub Sub search(a(), ByVal key%, ByRef index%) Dim i% For i = LBound(a) To UBound(a) If key = a(i) Then index = i Exit Sub End If Next i index = -1 End Sub 5 判断回文数过程;P140,4-4-3 Private Function huiwen(str As String) Di
8、m i As Integer, l As Integer str = Text1 l = Len(str) For i = 1 To l / 2 If Mid(str, i, 1) <> Mid(str, l - i + 1, 1) Then Label1.Caption = str & "不是回文数" Else Label1.Caption = str & "是回文数" End If Next i End Function Private Sub Command1_Click() Dim x As String huiwen
9、 (x) End Sub 6 递归函数求阶乘;书P137 Private Sub Command1_Click() Dim n As Integer n = Val(InputBox("请输入n以求n的阶乘") Print n & "!=" fac(n) End Sub Public Function fac(n As Integer) As Integer If n = 1 Then fac = 1 Else fac = n * fac(n - 1) End If End Function 7 递归函数求Fibonacci数列;求斐波拉契数列的
10、第n项 Private Sub Command1_Click() Text2.Text = fab(Val(Text1.Text) '注意参数 End Sub Private Sub Command2_Click() Text1.Text = "" Text2.Text = "" Command1.SetFocus End Sub Private Sub Command3_Click() End End Sub Function fab(n As Integer) As Integer If n = 1 Or n = 2 Then '注意
11、不能写成if n=1 or 2 fab = 1 Else fab = fab(n - 1) + fab(n - 2) '递归 End If End Function 8 矩阵靠边元素之和;P99 Option Explicit Private Sub Command1_Click() Dim n As Integer, i As Integer, j As Integer, all As Integer n = InputBox("请输入方阵的阶数:") ReDim a(n, n) As Integer Randomize For i = 1 To n For j
12、= 1 To n a(i, j) = Int(90 * Rnd) + 10 Text1.Text = Text1.Text & a(i, j) & " " Next j Text1.Text = Text1.Text & vbCrLf Next i all = 0 For i = 1 To n all = all + a(1, i) all = all + a(i, 1) all = all + a(n, i) all = all + a(i, n) Next i Text2 = "所有靠边元素之和" + Str(all - a(
13、1, 1) - a(1, n) - a(n, 1) - a(n, n) End Sub 9 矩阵不靠边元素之和; Option Explicit Private Sub Command1_Click() Dim n As Integer, i As Integer, j As Integer, all As Integer n = InputBox("请输入方阵的阶数:") ReDim a(n, n) As Integer Randomize For i = 1 To n For j = 1 To n a(i, j) = Int(90 * Rnd) + 10 Text1.Text = Text1.Text & a(i,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
评论
0/150
提交评论