中国石油大学华东VB练习题大一下.doc_第1页
中国石油大学华东VB练习题大一下.doc_第2页
中国石油大学华东VB练习题大一下.doc_第3页
中国石油大学华东VB练习题大一下.doc_第4页
中国石油大学华东VB练习题大一下.doc_第5页
已阅读5页,还剩36页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

中国石油大学华东VB练习题(大一下)1、 时钟2、 滚动条3、 列表框4、 复选框5、 单选框6、 组合框7、 多分枝选择8、 定义函数九、TXT十、数组一、时钟程序运行后,每隔2秒命令按钮Command1向右移动100个单位。Private Sub Form_Load()Timer1.Interval = 2End SubPrivate Sub timer1_timer()Call mymoveEnd SubSub mymove() Command1.Left = Command1.Left + 200End Sub二、滚动条水平滚动条HScroll1的数值范围04000,LargeChange设置为200,SmallChange设置为50,水平滚动条HScroll1的值(Value)代表命令按钮Command1的宽度,当水平滚动条的滑块变化时,命令按钮Command1的宽度随之改变Private Sub Form_Load()HScroll1.Min = 0HScroll1.Max = 4000HScroll1.SmallChange = 50HScroll1.LargeChange = 200Command1.Caption = 驻倚靠危楼风细细,无言谁会凭栏意。衣带渐宽终不悔,为伊销得人憔悴End SubPrivate Sub HScroll1_Change()Command1.Width = Val(HScroll1.Value)End Sub三、列表框 点击“-”按钮,将左边列表中的选中项移动到右边列表的末尾;点击“”按钮,将左边列表所有内容移动到右边列表的末尾;点击“= 0 And i = 0 And i = 0 And i = 0 And i -1 Then _ .RemoveItem .ListIndex End With End SubPrivate Sub Command5_Click() ClrList List1 List1.ClearEnd SubPrivate Sub Form_Load() List1.Clear: List1.AddItem 语文: List1.AddItem 数学: List1.AddItem 英语End Sub实现第一条、前一条、后一条、最后一条等4按钮的功能 Private Sub form_load()List1.AddItem 财务List1.AddItem 会计List1.AddItem 国贸List1.AddItem 营销List1.AddItem 工管Private Sub Command1_Click()List1.Text = List1.List(0)End SubPrivate Sub Command2_Click()List1.Text = List1.List(List1.ListIndex - 1)End SubPrivate Sub Command3_Click()List1.Text = List1.List(List1.ListIndex + 1)End SubPrivate Sub Command4_Click()List1.Text = List1.List(List1.ListCount - 1)End Sub4、 复选框 窗体中4个复选框分别代表4各班级,复选框中的标题文字代表班级人数,用户选择复选框后,点击命令按钮Command1,程序计算出所选班级的总人数,结果放入文本框Text1中。Private Sub Command1_Click()Dim s%If Check1.Value = 1 Then s = s + Val(Check1.Caption)End IfIf Check2.Value = 1 Then s = s + Val(Check2.Caption)End IfIf Check3.Value = 1 Then s = s + Val(Check3.Caption)End IfIf Check4.Value = 1 Then s = s + Val(Check4.Caption)End IfText1.Text = Str(s)End SubPrivate Sub Form_Load()Check1.Caption = 30Check2.Caption = 28Check3.Caption = 31Check4.Caption = 29Label1.Caption = 人数Text1.Text = End Sub用户选择复选框后,点击命令按钮Command1,程序根据复选框对文本框Text1中的字体进行相应的设置。Private Sub Command1_Click()If Check1.Value = 1 Then Text1.FontBold = TrueIf Check2.Value = 1 Then Text1.FontItalic = TrueIf Check3.Value = 1 Then Text1.FontUnderline = TrueEnd Sub五、 单选框用户在数和算法两个框架中各选一个单选按钮,点击命令按钮Command1,程序根据选中单选按钮进行相应的计算,结果放入文本框Text1中。Private Sub Command1_Click()Dim a%, b%, c%, d%k = 1: t = 0a = Val(Option1.Caption)b = Val(Option2.Caption)If Option1.Value And Option3.Value Then For i = 1 To a k = k * i Next iText1.Text = kElseIf Option1.Value And Option4.Value Then For i = 1 To a t = t + i Next iText1.Text = tElseIf Option2.Value And Option3.Value Then For i = 1 To b k = k * i Next iText1.Text = kElseIf Option2.Value And Option4.Value Then For i = 1 To b t = t + i Next iText1.Text = tEnd IfEnd Sub5、 组合框字体组合框包括宋体、黑体、幼圆、隶书4个列表项;字号组合框列表项的值从1248,以4递增。选择字体组合框或者字号组合框中的某一项,点击按钮Command1,文本框中的字体和字号进行相应的改变。Private Sub Command1_Click()Text1.FontName = Combo1.TextText1.FontSize = Combo2.TextEnd SubPrivate Sub Form_Load()Combo1.AddItem 宋体Combo1.AddItem 黑体Combo1.AddItem 幼圆Combo1.AddItem 隶书For i = 12 To 48 Step 4Combo2.AddItem iNext iText1.Text = Visual BasicEnd Sub七、多分枝加密:点击加密按钮Command1,输入1个小写英文字母,程序将该英文字母变为字母表中其后面的一个字母输出,如a改为b,m改为n,z改为a,以此类推。点击解密按钮Command2,输入1个小写英文字母,程序将其解密后通过输出。使用InputBox和MsgBox完成输入输出。Private Sub Command1_Click()Dim a As String, b As Stringa = InputBox()If Asc(a) = 122 Then b = Chr(97)Else: b = Chr(Asc(a) + 1)End IfMsgBox bEnd SubPrivate Sub Command2_Click()Dim c As String, d As Stringc = InputBox()If Asc(c) = 97 Then d = Chr(122)Else: d = Chr(Asc(c) - 1)End IfMsgBox dEnd Sub铁路收费:铁路行李托运费计算规则为:行李重量不超过50kg时,每千克0.25元;超过50kg不超过100kg时,其超过部分每千克0.35元;超过100kg时,其超过部分每千克0.45元。点击按钮Command1,程序输入行李重量,计算并输出托运费用。使用InputBox和MsgBox完成输入输出。Private Sub Command1_Click()Dim a As Double, b As Doublea = Val(InputBox()If a = 0 And a = 50 Then b = a * 0.25 ElseIf a = 100 Then b = 30 + (a - 100) * 0.45End IfMsgBox bEnd Sub (也可用select case 语句)成绩评定; 成绩评价方法为:90及以上优秀,80及以上良好,70及以上中等,60及以上及格,60以下不及格。点击按钮Command1,程序输入成绩,输出成绩评价。使用InputBox和MsgBox完成输入输出。Private Sub Command1_Click() a = Val(InputBox(输入成绩) If a = 90 Then b = 优秀 ElseIf a = 80 Then b = 良好 ElseIf a = 70 Then b = 中等 ElseIf a = 60 Then b = 及格 Else b = 不及格 End If MsgBox bEnd Sub (也可用select case 语句或者直接限定范围进行选择)8、 定义函数: 【一个月有几天】一个月的天数:在窗体Form1中实现函数Days。参数y和m分别表示年份和月份,函数返回该年该月的天数。 提示:闰年2月份有29天,平年有28天。 闰年的条件:1)年份能被4整除,但不能被100整除;2)或年份能被400整除 Public Function Days(y As Long, m As Long) As Long End FunctionFunction leap(y As Long) As Boolean b1 = y Mod 4 = 0 b2 = y Mod 100 0 b3 = y Mod 400 = 0 leap = b1 And b2 Or b3End FunctionPublic Function days(y As Long, m As Long) As Long Select Case m Case 2 If leap(y) Then days = 29 Else days = 28 End If Case 4, 6, 9, 11 days = 30 Case 1, 3, 5, 7, 8, 10, 12 days = 31 End SelectEnd FunctionPrivate Sub Command1_Click() Dim a&, b&, c& a = Val(InputBox() b = Val(InputBox() c = days(a, b) MsgBox cEnd Sub【三角形】:在窗体Form1中实现函数Triangle。a,b,c三个参数为三角形三条边,若三边能构成三角形,通过参数p将周长返回,函数返回值返回三角形面积;否则周长和面积均返回0。 提示: 海伦公式为: , Public Function Triangle(a As Long, b As Long, c As Long, p As Long) As Double End FunctionPublic Function Triangle(a As Long, b As Long, c As Long, ByRef p As Long) As Double Dim l If a + b = c Or a + c = b Or b + c a(j) Then p = j Next j k = a(i) a(i) = a(p) a(p) = kNext iEnd Sub(王老师的那种算法可能会评测错误)【判断是否三角形等腰】: 在窗体Form1中实现函数TriangleClass。a,b,c三个参数为三角形三条边,函数返回三角形的形状。可能的三角形形状为以下4种:等边三角形,等腰三角形,一般三角形,不构成三角形。 Public Function TriangleClass(a As Long, b As Long, c As Long) As String End Function Private Sub Command1_Click()Dim a As Long, b As Long, c As Longa = Val(InputBox()b = Val(InputBox()c = Val(InputBox()MsgBox triangleclass(a, b, c)End SubPublic Function triangleclass(a As Long, b As Long, c As Long) As StringDim s$If a + b = c Or b + c = a Or a + c max Then max = a(i)Next iEnd Function【排序】:在窗体Form1中实现子程序Sort。参数a()为数组,子程序Sort对该数组按逆序(从大到小)排序。 Public Sub Sort(a() As Long) End Sub Public Sub Sort(a() As Long)n = UBound(a)For i = 0 To n - 1For j = i + 1 To nIf a(j) a(i) Thent = a(i)a(i) = a(j)a(j) = tEnd IfNext jNext iEnd Sub【完数】:在窗体Form1中实现函数Perfect。如果参数x为完全数,该函数返回True;否则返回False。(完全数:真因子(除自身以外的因子)之和为其本身) Public Function Perfect(x As Long) As Boolean End FunctionPrivate Sub Command1_Click()Dim x As Longx = Val(InputBox()MsgBox perfect(x)End SubPublic Function perfect(x As Long) As BooleanFor i = 0 To x - 1If x Mod i = 0 Thens = s + iEnd IfNext iIf s = x Thenperfect = TrueElseperfect = FalseEnd IfEnd Function【符号函数】 在窗体Form1中实现函数MySgn。当参数x0时,函数MySgn返回1;当参数x 0 Then MySgn = 1ElseIf x = 0 Then MySgn = 0Else MySgn = -1End IfEnd Function九、TXT:复制文件:复制文件。点击Command1按钮,程序将文件a.txt中的内容复制到文件b.txt中Private Sub Command1_Click()Dim a$Open a.txt For Input As #1 Open b.txt For Output As #2 Do While Not EOF(1) Line Input #1, aPrint #2, aLoopClose #2逆序: 文件a.txt中每行包括2列内容,分别为姓名,成绩。单击Command1按钮,程序将a.txt文件中的各行,逆序写入b.txt文件中。 a.txt文件格式示例 - 小红,90 小明,95 小刚,85 b.txt文件格式示例 - 小刚,85 小明,95 小红,90Private Sub Command1_Click()Dim a$(1 To 10), b%(1 To 10), i%, j%Open a.txt For Input As #1Open b.txt For Output As #2i = 1 Do While Not EOF(1) Input #1, a(i), b(i) i = i + 1 LoopFor j = UBound(b) To LBound(b) Step -1 If b(j) 0 Then Write #2, a(j), b(j) End IfNext jClose #2Close #1End Sub找最大: 文件a.txt中每行包括2列内容,分别为姓名,成绩。单击Command1按钮,程序找出最高分所在行,将姓名和成绩写入b.txt文件中。 a.txt文件格式示例 - 小红,90 小明,95 小刚,85 b.txt文件格式示例 - 小明,95Private Sub Command1_Click()Dim a$, b%, c$, d%Open a.txt For Input As #1 Open b.txt For Output As #2 Do While Not EOF(1) Input #1, a, b If b d Then d = b: c = a Loop Write #2, c, dClose #1Close #2End Sub 求和: 文件a.txt中每行包括3列内容,分别为姓名,语文成绩,数学成绩。单击Command1按钮,程序计算出每名同学的总成绩,写入b.txt文件中。 a.txt文件格式示例 - 小红,80,90 小明,70,80 b.txt文件格式示例 - 小红,170 小明,150Private Sub Command1_Click()Dim n$, c%, d%, e%Open a.txt For Input As #1Open b.txt For Output As #2Do While Not EOF(1) Input #1, n, c, d e = c + dWrite #2, n, eLoop Close #1 Close #2End Sub 选择 : 文件a.txt中每行包括2列内容,分别为姓名,成绩。单击Command1按钮,程序将分数大于等于90分对应的行,写入b.txt文件中。 a.txt文件格式示例 - 小红,90 小明,95 小刚,85 b.txt文件格式示例 - 小红,90 小明,95Private Sub Command1_Click()Dim a$, b%Open a.txt For Input As #1Open b.txt For Output As #2Do While Not EOF(1) Input #1, a, b If b = 90 Then Write #2, a, b End IfLoopClose #1Close #2End Sub排序: 文件a.txt中每行包含1个数。单击Command1按钮,程序将所有数排序后写入b.txt文件中。 a.txt文件格式示例 - 13 8 17 12 b.txt文件格式示例 - 8 12 13 17Private Sub Command1_Click()Dim a%(1 To 12), i%, j%, k%, t%,h = UBound(a): g = LBound(a): i = LBound(a)sp = App.Path & Open sp & a.txt For Input As #1Open sp & b.txt For Output As #2For i = 1 To 12 Input #1, a(i)Next iFor j = 1 To 11 For k = j To 12 If a(j) a(k) Then t = a(j): a(j) = a(k): a(k) = t Print a(j) Next kNext jFor j = 1 To 12If a(j) 0 Then Write #2, a(j)Next j Close #1 Close #2End Sub素数; 文件a.txt中每行包含1个数。单击Command1按钮,程序将所有的素数写入b.txt文件中。 a.txt文件格式示例 - 13 8 17 12 b.txt文件格式示例 - 13 17Private Sub Command1_Click()Dim a%sp = App.Path & Open sp & a.txt For Input As #1Open sp & b.txt For Output As #2Do While Not EOF(1) Input #1, a For i = 2 To a - 1 If a Mod i = 0 Then Exit For Next i If i a - 1 Then Write #2, aLoopClose #1Close #2End Sub 十、数组 数组删除: 在文本框Text1中放入一维数组元素个数n,在文本框Text2中放入n个数组元素,在文本框Text3中放入待查找的数,点击按钮Command1,程序在数组中从左到右进行查找,将第1个等于该数的元素删除后(若找不到则不删除任何元素),将数组放入Text4中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。Option Base 1Private Sub Command1_Click()Dim n%, a%(), b%(), i%, m%, idx%n = Val(Text1.Text) :m = Val(Text3.Text)ReDim a(n)TextToArray Text2, aidx = -1For i = 1 To nIf m = a(i) Then idx = i: Exit ForEnd IfNext iIf idx 0 Then For i = idx + 1 To n a(i - 1) = a(i) Next i n = n - 1 ReDim Preserve a(1 To n)End IfArrayToText a, Text4End Sub插入: 在文本框Text1中放入一维数组元素个数n,在文本框Text2中放入n个数组元素,在文本框Text3中放入待插入的数据,在文本框Text4中放入在数组中插入的位置,点击按钮Command1,程序将数据插入数组中,然后将数组放入Text5中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。要求数组元素下标从0开始。Private Sub Command1_Click()Dim a%(), n%, k%, t%,j%n = Val(Text1.Text)ReDim a(n - 1)TextToArray Text2, ak= Val(Text3.Text)t = Val(Text4.Text)For i = 0 To nIf i = k Then P = i: Exit ForNext iFor i = n To p + 1 Step -1 a(i + 1) = a(i)Next ia(p) = kReDim Preserve a(n + 1)ArrayToText a, Text4End Sub 在文本框Text1中放入一维数组元素个数n,在文本框Text2中放入n个数组元素,在文本框Text3中放入待插入的数据,在文本框Text4中放入在数组中插入的位置,点击按钮Command1,程序将数据插入数组中,然后将数组放入Text5中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。要求数组元素下标从0开始。Private Sub Command1_Click()Dim a%(),n%,i%,j%,k%,t%n = Text1.Text - 1k =val(text3.text) t = Text4.TextReDim a(n)TextToArray Text2, aFor i = n To t + 1 Step -1a(i + 1) = a(i)Next ia(t) = kReDim Preserve a(n + 1)ArrayToText a, Text5End Sub 最大最小值: 在文本框Text1中放入一维数组元素个数n,在文本框Text2中放入n个数组元素,点击按钮Command1,找出这n个数中第二小的数放入文本框Text3中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。Private Sub Command1_Click()Dim a() As Double, t As Doublen = Val(Text1.Text)ReDim a(n)TextToArray Text2, aFor i = 0 To n - 2 For j = 0 To n - i - 1 If a(j) a(j + 1) Then t = a(j + 1): a(j + 1) = a(j): a(j) = t End If Next j Next i Text3.Text = a(2)End Sub 在文本框Text1中放入一维数组元素个数n,在文本框Text2中放入n个数组元素,点击按钮Command1,找出这n个数中最小的数放入文本框Text3中,将该数在数组中的下标放入文本框Text4中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。要求数组元素下标从0开始。Private Sub Command1_Click()Dim n%, b%, c%, i%, a%() n = Val(Text1.Text)ReDim a( n - 1) TextToArray Text2.Text, ab = a(0): c = 0For i= LBound(a) To UBound(a) If a(i) a(j) Then k = a(i) a(i) = a(j) a(j) = k End If Next jNext iArrayToText a, Text3方法二:Private Sub Command1_Click()Dim n%, a%(), i%, j%, k%n = Val(Text1.Text)ReDim a(1 To n)TextToArray Text2, aFor i = LBound(a) To UBound(a) - 1 t= iFor j = i + 1 To UBound(a)If a(t) a(j) Then t = j Next j k = a(i) a(i) = a(j) a(j) = kNext iArrayToText a, Text3数组的输入输出: 逆序输出 点击按钮Command1,程序分别输入个数n以及n个整数,然后将这n个整数分别逆序输出(比如n=3,输入3个数分别3 9 7,输出 7 9 3)。使用InputBox和MsgBox完成输入输出。(1次输入或输出1个数据)Private Sub Command1_Click()Dim a%() , b%(),n%,i%,j%n = Val(InputBox(输入n个数)ReDim a(1 To n) b(1 To n)For i = 1 To na(i) = Val(InputBox(输入数字)Next iFor i = 1 To nb(i) = a(n - i + 1)MsgBox b(i)Next iEnd Sub 在文本框Text1中放入一维数组的5个元素,点击按钮Command1,将5个元素逆序放入文本框Text2中。使用ArrayToText和TextToArray完成数组与字符串之间的转换。Private Sub Command1_Click()Dim a(4) As Double, b(4) As DoubleTextToArray Text1, aFor i = o To 4b(i) = a(4 - i)Next iArrayToText b, Text2End Sub 行列交换 在文本框Text1中放入二维数组(2行2列)的4个元素,点击按钮Command1,将数组上下2行调换后放入文本框Text2中。使用ArrayToText和TextToArray完成数组与文本框之间的转换。 提示:设置文本框的MultiLine属性为True,文本框中才能显示多行数据 Option Base 1Private Sub Command1_Click()Dim a%(), t%ReDim a(2, 2)TextToArray T

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论