《VB经典例题13道(附答案).doc》由会员分享,可在线阅读,更多相关《VB经典例题13道(附答案).doc(7页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1.一串字符求有多少字母,有多少数字,有多少其他Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, c, q, w, r s = TextBox1.Text For i = 1 To Len(s) c = Mid(s, i, 1) If UCase(c) = A And UCase(c) = 0 And c = 9 Then w = w + 1 Else r = r + 1 End If Next Label1.Tex
2、t = 字母有 & q & 个 数字有 & w & 个 其他有 & r & 个 End Sub2.一串字符以?结尾,求有多少字母,有多少数字,有多少其他Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, a, z, x, c, i s = TextBox1.Text a = Mid(s, 1, 1) i = 1 Do While a ? If UCase(a) = A And UCase(a) = 0 And a =
3、0 Then Label1.Text = 是 & Len(c) & 位数 Else Label1.Text = 是 & Len(c) - 1 & 位数 End If Else If c = 0 Then For i = 1 To Len(c) If Mid(c, i, 1) = . Then Exit For j = j + 1 Next Label1.Text = 小数点前有 & j & 位数 & 小数点后有 & Len(c) - j - 1 & 位数 Else For i = 1 To Len(c) If Mid(c, i, 1) = . Then Exit For j = j + 1 N
4、ext Label1.Text = 小数点前有 & j - 1 & 位数 & 小数点后有 & Len(c) - j - 1 & 位数 End If End If End Sub4.求圆周率 公式Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a, s, n, pi a = 1 n = 1 While Math.Abs(1 / n) 0.0001 pi = pi + a * (1 / n) n = n + 2 a = -a
5、 End While Label1.Text = 4 * pi End Sub5.求自然对数e 公式e=1+1+1/2!+1/3!+1/n!+.Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, e1, l n = 1 e1 = 1 While 1 / n 0.01 l = 1 For i = 1 To n l = l * i Next n = n + 1 e1 = e1 + 1 / l End While Label
6、1.Text = e1End Sub6.右直角三角形 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, s Label1.Text = n = Val(TextBox1.Text) For i = 1 To n s = Label1.Text &= Space(n - i) For j = 1 To i s &= * Next Label1.Text &= s & vbCrLf Next End Sub7等腰三角形
7、Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, s Label1.Text = n = Val(TextBox1.Text) For i = 1 To n s = Label1.Text &= Space(n - i) For j = 1 To 2 * i - 1 s &= * Next Label1.Text &= s & vbCrLf Next End Sub8倒等腰三角形Private Sub Button1
8、_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, s Label1.Text = n = Val(TextBox1.Text) For i = n To 1 Step -1 s = Label1.Text &= Space(n - i) For j = 2 * i - 1 To 1 Step -1 s &= * Next Label1.Text &= s & vbCrLf Next End Sub9右上角的直角三角形Private Sub Button1
9、_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, s Label1.Text = n = Val(TextBox1.Text) For i = n To 1 Step -1 s = Label1.Text &= Space(n - i) For j = i To 1 Step -1 s &= * Next Label1.Text &= s & vbCrLf Next End Sub10菱形Private Sub Button1_Click(ByVal s
10、ender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, s Label1.Text = For i = 1 To 6 s = Label1.Text &= Space(i - 1) For j = 1 To 12 s &= * Next Label1.Text &= s & vbCrLf Next End Sub11.水仙花数字 三位数字的个位数字的立方和等于该数字本身(1).判断输入的数字是否是水仙花数Private Sub Button1_Click(ByVal sender As
11、System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, a, b, c s = Val(TextBox1.Text) Label1.Text = a = s 100 b = (s - a * 100) 10 c = s Mod 10 If a 3 + b 3 + c 3 = s Then Label1.Text &= s & 是水仙花数 Else Label1.Text &= s & 不是水仙花数 End If End Sub(2).找出所有的水仙花数Private Sub Button1_Click(B
12、yVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, a, b, c Label1.Text = 水仙花数是 & vbCrLf For s = 100 To 999 a = s 100 b = (s - a * 100) 10 c = s Mod 10 If a 3 + b 3 + c 3 = s Then Label1.Text &= s & vbCrLf End If Next End Sub12.完全数 正整数的因数之和等于这个数(1).判断输入的数字是不是完全数Pr
13、ivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s, j, l, a(100) s = Val(TextBox1.Text) j = 1 For i = 1 To s - 1 If s Mod i = 0 Then a(j) = i j = j + 1 End If Next For i = 1 To j l = l + a(i) Next If l = s Then Label1.Text = s & 是完全数 Else
14、Label1.Text = s & 不是完全数 End If End Sub(2).找出1到n内的完全数,n 由用户输入Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n, j n = Val(TextBox1.Text) Label1.Text = For p = 2 To n j = 0 For i = 1 To p - 1 If p Mod i = 0 Then j = j + i End If Next If j
15、 = p Then Label1.Text &= p & End If Next End Sub13.显示1,1,2,3,5,8,13,21 F(i)=f(i-1)+ f(i-2)Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim f(30) f(1) = 1 f(2) = 1 For i = 3 To 30 f(i) = f(i - 1) + f(i - 2) Next Label1.Text = For i = 1 To
16、 30 Label1.Text &= f(i) & , If i Mod 6 = 0 Then Label1.Text &= vbCrLf Next End Sub7 少 , 少 字 多 其 , & . = . ( ( ( 0( . ) . ( _ ) ) , &=& . += . ( = . _ 输用 全内到出 数全 = . 数完 ( - = ( 0 , . _ 全完是入判个于和因整 全 & = = ) = 0 = = 花 . , ) . ( 花水所找 数仙是 & . 数仙是 00 00 . . ( _ 花仙是的断.本字该和数个位 数 = * -( & . . . , ( 菱 = . * -
17、 = ( = = - ( . ( 角角的 & *& - - = -( . - = . = . _ 角角等 & . * -* = ( = = = . , . ( 角三 & . *& -( . . ( = . , _ 角角 + * 00 / = , ) , _ !/+/! + 公 然 - / 000)/( = , , 公 数& ) 有小 数 &前数= += .= , = 位 ) 有小 位 点= . ) ,( = 0 数 - ( = 数位 ) & )( ) = . ) ( _ . ( ) . 字位它数定 & ( + + = = ? , ( = , ) , 其多字少母少,?以 个 & 有 &数个 字= . = = =)( ( , , ) , .