中国石油大学华东VB练习题(大一下)课件

更新时间:2023-03-08 05:09:32 阅读量: 高等教育 文档下载

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

中国石油大学华东VB练习题(大一下)

一、时钟 二、滚动条 三、列表框 四、复选框 五、单选框 六、组合框 七、多分枝选择 八、定义函数 九、TXT 十、数组

一、时钟

程序运行后,每隔2秒命令按钮Command1向右移动100个单位。

Private Sub Form_Load()

Timer1.Interval = 2 End Sub

Private Sub timer1_timer()

Call mymove End Sub

Sub mymove()

Command1.Left = Command1.Left + 200 End Sub

二、滚动条

水平滚动条HScroll1的数值范围0~4000,LargeChange设置为200,SmallChange设置为50,水平滚动条HScroll1的值(Value)代表命令按钮Command1的宽度,当水平滚动条的滑块变化时,命令按钮Command1的宽度随之改变

Private Sub Form_Load() HScroll1.Min = 0 HScroll1.Max = 4000

HScroll1.SmallChange = 50 HScroll1.LargeChange = 200

Command1.Caption = \驻倚靠危楼风细细,无言谁会凭栏意。衣带渐宽终不悔,为伊销得人憔悴\End Sub

Private Sub HScroll1_Change()

Command1.Width = Val(HScroll1.Value) End Sub

三、列表框

① 点击“->”按钮,将左边列表中的选中项移动到右边列表的末尾;点击“<-”按钮,将右边列表中的选中项移动到左边列表的末尾;点击“=>”按钮,将左边列表所有内容移动到右边列表的末尾;点击“<=”按钮,将右边列表所有内容移动到左边列表的末尾。

Private Sub Form_Load() List1.AddItem \List1.AddItem \List1.AddItem \List2.AddItem \List2.AddItem \End Sub

Private Sub Command1_Click() i = List1.ListIndex

If i >= 0 And i <= List1.ListCount - 1 Then List2.AddItem List1.Text List1.RemoveItem i End If End Sub

Private Sub Command2_Click() i = List2.ListIndex

If i >= 0 And i <= List2.ListCount - 1 Then List1.AddItem List2.Text List2.RemoveItem i End If End Sub

Private Sub Command3_Click() Dim i%

For i = 0 To List1.ListCount - 1

List2.AddItem List1.List(i) Next i List1.Clear End Sub

Private Sub Command4_Click() Dim i%

For i = 0 To List2.ListCount - 1 List1.AddItem List2.List(i) Next i List2.Clear End Sub

② 实现追加、插入、修改、删除、清空等

5个按钮的功能

Sub ClrList(AList As ListBox)

For i = AList.ListCount - 1 To 0 Step -1 AList.RemoveItem i Next i

End Sub

Private Sub Command1_Click() List1.AddItem Text1.Text End Sub

Private Sub Command2_Click() i = List1.ListIndex With List1

If i >= 0 And i <= .ListCount - 1 Then .AddItem Text1.Text, i End With End Sub

Private Sub Command3_Click() i = List1.ListIndex

If i >= 0 And i <= List1.ListCount - 1 Then List1.List(i) = Text1.Text End If End Sub

Private Sub Command4_Click() With List1

If .ListIndex > -1 Then _ .RemoveItem .ListIndex End With End Sub

Private Sub Command5_Click() ClrList List1 ' List1.Clear End Sub

Private 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 Sub

Private Sub Command2_Click()

List1.Text = List1.List(List1.ListIndex - 1) End Sub

Private Sub Command3_Click()

List1.Text = List1.List(List1.ListIndex + 1) End Sub

Private Sub Command4_Click()

List1.Text = List1.List(List1.ListCount - 1) End Sub

四、复选框

① 窗体中4个复选框分别代表4各班级,复选框中的标题文字代表班级人数,用户选择复选框后,点击命令按钮Command1,程序计算出所选班级的总人数,结果放入文本框Text1中。

Private Sub Command1_Click() Dim s%

If Check1.Value = 1 Then s = s + Val(Check1.Caption) End If

If Check2.Value = 1 Then s = s + Val(Check2.Caption) End If

If Check3.Value = 1 Then s = s + Val(Check3.Caption) End If

If Check4.Value = 1 Then s = s + Val(Check4.Caption) End If

Text1.Text = Str(s) End Sub

Private Sub Form_Load() Check1.Caption = 30 Check2.Caption = 28 Check3.Caption = 31 Check4.Caption = 29 Label1.Caption = \人数\Text1.Text = \

End Sub

②用户选择复选框后,点击命令按钮Command1,程序根据复选框对文本框Text1

中的字体进行相应的设置。

Private Sub Command1_Click()

If Check1.Value = 1 Then Text1.FontBold = True If Check2.Value = 1 Then Text1.FontItalic = True If Check3.Value = 1 Then Text1.FontUnderline = True End Sub

五、 单选框

用户在数和算法两个框架中各选一个单选按钮,点击命令按钮Command1,程序根据选中单选按钮进行相应的计算,结果放入文本框Text1中。

Private Sub Command1_Click() Dim a%, b%, c%, d% k = 1: t = 0

a = Val(Option1.Caption) b = Val(Option2.Caption)

If Option1.Value And Option3.Value Then

For i = 1 To a k = k * i Next i Text1.Text = k

ElseIf Option1.Value And Option4.Value Then For i = 1 To a t = t + i Next i Text1.Text = t

ElseIf Option2.Value And Option3.Value Then For i = 1 To b k = k * i Next i Text1.Text = k

ElseIf Option2.Value And Option4.Value Then For i = 1 To b t = t + i Next i Text1.Text = t End If End Sub

五、组合框

字体组合框包括宋体、黑体、幼圆、隶书4个列表项;字号组合框列表项的值从12~48,以4递增。选择字体组合框或者字号组合框中的某一项,点击按钮Command1,文本框中的字体和字号进行相应的改变。

Private Sub Command1_Click() Text1.FontName = Combo1.Text Text1.FontSize = Combo2.Text End Sub

Private Sub Form_Load() Combo1.AddItem \宋体\Combo1.AddItem \黑体\Combo1.AddItem \幼圆\Combo1.AddItem \隶书\For i = 12 To 48 Step 4 Combo2.AddItem i Next i

Text1.Text = \End Sub

七、多分枝

①加密:点击加密按钮Command1,输入1个小写英文字母,程序将该英文字母变为字母表中其后面的一个字母输出,如a改为b,m改为n,z改为a,以此类推。点击解密按钮Command2,输入1个小写英文字母,程序将其解密后通过输出。使用InputBox和MsgBox完成输入输出。 Private Sub Command1_Click() Dim a As String, b As String a = InputBox(\If Asc(a) = 122 Then b = Chr(97)

Else: b = Chr(Asc(a) + 1) End If MsgBox b End Sub

Private Sub Command2_Click() Dim c As String, d As String

c = InputBox(\If Asc(c) = 97 Then d = Chr(122)

Else: d = Chr(Asc(c) - 1) End If MsgBox d End Sub

②铁路收费:铁路行李托运费计算规则为:行李重量不超过50kg时,每千克0.25元;超过50kg不超过100kg时,其超过部分每千克0.35元;超过100kg时,其超过部分每千克0.45元。点击按钮Command1,程序输入行李重量,计算并输出托运费用。使用InputBox和MsgBox完成输入输出。 Private Sub Command1_Click() Dim a As Double, b As Double a = Val(InputBox(\If a >= 0 And a <= 50 Then b = a * 0.25

ElseIf a <= 100 Then b = 12.5 + (a - 50) * 0.35 ElseIf a >= 100 Then b = 30 + (a - 100) * 0.45 End If

MsgBox b

End 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 b

End Sub (也可用select case 语句或者直接限定范围进行选择)

八、定义函数:

①【一个月有几天】一个月的天数:在窗体Form1中实现

函数Days。参数y和m分别表示年份和月份,函数返回该年该月的天数。 提示:闰年2月份有29天,平年有28天。 闰年的条件:1)年份能被4整除,但不能被100整除;2)或年份能被400整除 Public Function Days(y As Long, m As Long) As Long …… End Function

Function 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 b3 End Function

Public 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 Select End Function

Private Sub Command1_Click() Dim a&, b&, c& a = Val(InputBox(\ b = Val(InputBox(\ c = days(a, b) MsgBox c End 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 Function

Public 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 Then Triangle = 0: p = 0 Else

p = a + b + c l = p / 2

Triangle = Sqr(l * (l - a) * (l - b) * (l - c)) End If End Function

Private Sub Command1_Click() Dim a&, b&, c&, p&, s# a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) s = Triangle(a, b, c, p) Text4.Text = str(p) Text5.Text = str(s) End Sub

③【计算总和】: 在窗体Form1中实现函数Sum。参数a()为输入的数组,函数返回数组中所有元素之和。 Public Function Sum(a() As Long) As Long …… End Function Function sum(a() As Long) As Long sum = 0

For i = LBound(a) To UBound(a) sum = sum + a(i) Next i End Function

Private Sub Command1_Click() Dim a(3) As Long TextToArray Text1, a MsgBox sum(a) End Sub

④【排序】: 在窗体Form1中实现子程序Sort。参数a()为数组,子程序Sort对该数组按正序(从小到大)排序。 Public Sub Sort(a() As Long) …… End Sub Public Sub sort(a() As Long) Dim i%, j%, k%, p%

For i = LBound(a) To UBound(a) - 1 p = i

For j = i + 1 To UBound(a) If a(p) > a(j) Then p = j

Next j

k = a(i)

a(i) = a(p) a(p) = k

Next i

End 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 Long a = Val(InputBox(\b = Val(InputBox(\c = Val(InputBox(\

MsgBox triangleclass(a, b, c)

End Sub

Public Function triangleclass(a As Long, b As Long, c As Long) As String Dim s$

If a + b <= c Or b + c <= a Or a + c <= b Then

s = \不构成三角形\

ElseIf a = b And a = c And b = c Then

s = \等边三角形 \

ElseIf a = b Or b = c Or a = c Then

s = \等腰三角形\

Else:

s = \一般三角形\End If

triangleclass = s End Function

⑥【最大值】: 在窗体Form1中实现函数Max。参数a()为输入

的数组,函数返回数组中的最大值。 Public Function Max(a() As Long)

As Long …… End Function

Option Base 1

Private Sub Form_Load() Dim n %, a%() ,i% n = Val(InputBox(\ReDim a(n) For i = 1 To n

a(i) = Val(InputBox(\Next i

MsgBox max(a()) End Sub

Public Function max(a() As Long) As Long Dim i As Integer max = a(LBound(a))

For i = LBound(a) To UBound(a) If a(i) > max Then max = a(i) Next i End 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 - 1

For j = i + 1 To n If a(j) > a(i) Then

t = a(i)

a(i) = a(j) a(j) = t End If Next j Next i End Sub

⑧【完数】:在窗体Form1中实现函数Perfect。如果参数x为完全

数,该函数返回True;否则返回False。(完全数:真因子(除自身以外的因子)之和为其本身) Public Function Perfect(x As Long) As Boolean …… End Function

Private Sub Command1_Click() Dim x As Long

x = Val(InputBox(\MsgBox perfect(x) End Sub

Public Function perfect(x As Long) As Boolean For i = 0 To x - 1 If x Mod i = 0 Then s = s + i End If Next i If s = x Then perfect = True Else

perfect = False

End If End Function

⑨【符号函数】 在窗体Form1中实现函数MySgn。当参数x>0时,函数MySgn返回1;当参数x<0时,函数MySgn返回-1;当参数x=0时,函数MySgn返回0。 Public Function MySgn(x As Long) As Long …… End Function

Public Function MySgn(ByVal x As Long) As Long If x > 0 Then MySgn = 1 ElseIf x = 0 Then MySgn = 0 Else

MySgn = -1 End If End Function

九、TXT:

①复制文件: 复制文件。点击Command1按钮,程序将文件a.txt

中的内容复制到文件b.txt中

Private Sub Command1_Click()

Dim a$

Open \ Open \

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

Top