vb语言程序设计简介
以vb程序设计语言为工具,对特定的问题进行分析,并设计出解题的算法,根据得到的算法,用vb语言编写出源程序的过程就是vb程序设计。
编写计算机程序需要使用计算机编程语言。计算机编程语言是人和计算机“对话”的桥梁。就像人类的语言一样,计算机编程语言也有很多。目前较为广泛使用的语言有“C++++”、“Pascal”、“Basic”等。在众多的计算机编程语言中,以BASIC语言最为易学易用。
BASIC是英文Beginner’s All purpose Symbolic Instruction Code的缩写,意思为初学者通用符号指令代码,一直都是程序设计入门的首选语言。
1991年,Visual Basic(简称VB)面世,他是第三代BASIC语言,它不但秉承了BASIC语言的易学易用的优点,而且增加了图形界面设计工具。它简化了复杂的窗口程序编写过程,让编程者将更多的精力致力于问题的求解过程。
vb语言程序设计实例(一)
Option Explicit Private Sub Command1_Click() Dim i%, j%, r! i = Val(Text1.Text) j = Val(Text2.Text) Picture1.Print i; j; Call gysh(i, j) Picture1.Print “最大公约数是” & i r = Val(InputBox(“输入半径”)) Call S(r) End Sub Public Function gysh(m As Integer, n As Integer) As Integer Dim t% Do t = m Mod n: m = n: n = t Loop While t 《》 0 End Function Public function S(r!) Const pi = 3.1415926 S = pi * r ^ 2 MsgBox “圆面积为:” & S End function Public Function 定积分() End Function
vb语言程序设计实例(二)
Dim a() As Integer, i%, j%, n%, t% Private Sub Command1_Click() Picture1.Print “排序前的数组为:” n = Val(InputBox(“请输入一个 3 到 100 的整数”)) ReDim a(1 To n) Randomize For i = 1 To n a(i) = Int(Rnd * 100) + 1 Picture1.Print Tab(((i - 1) Mod 5) * 6); a(i); Next i End Sub Private Sub Command2_Click() Picture1.Print Picture1.Print “排序后的数组为:” For i = 1 To n - 1 For j = i + 1 To n If a(i) 》 a(j) Then t = a(i) a(i) = a(j) a(j) = t End If Next Next For i = 1 To n Picture1.Print Tab(((i - 1) Mod 5) * 6); a(i); Next End Sub
vb语言程序设计实例(三)
Dim a(), i%, j% Private Sub Command1_Click() Dim n% n = Val(InputBox(“请输入一个整数”)) Randomize ReDim a(n) For i = 1 To n a(i) = Int(Rnd * 100) + 1 Next i Picture1.Print “选择法排序后的数组为:” For i = 1 To n - 1 For j = i + 1 To n If a(i) 》 a(j) Then t = a(i): a(i) = a(j): a(j) = t End If Next Next For i = 1 To n Picture1.Print Tab(((i - 1) Mod 5) * 6); a(i); Next Picture1.Print End Sub Private Sub Command2_Click() Dim n% n = Val(InputBox(“请输入一个整数”)) Randomize ReDim a(n) For i = 1 To n a(i) = Int(Rnd * 100) + 1 Next i Picture2.Print “冒泡排序法后的数组为:” For i = 1 To n-1 For j = n To i-1 Step -1 If a(j-1) 》 a(j) Then t = a(j): a(j) = a(j-1): a(j-1) = t End If Next Next For i = 1 To n
Picture2.Print Tab(((i - 1) Mod 5) * 6); a(i); Next Picture2.Print End Sub
vb语言程序设计实例(四)
Dim a(), i%, j%, n%, m%, t% Private Sub Command1_Click() n = Val(InputBox(“请输入一个整数”)) Call fuzhi(n) Picture1.Print “选择排序法后的数组为:” Call paixu(n) Picture1.Print ReDim Preserve a(n) End Sub Private Sub Command2_Click() m = Val(InputBox(“请输入一个整数”)) Call fuzhi(m) Picture1.Print “冒泡排序法后的数组为:” For i = 1 To m - 1 For j = m To i + 1 Step -1 If a(j - 1) 》 a(j) Then t = a(j - 1): a(j - 1) = a(j): a(j) = t End If Next Next For i = 1 To m Picture1.Print Tab(((i - 1) Mod 5) * 6); a(i); Next Picture1.Print End Sub Private Sub Command3_Click() ReDim a(t) Picture1.Print “合并后的数组为:” Call paixu(t)
Picture1.Print End Sub Public Sub fuzhi(n%) Randomize ReDim a(n) For i = 1 To n a(i) = Int(Rnd * 100) + 1 Next i End Sub Public Function paixu(n%) As Integer For i = 1 To n - 1 For j = i + 1 To n If a(i) 》 a(j) Then t = a(i): a(i) = a(j): a(j) = t End If Next Next For i = 1 To n Picture1.Print Tab(((i - 1) Mod 5) * 6); a(i); Next End Function
vb语言程序设计实例(五)
Dim i%, j%, s% Private Sub Command1_Click() Picture1.Cls Picture1.Scale (0, 0)-(10, 10) For i = 1 To 9 Picture1.Line (1, i)-(9, i) Picture1.Line (i, 1)-(i, 9) Next Picture1.FillColor = QBColor(0) Picture1.Circle (4.5, 4.5), 0.4 Picture1.Circle (5.5, 5.5), 0.4 Picture1.FillColor = QBColor(15) Picture1.Circle (4.5, 5.5), 0.4
Picture1.Circle (5.5, 4.5), 0.4 End Sub Private Sub Command2_Click() s = Abs(s - 1) End Sub Private Sub picture1_mousedown(botton As Integer, shift As Integer, x!, y!) i = Fix(x) j = Fix(y) If s = 0 Then Picture1.FillColor = QBColor(0) If s = 1 Then Picture1.FillColor = QBColor(15) Picture1.Circle (i + 0.5, j + 0.5), 0.4 s = Abs(s - 1) End Sub