Vb源代码
(周至县第五中学 710402 数学组 唐永鸽)
1 Private Sub Form_Click()
Dim n As Integer, f1 As Double, f2 As Double, f3 As Double Dim i As Integer
n = Val(InputBox(\输入要输出的项数:\输出斐波那契数列\ f1 = 0 f2 = 1
Print Tab(8); f1 Print Tab(8); f2 For i = 3 To n f3 = f2 + f1 Print Tab(8); f3 f1 = f2 f2 = f3 Next End Sub
2 Private Sub Form_Click() Dim n As Integer, f1 As Double n = 1
For n = 1 To 66 f1 = 15 * n Print f1 Next End Sub
3 Private Sub Form_Click() Dim a1 As Double, n As Double, i As Integer
a1 = Val(InputBox(\请输入第1个数\找最大数\Print Tab(5); a1 For i = 2 To 5
n = Val(InputBox(\请输入第\个数\Print Tab(5); n
If n > a1 Then a1 = n Next Print \
Print \最大数为\End Sub
4、 Private Sub Form_Click() Dim year As Long
1
year = InputBox(\请输入年份\ If year Mod 4 = 0 Then
If year Mod 100 = 0 Then If year Mod 400 = 0 Then Print year; \年是闰年\ Else
Print year; \不年是闰年\ End If Else
Print year; \年是闰年\ End If Else
Print year; \不年是闰年\End If End Sub
5、 Private Sub form_Click() Dim year As Long
For year = 2000 To 2500 If year Mod 4 = 0 Then
If year Mod 100 = 0 Then If year Mod 400 = 0 Then Print year; \年是闰年\ Else
Print year; \年不是闰年\ End If Else
Print year; \年是闰年\ End If Else
Print year; \年不是闰年\ End If Next End Sub
6、Option Base 1
Private Sub Form_Click()
Const n = 10
Dim a(n) As Single Max = 0 k = 0
2
For i = 1 To n
a(i) = Val(InputBox(\请输入第\个数\找最大数\Print Tab(5); \第\个数为:\Next i
For i = 1 To n If Max < a(i) Then Max = a(i) k = i End If Next Print
Print \最大数为第\个数\End Sub
7 、 方法1、 Private Sub Form_Click()
Dim n As Integer, f1 As Integer, f2 As Integer, f3 As Integer For n = 100 To 999
f1 = Int(n / 100)
f2 = Int(n / 10) Mod 10 f3 = n Mod 10
If f1 ^ 3 + f2 ^ 3 + f3 ^ 3 = n Then Print n; \为水仙花数\ End If Next End Sub
方法2、Private Sub Form_Click() Dim n As Integer, f1 As Integer, f2 As Integer, f3 As Integer n = 100 Do While n <= 999 f1 = Int(n / 100) f2 = Int(n / 10) Mod 10 f3 = n Mod 10 If f1 ^ 3 + f2 ^ 3 + f3 ^ 3 = n Then Print n; \为水仙花数\ End If
n = n + 1 Loop End Sub
8 、 Private Sub Form_Click() Dim n As Integer, s As Integer n = 1 s = 0
3
Do
s = s + n n = n + 1
Loop While s <= 200 n = n - 2 Print n End Sub
9、二分法 方法1、
Public Function f(x As Double) As Double f = x ^ 3 - x - 1 End Function
Private Sub command1_Click()
Dim a As Double, b As Double, p As Single a = 1 b = 1.5 p = 0.00001 Do
If f((a + b) / 2) = 0 Then Exit Do Else
If f(a) * f((a + b) / 2) < 0 Then b = (a + b) / 2 Else a = (a + b) / 2 End If End If Loop While b - a >= p Label1.Caption = \方程的根是\End Sub
Private Sub Command2_Click()
Label2.Caption = \求方程 f = x ^ 3 - x - 1的近似解\End Sub
方法2、
Private Sub Command1_Click() a = Val(Text1.Text) b = Val(Text2.Text) e = Val(Text3.Text)
4
fa = (a * a - 1) * a - 1 fb = (b * b - 1) * b - 1 If fa * fb >= 0 Then
Text4.Text = \求解范围错!\Else Do
x = (a + b) / 2
f = (x * x - 1) * x - 1 If f * fa > 0 Then a = x: fa = f Else
b = x: fb = f End If
Loop Until f = 0 Or Abs(b - a) < e Text4.Text = (a + b) / 2 End If End Sub
10、冒泡排序
Private Sub Command1_Click() Dim C(6) As Double For n = 1 To 6
C(n) = InputBox(\输入需要排序的数\个数的冒泡排序\Next n
For i = 1 To 6
For j = 6 To i Step -1 If C(j) < C(j - 1) Then a = C(j): C(j) = C(j - 1): C(j - 1) = a End If Next j Next i For i = 1 To 6 Print \Next i End Sub
11、求12+22+32+…+1002的算法 Private Sub Form_Load() Dim n As Long, s As Long
n = InputBox(\请输入 n:\从1到n求平方和\s = 0 i = 1
While i <= n s = s + i * i
5