《vb常用程序代码(35页).doc》由会员分享,可在线阅读,更多相关《vb常用程序代码(35页).doc(37页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、-1、求半径为r的圆的面积,要求用户从键盘输入半r。程序代码:Private Sub Form_click()Const PI = 3.14Dim r%Dim s!r = InputBox(请输入半径r的值:, 求圆的面积)s = PI * r * rForm1.FontSize = 16Print 半径是 + Str(r) + 的圆的面积是 + Str(s)End Sub2、输入三个整数,从小到大顺序输出。程序代码:Private Sub Form_click()a = InputBox(a)b = InputBox(b)c = InputBox(c)Print 排序前:; a, b, cI
2、f a b Then t = a: a = b: b = tIf a c Then t = a: a = c: c = tIf b c Then t = b: b = c: c = tPrint 排序后:; a, b, cEnd Sub3、输入三个整数,从大到小顺序输出。程序代码:Private Sub Form_click()a = InputBox(a)b = InputBox(b)c = InputBox(c)Print 排序前:; a, b, cIf a b Then t = a: a = b: b = tIf a c Then t = a: a = c: c = tIf b c Th
3、en t = b: b = c: c = tPrint 排序后:; a, b, cEnd Sub4、输入x的值,输出对应的值。y=10+2x(0x10) 50-x(10x20) 30(20x30) 60-x(30 0 And x = 10 Then y = 10 + 2 * x MsgBox x= & x & y= & yElseIf x = 20 Then y = 50 - x MsgBox x= & x & y= & yElseIf x = 30 Then y = 30 MsgBox x= & x & y= & yElseIf x = 40 Then y = 60 - x MsgBox x
4、= & x & y= & yElse MsgBox x= & x & 不在统计数据的范围内End IfEnd Sub(二)、Private Sub Command1_Click()Dim x%, y%x = InputBox(x)Select Case xCase Is = 10y = 10 + 2 * xCase Is = 20y = 50 - xCase Is = 30y = 30Case Is = 40y = 60 - xCase ElseMsgBox 输入不合法End SelectPrint x=; x, y=; yEnd Sub5、编写一个程序,输出给定的某年某月的天数。程序代码:
5、Private Sub Command1_Click() Dim year As Integer, month As Integer, day As Integer year = CInt(Text1.Text) month = CInt(Text2.Text) Select Case month Case 1, 3, 5, 7, 8, 10, 12 day = 31 Case 4, 6, 9, 11 day = 30 Case 2 If year Mod 4 = 0 And year Mod 100 0 Or year Mod 400 = 0 Then day = 29 Else day =
6、 28 End If Case Else day = 0 End Select If day 0 Then MsgBox year & 年 & month & 月有 & day & 天 Else MsgBox 输入的月份不符合实际! End IfEnd Sub6、求S=1+2+3+.+100的累加和。程序代码:(一)、For.Next循环 Private Sub Command1_Click() Dim i%, s% s = 0 For i = 1 To 100 s = s + i Next i Print s=; sEnd Sub(二)、Do WhileLoop循环 Private Sub
7、Command1_Click() Dim i%, s% s = 0: i = 1 Do While i 100 s = s + i i = i + 1 Loop Print s=; sEnd Sub7、接6,如果累加达到1000时马上终止循环,同时输出此时循环变量i的值。程序代码:(一)、Private Sub Command1_Click() Dim i%, s% s = 0 For i = 1 To 100 s = s + i If s = 1000 Then Exit For Next i Print s=; s, i=; iEnd Sub(二)、 Private Sub Command
8、1_Click() Dim i%, s% s = 0: i = 0 While s 1000 i = i + 1 s = s + i Wend Print s=; s, i=; iEnd Sub(三)、 Private Sub Command1_Click() Dim i%, s% s = 0: i = 1 While s 1000 s = s + i i = i + 1 Wend Print s=; s, i=; i - 1End Sub8、任意俩个正整数,使用欧几里得算法(即辗转相除法)求它们的最大公约数。程序代码:(一)、While.Wend循环 Private Sub Command1
9、_Click()Dim M%, N%, R%M = Val(InputBox(请输入一个数M:, 求最大公约数)N = Val(InputBox(请输入一个数N:, 求最大公约数)R = M Mod NWhile R 0 M = N N = R R = M Mod NWendPrint 它们的最大公约数是:; NEnd Sub(二)、Do.Loop循环 Private Sub Command1_Click()Dim M%, N%, R%M = Val(InputBox(请输入一个数M:, 求最大公约数)N = Val(InputBox(请输入一个数N:, 求最大公约数)R = M Mod ND
10、o While R 0 M = N N = R R = M Mod NLoopPrint 它们的最大公约数是:; NEnd Sub9、给定一个正整数n(n3),判断它是否是素数。程序代码:(一)、原版 Private Sub Command1_Click() Dim Flag%, I%, N& N = Val(InputBox(请输入一个整数, 判断是否是素数) Flag = 0: I = 2 While I = N - 1 And Flag = 0 If N Mod I = 0 Then Flag = 1 Else I = I + 1 Wend If Flag = 0 Then Print
11、N & 是素数 Else Print N & 不是素数End Sub(二)、改进版 Private Sub Command1_Click() Dim Flag%, I%, k%, N& N = Val(InputBox(请输入一个整数, 判断是否是素数) Flag = 0: I = 2: k = Int(Sqr(N) While I = k And Flag = 0 If N Mod I = 0 Then Flag = 1 I = I + 1 Wend If Flag = 0 Then Print N & 是素数 Else Print N & 不是素数 End IfEnd Sub10、找出10
12、0200之间的全部素数。程序代码:(一)、 Private Sub Form_click() Dim Flag%, i%, k%, n%, d% For n = 101 To 200 Step 2 Flag = 0: i = 2: k = Int(Sqr(n) While i Sqr(i) Then Text1.Text = Text1.Text & i & Space(2)Next iEnd Sub11、输出1n之间的所有素数。程序代码: Private Sub Command1_Click()n = Text1.Texts = 0For i = 3 To nFor j = 2 To Sqr
13、(i)If i Mod j = 0 Then Exit ForNext jIf j Sqr(i) Then Text2.Text = Text2.Text & i & Space(2)Next iEnd Sub12、输出菱形(1,3,5,7,5,3,1)程序代码:Private Sub Form_click()For i = 1 To 4Print Spc(9 - i);For j = 1 To 2 * i - 1Print *;Next jPrintNext iFor i = 1 To 3Print Spc(5 + i);For j = 1 To 7 - 2 * iPrint *;Next
14、jPrintNext iEnd Sub13、斐波那契数列问题。假设有一对兔子,从出生后第三个月开始,每个月都生一对小兔子。小兔子到第三个月又开始生下一代小兔子。假设兔子不死,问第20个月共有几对兔子?程序代码:(一)、 Private Sub Command1_Click()Dim f1%, f2%, f3%, i%f1 = 1: f2 = 1For i = 3 To 20 f3 = f1 + f2 f1 = f2 f2 = f3Next iText1.Text = f3End Sub(二)、 Private Sub Command1_Click()Dim f1%, f2%, i%f1 = 1
15、: f2 = 1For i = 2 To 10 f1 = f1 + f2 f2 = f2 + f1Next iText1.Text = f2End Sub14、从键盘输入六个数,从小到大排列。(选择排序)程序代码: Private Sub Command1_Click()Const n = 6Dim a(1 To n) As IntegerDim i As Integer, j As Integer, m As Integer, t As IntegerFor i = 1 To n a(i) = InputBox(input a( & Str(i) & )Next iPrint 无序数列: ;
16、For i = 1 To n Print a(i); ;Next iPrintFor i = 1 To n - 1 m = i For j = i + 1 To n If a(j) a(m) Then m = j Next j If m i Then t = a(i): a(i) = a(m): a(m) = t End IfNext i Print 有序数列: ; For i = 1 To n Print a(i); ; Next i PrintEnd Sub15、从键盘输入六个数,从大到小排列。(选择排序)程序代码: Private Sub Command1_Click()Const n
17、= 6Dim a(1 To n) As IntegerDim i As Integer, j As Integer, m As Integer, t As IntegerFor i = 1 To n a(i) = InputBox(input a( & Str(i) & )Next iPrint 无序数列: ;For i = 1 To n Print a(i); ;Next iPrintFor i = 1 To n - 1 m = i For j = i + 1 To n If a(j) a(m) Then m = j Next j If m i Then t = a(i): a(i) = a
18、(m): a(m) = t End IfNext i Print 有序数列: ; For i = 1 To n Print a(i); ; Next i PrintEnd Sub16、采用冒泡排序法完成10个俩位随机数的从小到大排列。(冒泡排序)程序代码: Private Sub Command1_Click()Const n = 10Dim x(n) As IntegerDim i, j, l, t As IntegerRandomizeFor i = 1 To n x(i) = Int(90 * Rnd + 10)Next iPrint 无序序列:;For i = 1 To nPrint
19、x(i); ;Next iPrintFor i = n To 2 Step -1 l = 0 For j = 1 To i - 1 If x(j) x(j + 1) Then t = x(j): x(j) = x(j + 1): x(j + 1) = t l = 1 End IfNext j If l = 0 Then Exit ForNext iPrint 有序序数: ;For i = 1 To n Print x(i); ;Next iPrintEnd Sub17、采用冒泡排序法完成10个俩位随机数的从大到小排列。(冒泡排序)程序代码: Private Sub Command1_Click
20、()Const n = 10Dim x(n) As IntegerDim i, j, l, t As IntegerRandomizeFor i = 1 To n x(i) = Int(90 * Rnd + 10)Next iPrint 无序序列:;For i = 1 To nPrint x(i); ;Next iPrintFor i = n To 2 Step -1 l = 0 For j = 1 To i - 1 If x(j) x(j + 1) Then t = x(j): x(j) = x(j + 1): x(j + 1) = t l = 1 End IfNext j If l = 0
21、 Then Exit ForNext iPrint 有序序数: ;For i = 1 To n Print x(i); ;Next iPrintEnd Sub18、计算三角形面积。程序代码: Private Sub Command1_Click()Dim a%, b%, c%, p!, s!, r%a = Val(InputBox(请输入三角形的边长a的值)b = Val(InputBox(请输入三角形的边长b的值)c = Val(InputBox(请输入三角形的边长c的值)If a + b c Or a + c b Or b + c max Then max = c(i, j) h = i
22、l = j End If Next jNext iPrint 最大值=; maxPrint 所在行号=; h; 所在列号=; lEnd Sub20、建立输出一个二维数组。程序代码: Private Sub Form_click()Dim x(3, 4) As IntegerDim i%, j%For i = 1 To 3 For j = 1 To 4 x(i, j) = InputBox(请输入x( & Str(i) & , & Str(j) & )的值) Next jNext iFor i = 1 To 3 For j = 1 To 4 Print x(i, j); ;Next jPrint
23、Next iEnd Sub21、给定m行n列的一张数据表,编程求每一行的行与每一列的列的和。程序代码: Private Sub Form_click() Const m = 3 Const n = 4 Dim a(m, n), h(m), l(n) As Integer Dim i, j As Integer For i = 1 To m h(i) = 0 Next i For j = 1 To n l(j) = 0 Next j For i = 1 To m For j = 1 To n a(i, j) = InputBox(请输入a( & Str(i) & , & Str(j) & )的值
24、) h(i) = h(i) + a(i, j) l(j) = l(j) + a(i, j) Next j Next i Print For i = 1 To m Print ; For j = 1 To n Print a(i, j); ; Next j Print 第; Str(i); 行和=; h(i) Next i Print 列的和=; For j = 1 To n Print l(j); ; Next j PrintEnd Sub22、某班学生的期末4门课程(用A、B、C、D表示课程名称)成绩表如下:学号 A B C D 总分9901 68 90 83 74 3159903 71 9
25、0 83 60 3049908 91 82 85 79 3379910 76 97 88 82 3439911 65 72 70 98 305用vb依总分从高到低打出一张成绩单。程序代码: Private Sub Form_click()Dim a(5, 6), k(5) As IntegerDim i, j, m, c As IntegerFor i = 1 To 5 a(i, 6) = 0 For j = 1 To 5 a(i, j) = InputBox(请输入第 & Str(i) & , & Str(j) & 个元素) If j 1 Then a(i, 6) = a(i, 6) + a
26、(i, j) Next jNext iFor i = 1 To 5 k(i) = iNext iFor i = 1 To 4 m = i For j = i + 1 To 5 If a(k(j), 6) a(k(m), 6) Then m = j Next j If m i Then c = k(i): k(i) = k(m): k(m) = cNext iPrint No ; A ; B ; C ; D ; 总分 PrintFor i = 1 To 5 For j = 1 To 6 Print Tab(9 * j - 8); a(k(i), j); Next j PrintNext iEnd
27、 Sub 23、生成单位矩阵。编程产生一个6行6列的单位矩阵。单位矩阵就是主对角线上元素全为1,其他元素全为0的方阵。程序代码: Private Sub Command1_Click()Dim a(6, 6) As IntegerDim i, j As IntegerFor i = 1 To 6 For j = 1 To 6 If i = j Then a(i, j) = 1 Else a(i, j) = 0 Print Tab(3 * j); a(i, j); Next j PrintNext iEnd Sub24、求一个已知矩阵的转置矩阵。程序代码: Private Sub Command
28、1_Click()Dim a(2, 3), b(3, 2) As IntegerDim i, j As IntegerPrint 原矩阵:For i = 1 To 2 For j = 1 To 3 a(i, j) = InputBox(请输入第 & Str(i) & , & Str(j) & 个元素) b(j, i) = a(i, j) Print ; a(i, j); ; Next j PrintNext iPrint 转置矩阵:For i = 1 To 3 For j = 1 To 2 Print ; b(i, j); ; Next j PrintNext iEnd Sub25、水仙花数程
29、序代码:Private Sub Command1_Click()Dim a%, b%, c%, n%, i%n = Text1.TextFor i = 100 To n a = i 100 b = Int(i Mod 100) / 10) c = i Mod 10 If i = a 3 + b 3 + c 3 Then Text2.Text = Text2.Text & i & Space(2)Next iEnd Sub26、同构数程序代码: Private Sub Command1_Click()n = Text1.TextFor i = 1 To n j = i 2 If CStr(i) = Right(CStr(j), Len(CStr(i) Then Text2.Text = Text2.Text & i & Space(2) End IfNext iEnd Sub27、完全平方数程序代码: Private Sub Command1_Click()n = Text1.TextFor i = 1 To n a = Sqr(i) If a = Int(a) Then Text2.Text = Text2.Text & i & Space(2) Next iEnd Sub第 37 页-
限制150内