当前位置:文档之家› vb程序设计的几个简单例子

vb程序设计的几个简单例子

1
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
2
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
3
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

4
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

5
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

相关主题