教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 互联网资料 >

《VB程序设计》课堂例题总结(8)

来源:网络收集 时间:2026-08-02
导读: 已知多边形各条边及对角线的长度,要计算多边形的面积。 Private Sub Command1_Click() Dim a!, b!, c!, d!, e!, f!, g!, s1!, s2!, s3! a = InputBox(输入边长a) b = InputBox(输入边长b) c = InputBox(输入边长c)

已知多边形各条边及对角线的长度,要计算多边形的面积。 Private Sub Command1_Click()

Dim a!, b!, c!, d!, e!, f!, g!, s1!, s2!, s3! a = InputBox(输入边长a) b = InputBox(输入边长b) c = InputBox(输入边长c) d = InputBox(输入边长d) e = InputBox(输入边长e) f = InputBox(输入边长f) g = InputBox(输入边长g) s1 = area(a, b, c) s2 = area(c, d, e) s3 = area(e, f, g) Print s1 + s2 + s3 End Sub

Public Function area(x!, y!, z!) As Single Dim c!

c = 1 / 2 * (x + y + z)

area = Sqr(c * (c - x) * (c - y) * (c - z)) End Function

编写判断素数的函数过程Prime(n),函数的返回值类型为布尔型。(重点) Function Prime(n%) As Boolean tag = True

For i = 2 To n - 1

If n Mod i = 0 Then tag = False Exit For End If Next Prime = tag End Function

Private Sub Text1_KeyPress(KeyAscii As Integer) Dim tag As Boolean,i%,n% If KeyAscii = 13 Then n = Val(Text1.Text)

If Prime(n) = True Then P1.Print n;”是素数” Else

P1.Print n;”不是素数” End If End If End Sub

36

判断素数,并求范围内的素数之和。(重点)

Dim sum%

Private Sub Command1_Click() Dim n1%, n2%, i% sum = 0

If Option1.Value Then a = 100: b = 200 Else

a = 200: b = 400 End If

For i = a To b

If prime(i) Then sum = sum + i Next i

Text1 = sum End Sub

Function prime(n%) As Boolean prime = True

For i = 2 To Sqr(n) If n Mod i = 0 Then prime = False Exit For End If Next i

End Function

编写求一维数组的最小值的子过程或函数。 Option Base 1

Dim arr(10), b(), s()

Private Sub Command1_Click() Dim i%

For i = 1 To 10

arr(i) = Int(Rnd * 101) + 100 Print arr(i); Next i Print

b = Array(18, 12, 65, 42, 85, 96, 68, 75) s = Array(\

Print \

37

Print \ Print End Sub

Function FunMin(a()) As Variant ?函数过程 Dim i%, min

min = a(LBound(a))

For i = LBound(a) + 1 To UBound(a) If a(i) < min Then min = a(i) Next

FunMin = min End Function

Sub ProcMin(a(), min) ?子过程 Dim i%

min = a(LBound(a))

For i = LBound(a) + 1 To UBound(a) If min > a(i) Then min = a(i) Next i End Sub

Private Sub Command2_Click() Print \调用过程求最小:\ ProcMin arr, mm

Print \数组的最小值为\ ProcMin b, mm

Print \数组的最小值为\ ProcMin s, mm

Print \数组的最小值为\ Print End Sub

Private Sub Command3_Click() Print \调用函数求最小:\

Print \数组的最小值为\ Print \数组的最小值为\ Print \数组的最小值为\ Print End Sub

把文本框Mytxt的内容,写入文件Myfile.dat中 方法1:把整个文本框的内容一次性地写入文件。 Open App.Path + “\\Myfile.dat\ For Output As #1 Print #1, Mytxt.Text Close #1

方法2:把整个文本框的内容一个字符一个字符地写入文件。 Open “.\\Myfile.dat\For i=1 To Len(Mytxt.Text)

38

Print #1,Mid(Mytxt.Text,i,1); Next i Close #1

随机产生20个[50,100]的随机整数,存入数组中,并把数组的各元素值(按每行5个元素输出)写入文件MyList.dat中 Private Sub Command1_Click() Dim a%(20), i%

Open “D:\\Mylist.dat\ ?打开文件 For i = 1 To 20

a(i) = Int(Rnd * 51) + 50

Print #1, a(i), ?写文件

If i Mod 5 = 0 Then Print #1, ?在文本文件中换行 Next

Close #1 ?关闭文件 End sub

将文本文件MYFILE.TXT的内容读到文本框Text1中。 Text1.Text = \ Open “D:\\ myfile.txt\ Do While Not EOF(1) Line Input #1, s Text1.Text = Text1.Text & s & vbCrLf Loop Close #1

将文件MyList.dat的数据读出来存放到数组中,并在列表框中显示 Option Base 1

Private Sub Command2_Click() Dim a(), i%

Open \ i = 0

Do While Not EOF(1) i = i + 1

ReDim Preserve a(i) Input #1, a(i)

List1.AddItem a(i) Loop Close #1 End Sub

三种读文件方式的区别

Private Sub Command1_Click()

Open \

39

Do While Not EOF(1) Line Input #1, s List1.AddItem s Loop Close End Sub

Private Sub Command2_Click()

Open \Do While Not EOF(1) s = Input(1, 1) List2.AddItem s Loop Close End Sub

Private Sub Command3_Click()

Open \Do While Not EOF(1) Input #1, s List3.AddItem s Loop Close End Sub

过程(自定义函数与子过程)与文件读写结合 Option Base 1 Dim a%(4, 4)

Dim max%, line3%, num% Private Sub Form_Load() Randomize Label2 = \End Sub

Private Sub Command1_Click(Index As Integer) Dim i%, j%

Select Case Index Case 0

Open App.Path & \ '打开数据文件,准备写入 For i = 1 To 16

num = Int(Rnd * 30 + 1)

Print #2, num; '将数据写入磁盘文件

If i Mod 4 = 0 Then Print #2, '在数据文件中换行 Next i Close #2

MsgBox \已写入文件。\ Case 1

40

Open App.Path & \ '打开数据文件,准备读出 For i = 1 To 4 For j = 1 To 4

Input #2, a(i, j) '读磁盘文件中的一个数据,并赋值给数组元素

s$ = IIf(Len(CStr(a(i, j))) = 1, \ \ \ '为了标签中输出规整,判断数据是1位还是2位,分别空3个或2个空格 Label2 = Label2 & a(i, j) & s Next j

Label2 = Label2 & vbCrLf '换行 Next i Close #2 Case 2

max% = a(1, 1) For i = 1 To 4 For j = 1 To 4

If a(i, j) > max Then max = a(i, j) Next j Next i

Label3 = \最大值:\ Case 3

For j = 1 To 4

line3 = line3 + a(3, j) Next j

Label4 = \第三行元素之和:\ Case 4

Open App.Path & \ Print #3, max, line3 Close

MsgBox \已写入文件\ Case Else End End Select End Sub

41

'打开数据文件,准备追加数据

…… 此处隐藏:1983字,全部文档内容请下载后查看。喜欢就下载吧 ……
《VB程序设计》课堂例题总结(8).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/443196.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)