VB期末参考代码 大题

更新时间:2024-01-16 18:42:01 阅读量: 教育文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

【程序填空】

1. '功能:以下程序段用于实现矩阵转置,即将一个n×m的矩阵的行和列互换 Private Sub Form_Click() Const n = 3 Const m = 4 Dim a(n, m), b(m, m) As Integer For I = 1 To n For j = 1 To m a(I, j) = Int(Rnd * 90) + 10 Next j Next i For I = 1 To n For j = 1 To m B(I,j)=a(I,j) Next j Next I Print \矩阵转置前\For I = 1 To n For j = 1 To m Print a(I, j); Next j print Next I Print \矩阵转置后\For I = 1 To m For j = 1 To n Print b(I, j); Next j Print Next I End Sub

1. '功能:输入数字1-7,显示其对应是星期几(例:输入 1,显示星期日,如果输入其它内容,一律显示\输入错误\Private Sub Command1_Click() dim x as integer dim y as string x=val(text1.text) Select case x case 1 y=\今天是星期日\ case 2 y=\星期一\ case 3 y=\星期二\ case 4 y= \星期三\ case 5 y=\星期四\ case 6 y=\星期五\ case 7 y= \星期六\Case else y = \输入错误\ end select Text2.Text = y End Sub

1. 功能:求 1!+2!+??+10!的值。 Private Sub Form_Click() T=1 s = 1 For I = 2 To 10 t = t * I S=s+t Next i Print s End Sub

1. '功能:以下程序段用于计算n的阶乘:n!。(1!=1) Public Function fac(n As Integer) As Integer If n = 1 Then fac = 1 Else fac = n * fac(n-1) End If End Function Private Sub Command1_Click() Print \End Sub

1. 功能:以下程序的功能是:生成 20 个 200 到 300 之间的随机整数,输出其中能被 5 整除的数并求出它们的和 Private Sub Command1_Click() Dim s As Integer Dim x As Integer, i As Integer s = 0 For i = 1 To 20 Randomize X = Int(rnd * 100 + 200) If X mod 5 = 0 Then Print X s = s + X End If Next i Print \=\End Sub

1. '功能:在窗体上画一个标签(名称为 Label1 )和一个计时器(名称为 Timer1 ),然后编写如下几个事件过程:要求:每一秒钟Label1更新显示一次系统时间 Private Sub Form_Load() Timer1.Enabled = False Timer1.Interval = 1000 End Sub Private Sub Form_Click() Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Label1.Caption = Now End Sub

1. 功能:窗体上建立了两个文本框Text1(输入字符串)及Text2(转换结果),一个命令按钮Command1(\清除\。录入Text1内容时,将录入内容大写字母转为小写,小写转为大写。 Private Sub Command1_Click() Text1.Text = \ Text2.Text = \ Text1.SetFocus End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) Dim s As String * 1 s = chr(KeyAscii) Select Case s Case \ s = LCase(s) Case \ s = Ucase(s) Case Else s = \ End Select Text2.Text = Text2.Text + s End Sub 1. 功能:窗体上建立了一个文本框Text1(\输入口令\和一个命令按钮Command1(\检查\。判断密码是否正确,并显示相应提示在加载窗体时设置密码框最大长度为8,密码显示字符为“*” dim x as string Private Sub Command1_Click() x = Text1.Text If x = \ MsgBox (\欢迎你用机!\ Else MsgBox (\对不起,口令错\ End If Text1.Text = \ Text1.SetFocus End Sub Private Sub Form_Load() Text1.Text = \ Text1.maxlength = 8 Text1.Psswordchar = \ End Sub

1. 功能:下面的程序段用于打印出以下图形 ' ' * ' *** ' ***** ' ******* ' ***** ' *** ' * '------------------------------------------------------- Private Sub Form_Click() Dim i%, j%, k% For i = 0 To 3 For j = 0 To 2 - i Print \ Next j For k = 1 To 2*i+1 Print \ Next k Print Next i For i = 0 To 2 For j = 0 To i Print “ ”; Next j For k = 0 To 4 - 2 * i Print \ Next k print Next i End Sub

1. 功能:下面的程序段用于求矩阵相乘C=A×B,设A、B、C分别为m×p、p×n、m×n的矩阵。 Private Sub Form_Click() Const m = 4, p = 3, n = 2 Dim a(1 To m, 1 To p) As Integer Dim b(1 To p, 1 To n) As Integer Dim c(1 To m, 1 To n) As Integer Dim i As Integer, j As Integer, k As Integer, sum As Integer For i = 1 To m For k = 1 To p a(i, k) = i Next k Next i For i = 1 To p For k = 1 To n b(i, k) = i Next k Next i For i = 1 To m For j = 1 To n Sum=0 For k = 1 To p sum = sum +a(I,k)*b(k,j) Next k c(i, j) =sum Next j, i Print \输出矩阵\For i = 1 To m For k = 1 To n Print c(i, k), Next k Print Next i End Sub

1. 功能:利用下面的过程求m!和m*n Private Sub Form_Click() Dim m As Integer, n As Integer m = 2 n = 3 Find m,n End Sub Private Sub find(x As Integer, y As Integer) Dim s, i As Integer S=1 For i = 1 To x s = s * i p=x*y Next i Print s, p End Sub

1. 题目:已知用inputbox函数输入两个数x和y,比较它们的大小,使得x大于y。 Private Sub Form_Click() Dim x As Single, y As Single x = InputBox(\输入数据1:\y = InputBox(\输入数据2:\Print \值\值\Print x, y If x < y Then T=x X=y Y=t End If Print x, y End Sub

【程序改错】

1. '题目:以下程序段用于打印如图1所示的九九乘法表: Option Explicit Private Sub Form_Click() Dim i As Integer, j As Integer, k As Integer Print Tab(30); \Print: Print Print \ \For i = 1 To 9 Print Tab(i * 6); I; Next i Print For j = 1 To 9 Print j; \ For k = 1 To j Print Tab(k * 6); j * k; \ Next k Print Next j End Sub

1. '题目:求s=1!+3!+5!+7!,阶乘的计算用Function过程fact实现. Option Explicit Private Sub Form_Click() Dim i As Integer, s As Integer For i = 1 To 7 step 2 s = s + fact(i) Next i Print s End Sub Public Function fact(ByVal n As Integer) Dim t As Integer, i As Integer t = 1 For i = 1 To n t = t * i Next i fact =t End Function

1. 题目:请根据下列描述编写购物优惠程序。某商场为了加速促成商品流通,采用购物打折的优惠办法,每位顾客一次购物(1)在100元以上者,按九五折优惠;(2)在200元以上者,按九折优惠;(3)300元以上者,按八折优惠;(4)500元以上者按七折优惠。 Option Explicit Private Sub Command1_Click() Dim x As Single, y As Single x = Val(Text1.Text) If x < 100 Then Y=x Else If x < 200 Then y = 0.95 * x Else If x < 300 Then y = 0.9 * x Else If x < 500 Then y = 0.8 * x Else y = 0.7 * x End If End If End If End If Text2.Text = y End Sub 1. 题目:以下程序用于建立一个三行三列的矩阵,使其两条对角线上数字为1,其余位置为0 Option Explicit Private Sub Form_Click() Dim x(3, 3), n As Integer, m As Integer For n = 1 To 3 For m = 1 To 3 If n = m Or m + n = 4 Then x(n, m) = 1 Else x(n, m) = 0 Next m, n For n = 1 To 3 For m = 1 To 3 Print x(m, n); Next m Print Next n End Sub

1. 题目:下面程序段将7个随机整数从小到大排序 Option Explicit Private Sub Form_Click() Dim t%, m%, n%, w%, a(7) As Integer For m = 1 To 7 a(m) = Int(10 + Rnd() * 90) Print a(m); \Next m Print For m = 1 To 6 t = m For n = m+1 To 7 If a(t) > a(n) Then t=n Next n If t <> m Then w = a(m) a(m) = a(t) a(t) = w End If Next m For m = 1 To 7 Print a(m) Next m End Sub

1. 题目:输出40以内能够被3整除的数,要求输出结果为5个数一行。 Option Explicit Private Sub Form_Click() Cls Dim x As Integer Dim i As Integer i = 0 For x = 1 To 40 If (x / 3) = (x \\ 3) Then Print x; i = i + 1 End If If i Mod 5 = 0 Then Print End If next i End Sub

1. 题目:编程求一个m*n矩阵中最大元素及其所在的行列号。 Option Explicit Private Sub Form_Click() Dim A() As Integer, max As Integer Dim M As Integer, N As Integer Dim i As Integer, j As Integer Dim col As Integer, row As Integer M = InputBox(\输入矩阵的行数:\ N = InputBox(\输入矩阵的列数:\ ReDim A(1 To M, 1 To N) As Integer For i = 1 To M For j = 1 To N A(M, N) = InputBox(\输入数组元素:\ Next j Next i max = A(1,1) For i = 1 To M For j = 1 To N If max < A(i, j) Then max = A(i, j) col = j End If row = i Next j Next i Print Print \该矩阵元素的最大值:\ Print \最大值所在的行:\所在的列:\End Sub

1. 题目:程序功能:求1+2+3??,直到其和超出3000为止,并输入结果。 Option Explicit Private Sub Form_Click() Cls Dim i As Integer Dim s As Single i = 1 s = 0 Do i = i + 1 s = s + i Loop until s > 3000 Print \从1 到:\的和是\End Sub

1. 题目:用辗转相除法求两个整数的最大公约数。 Option Explicit Function gcd(ByVal m As Integer, ByVal n As Integer) As Integer Dim r As Integer r = m Mod n Do While r <> 0 M=n n = r r = m Mod n Loop gcd = n End Function Private Sub Form_Click() Dim a As Integer, b As Integer, c As Integer a = InputBox(\输入一个整数\ b = InputBox(\输入一个整数\ a = Val(a) b = Val(b) c=gcd(a, b) Print a, b, c End Sub

1. 题目:密码判断程序,如果密码为12345则显示“恭喜,密码正确”,否则显示“很遗憾,密码错误”,要求文本框中只允许输入数字。 Option Explicit Private Sub Command1_Click() Dim strPws As String strPws = Trim(Text1.Text) If Len(strPws) = 0 Then Exit Sub If strPws = \ MsgBox \恭喜,密码正确\验证 Else MsgBox \很遗憾,密码错误\验证\ End If End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) If Not (KeyAscii >= 48 And KeyAscii <= 57) Then KeyAscii = 0 End If End Sub

1. 题目:求s=72+102+132+??832的值。 Option Explicit Private Sub Form_Click() Cls Dim s As Long Dim i As Integer s = 0 For i = 7 To 832 step 30 s = s + i next i Print s End Sub

1. 题目:下边是一个小动画程序,在窗体上放一个标签Label1,每过1秒钟标签Label1的背景颜色由红到蓝,由蓝到绿,再由绿到红循环变化,并自动修改标签Label1的Left,Top值使其从左上角沿窗体的对角线移动到窗体的右下角,如此往复从而实现动画。 Option Explicit Private flag As Integer Private Sub Form_click() Timer1.Enabled = True End Sub Private Sub Form_Load() Timer1.Interval = 1000 Label1.Left = 0 Label1.Top = 0 Label1.BackColor = vbBlack Timer1.Enabled = False End Sub Private Sub Timer1_Timer() If Label1.Left < Left + Width And Label1.Top <= Top + Height Then Label1.Left = Label1.Left + 100 * Width / Height Label1.Top = Label1.Top + 100 Else Label1.Left = Left Label1.Top = Top End If If flag = 0 Then Label1.BackColor = vbBlue flag = 1 ElseIf flag = 1 Then Label1.BackColor = vbGreen flag = 2 Else Label1.BackColor = vbRed flag = 0 End If End Sub

【程序设计】

1. 题目:(事件)单击窗体。(响应)求200~300之间既能被3整除又能被7整除的数。并求出所有数之和存入变量SUM中 Private Sub Form_Click() Dim sum As Integer sum = 0 Dim i As Integer For i = 201 To 300 If ((i Mod 3) = 0) And ((i Mod 7) = 0) Then Print (CStr(i)) sum = sum + i End If Next Print sum YZJ (sum) End Sub Private Sub YZJ(i As Integer) Dim OUT As Integer OUT = FreeFile Open App.Path & \ Print #OUT, i Close #OUT End Sub 1. 题目:(事件)单击窗体。(响应)已知S=1+1/4+1/9+1/16+1/25+?+1/100,结果输出在窗体上。 Private Sub Form_Click() Dim s As Single Dim i As Integer s = 0 For i = 1 To 10 s = s + 1 / (i ^ i) Next Print (s) YZJ (s) End Sub Private Sub YZJ(i As Single) Dim OUT As Integer OUT = FreeFile Open App.Path & \ Print #OUT, i Close #OUT End Sub

1. 题目:(事件)单击窗体。(响应)一球从100米高度自由落下,每次落地返回原高度的一半,求第10次落地时,共经过多少米?并将结果在窗体上输出。 Private Sub Form_Click() Dim s As Single Dim j, i As Integer s = 0 j = 100 For i = 2 To 10 j = j / 2 s = s + 2 * j Next s = 100 + s Print (CStr(s)) YZJ (s) End Sub Private Sub YZJ(i As Single) Dim OUT As Integer OUT = FreeFile Open App.Path & \ Print #OUT, i Close #OUT End Sub

1. 题目:(事件)单击窗体。(响应)一球从100米高度自由落下,每次落地返回原高度的一半,求第10次落地时,共经过多少米?并将结果在窗体上输出。 Private Sub Form_Click() Dim s As Single Dim j, i As Integer s = 0 j = 100 For i = 2 To 10 j = j / 2 s = s + 2 * j Next s = 100 + s Print (CStr(s)) YZJ (s) End Sub Private Sub YZJ(i As Single) Dim OUT As Integer OUT = FreeFile Open App.Path & \ Print #OUT, i Close #OUT End Sub

本文来源:https://www.bwwdw.com/article/q4fo.html

Top