VB典型程序!
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 = "数据错误"
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
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、将一个二进制数原码转换成补码。
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
Text2.Text = source
Text3.Text = source
Else
For I = Len(source) To 2 Step -1
If Mid(source, I, 1) = "1" Then
Mid(source, I, 1) = "0"
Else
Mid(source, I, 1) = "1"
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"
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
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
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
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就是要求的根。
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
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个的形式显示在窗体上。
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
For k = 2 To Sqr(I)
If I Mod k = 0 Then Exit For
Next k
If k > Sqr(I) Then大一vb程序设计基础
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
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
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))

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。