好文档 - 专业文书写作范文服务资料分享网站

VB程序设计教程(第3版)课后实验答案

天下 分享 时间: 加入收藏 我要投稿 点赞

实验A A.1

Private Sub Command1_Click() Label3 = Text1 End Sub

A.2

Private Sub Form_Load() Timer1.Interval = 0 End Sub

Private Sub Command1_Click() '自动 Timer1.Interval = 200 End Sub

Private Sub Command2_Click() '手动 Timer1.Interval = 0 Call MyMove End Sub

Private Sub Timer1_Timer() Call MyMove End Sub

Sub MyMove()

Label1.Move Label1.Left - 50

If Label1.Left < 0 Then Label1.Left = Form1.Width End Sub

A.3

Private Sub Form_Click() Text1 = Val(Text1) + 1 End Sub

Private Sub Form_Load() Text1 = 0 End Sub

A.4

Private Sub Form_Click()

Caption = \单击窗体,改变图片\

Picture = LoadPicture(App.Path + \ Print \欢迎使用VB\End Sub

Private Sub Form_DblClick() Cls

Caption = \双击窗体,卸去图片\

Picture = LoadPicture(\ ' End Sub

Private Sub Form_Load() Caption = \装入窗体\

Picture = LoadPicture(App.Path + \ Print \装入图\End Sub

Private Sub Form_Resize() ' 该事件的作用窗体始终与图一样大 'Caption = \窗体大小不变\

'Form1.Width = 260 * 16 ' 260是Tongji-2.bmp图的宽度,象素单位

'Form1.Height = 260 * 16 + 200 ' 260是图的高度,象素单位,200是窗体的标题栏高度 End Sub

A.5

Sub Command1_Click()

Text1.FontName = \隶书\ Text1.FontSize = 25 End Sub

Sub Command2_Click()

Text2.Text = Text1.SelText

Text2.FontName = Text1.FontName Text2.FontSize = Text1.FontSize End Sub

实验B B.1

Private Sub Command1_Click()

Text2 = Format(5 / 9 * (Val(Text1) - 32), \End Sub

Private Sub Command2_Click()

Text1 = Format(9 / 5 * Val(Text2) + 32, \End Sub 或

Private Sub Command1_Click()

Dim f!, c! ' 声明两个变量 f = Val(Text1) c = 5 / 9 * (f - 32)

Text2 = Format(c, \ ' 保留两位小数 End Sub

Private Sub Command2_Click()

Dim ff!, cc! ' 声明两个变量 cc = Val(Text2) ff = 9 / 5 * cc + 32

Text1 = Format(ff, \ ' 保留两位小数 End Sub

B.2

Private Sub Command1_Click()

Label2 = Format(Val(Text1) * Val(Text1) * 3.14, \End Sub

Private Sub Command2_Click()

Label3 = Format(Val(Text1) * 3.14 * 2, \End Sub

Private Sub Text1_LostFocus()

If Not IsNumeric(Text1.Text) Then

MsgBox \输入有非数字字符,请重新输入\警告\ Text1.Text = \ Text1.SetFocus End If End Sub 或

Private Sub Command1_Click()

Label2 = Format(Val(Text1) * Val(Text1) * 3.14, \End Sub

Private Sub Command2_Click()

Label3 = Format(Val(Text1) * 3.14 * 2, \End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then

If Not IsNumeric(Text1.Text) Then Text1.Text = \ End If End If End Sub

B.3

Private Sub Command1_Click() n = Int(Log(2) / Log(1.008) + 1) Label1 = n & \年后人数超过26亿\End Sub

B.4

Private Sub Command1_Click() Dim x, dx, cd x = Text1.Text dx = UCase(x) cd = Len(x)

Print \大写字母为:\ Print \字符串长度为:\End Sub

B.5

Private Sub Command1_Click() Text1 = Int(Rnd * 900 + 100) End Sub

Private Sub Command2_Click() Dim x%, x1%, x2%, x3% x = Val(Text1)

x1 = x Mod 10 ' 分离出的个位数 x2 = (x Mod 100) \\ 10 ' 分离出的十位数 x3 = x \\ 100 ' 分离出的百位数 Label1 = x1 * 100 + x2 * 10 + x3 End Sub

B.6

Private Sub Form_Click() Label1 = Left(Text1, 11)

Label2 = Mid(Text1, 12, 6) Label3 = Right(Text1, 5) End Sub

B.7

Private Sub Command1_Click() Print

For i = 1 To 5

Print Tab(15 - i * 2); String(2 * i - 1, \★\★\ Next i End Sub

Private Sub Command2_Click() Cls End Sub

进一步要求:

Private Sub Command1_Click() Print

For i = 1 To 5

Print Tab(15 - i * 2); String(2 * i - 1, \★\☆\★\

Next i End Sub

Private Sub Command2_Click() Cls

End Sub

B.8

Private Sub Form_Click()

x = Val(InputBox(\输入一正实数\计算\pf = Format(x * x, \pfg = Format(Sqr(x), \lf = Format(x * x * x, \lfg = Format(x ^ (1 / 3), \

Print \平方为:\平方根为:\立方为:\立方根为:\lfg

End Sub

实验C C.1

Private Sub Form_Click() Dim x!, y!

x = Val(InputBox(\输入x的值\If x < 1000 Then y = x

ElseIf x < 2000 Then y = 0.9 * x

ElseIf x < 3000 Then y = 0.8 * x Else y = 0.7 * x End If Print y End Sub

C.2

Private Sub Form_Click() Dim x!, y!

x = Val(InputBox(\输入上网时间\If x < 10 Then y = 30

ElseIf x < 50 Then y = 30 + 2.5 * (x - 10) Else

y = 30 + 2.5 * 40 + 2 * (x - 50) End If

If y > 150 Then y = 150

End If Print y End Sub

C.3

Private Sub Command1_Click() Dim x!, y!, z!

x = InputBox(\ y = InputBox(\ z = InputBox(\

Print \ x y z\ Print \排序前\ \ \ If x < y Then t = x: x = y: y = t If x < z Then t = x: x = z: z = t If y < z Then t = y: y = z: z = t

Print \排序后\ \ \End Sub

Private Sub Command2_Click() Dim x!, y!, z!

x = InputBox(\ y = InputBox(\ z = InputBox(\

Print \ x y z\ Print \排序前\ \ \ If x < y Then t = x: x = y: y = t If y < z Then t = y: y = z: z = t If x < y Then

t = x: x = y: y = t End If End If

Print \排序后\ \ \End Sub

C.4

Dim a(3) As Integer

Private Sub Command1_Click() Picture1.Cls For i = 0 To 2

a(i) = Int(Rnd * 100 + 200) Picture1.Print a(i) Next i End Sub

Private Sub Command2_Click()

Picture2.Cls Dim z As Integer For i = 0 To 1

If a(i) > a(i + 1) Then z = a(i + 1) a(i + 1) = a(i) a(i) = z End If Next i

Picture2.Print a(0) Picture2.Print a(1) Picture2.Print a(2) End Sub

C.5

Private Sub Text2_LostFocus() Dim m%, n%, y% m = Val(Text1) n = Val(Text2)

If n Mod 2 <> 0 Then

MsgBox (\脚数必定为偶数\ Text2 = \

Text2.SetFocus Else

y = n / 2 - m If y < 0 Then

MsgBox (\脚数必须≥2倍的头数,请重新输入\ Text2 = \

Text2.SetFocus Else

x = n / 2 - m Label2 = y Label3 = m - y End If End If End Sub

C.6

Private Sub Command1_Click() Dim a!, b!, c!, x1!, x2!, de! a = Text1 b = Text2 c = Text3

de = b * b - 4 * a * c

t = 2 * a

If de = 0 Then

Text4 = Format(-b / t, \ Text5 = Format(-b / t, \ElseIf de > 0 Then

Text4 = Format((-b + Sqr(de)) / t, \ Text5 = Format((-b - Sqr(de)) / t, \Else

Text4 = Format(-b / t, \ Text5 = Format(-b / t, \End If End Sub

Private Sub Command2_Click() Text1 = \Text2 = \Text3 = \Text4 = \Text5 = \End Sub

C.7

Private Sub Text3_LostFocus() Select Case Trim(Text3) Case \

Text4 = Val(Text1) + Val(Text2) Case \

Text4 = Val(Text1) - Val(Text2) Case \

Text4 = Val(Text1) * Val(Text2) Case \

If Val(Text2) = 0 Then

MsgBox \分母为零,重新输入\ Text2 = \

Text2.SetFocus Else

Text4 = Val(Text1) / Val(Text2) End If End Select End Sub

C.8

Private Sub Text1_LostFocus() Select Case Trim(Text1) Case 1

Text2 = \ Case 2

Text2 = \ Case 3

Text2 = \ Case 4

Text2 = \ Case 5

Text2 = \ Case 6

Text2 = \ Case 7

Text2 = \ Case Is > 7, Is < 1

MsgBox \数字为1~7,重新输入\ Text1 = \

Text1.SetFocus End Select End Sub 或者

Private Sub Text1_LostFocus() If Text1 > 7 Or Text1 < 1 Then

MsgBox \数字为1~7,重新输入\ Text1 = \

Text1.SetFocus Else

Text2 = Choose(Text1, \\End If End Sub

实验D D.1

Private Sub Form_Click() For i = 1 To 9

Print Tab(10 - i); String(2 * i - 1, Trim(Str(i))) Next i End Sub

D.2

Private Sub Form_Click() For i = 1 To 10 Step 1

Print Tab(i); String((20 - 2 * i), Chr(64 + i))

Next i End Sub

D.3

Private Sub Command1_Click() Dim s!, t!, i& s = 1 t = 1

For i = 1 To 100000 t = t + i s = s + 1 / t

If 1 / t < 0.00001 Then Exit For Next i

Print \结构\项\End Sub

D.4

Private Sub Command1_Click() Dim n&, pi#, i&

n = InputBox(\输入n值\pi = 2

For i = 1 To n

pi = pi * (2 * i) / (2 * i - 1) * (2 * i) / (2 * i + 1) Next i

Print \当n=\时,pi=\End Sub

D.5

Private Sub Form_Click() Dim s!, t!, i!, a%, n% a = Int(Rnd * 9 + 1) n = Int(Rnd * 6 + 5) t = 0: s = 0

Print \ For i = 1 To n t = t * 10 + a s = s + t Print t; Next i Print

Print \End Sub

D.6

Private Sub Command1_Click() Dim s As Integer s = 0

For i = 1 To 9 For j = 0 To 9 For k = 0 To 9

s = i * 100 + j * 10 + k

If s = i ^ 3 + j ^ 3 + k ^ 3 Then Print s End If Next k Next j Next i End Sub

Private Sub Command3_Click() formd6.Hide main.Show End Sub

D.7

Private Sub Command1_Click() Dim a!, x0!, x1! a = 27 x0 = 2 i = 0 Do

i = i + 1

x1 = 2 * x0 / 3 + a / (3 * x0 * x0)

If Abs(x1 - x0) < 0.00001 Then Exit Do x0 = x1 Loop Print x1, i End Sub

D.8

Private Sub Command1_Click() s = 0 x0 = 0.01

For i = 1 To 30 s = s + x0 x0 = x0 * 2 Next i Print s End Sub

D.9

Private Sub Command4_Click() Picture1.Cls

Picture1.Print \课安排在 \课安排在 \课安排在 \Picture1.Print \For z = 5 To 6 For x = 1 To z - 2 For Y = x + 1 To z - 1

X1 = Choose(Weekday(x), \周一\周二\周三\周四\周五\周六\周日\Y1 = Choose(Weekday(Y), \周一\周二\周三\周四\周五\周六\周日\z1 = Choose(Weekday(z), \周一\周二\周三\周四\周五\周六\周日\Picture1.Print \ \ \Next Y Next x Next z End Sub

实验E

E.1

Private Sub Form_Click() Dim a(1 To 10) As Integer For i = 1 To 10

a(i) = Int(Rnd * 71 + 30) Print a(i); \ Next i

Max = a(1) Min = a(1) Avg = a(1) For i = 2 To 10

If a(i) > Max Then Max = a(i) If a(i) < Min Then Min = a(i) Avg = Avg + a(i) Next i

Avg = Avg / 10 Print

Print \ Min=\ Avg=\End Sub

E.2

Private Sub Form_Click() Dim a

a = Array(56, 78, 98, 88, 76, 78) For i = 0 To 5

Print String(a(i) \\ 5, \◆\ Print Next i End Sub

E.3

Dim a%(19)

Private Sub Command1_Click() Picture1.Cls

For i = 0 To 19

a(i) = Int(Rnd * 101) Picture1.Print a(i); \ \

If (i + 1) Mod 4 = 0 Then Picture1.Print Next i End Sub

Private Sub Command2_Click() Picture2.Cls Dim s(5 To 9) For i = 0 To 19 k = a(i) \\ 10 Select Case k Case 0 To 5

s(5) = s(5) + 1

Case 9 To 10 '90~100分的人数 s(9) = s(9) + 1

Case 6 To 8 ' 存放其他三个分数段的下标有规律,根据K获得 s(k) = s(k) + 1 End Select Next i

For i = 5 To 9

If s(i) <> 0 Then Picture2.Print \的人数有 \个\ Next i End Sub

E.4

Private Sub Command1_Click() Picture1.Cls

Dim d%(1 To 10) For i3 = 1 To 10 Randomize

d(i3) = Int(Rnd * 91 + 10) Next i3

For i = 1 To 10

For j = 1 To 10 - i If d(j) < d(j + 1) Then

t = d(j): d(j) = d(j + 1): d(j + 1) = t End If Next j Next i

For i = 1 To 10: Picture1.Print d(i); If i Mod 5 = 0 Then Picture1.Print Next i End Sub

E.5

Dim a%(3, 3), b%(3, 3) Private Sub Form_Load() For i = 0 To 3 For j = 0 To 3

a(i, j) = Int(Rnd * 36 + 35) b(i, j) = Int(Rnd * 41 + 100) Next j Next i End Sub

Private Sub Command1_Click() Picture1.Cls For i = 0 To 3 For j = 0 To i

Picture1.Print a(i, j); \ \ Next j

Picture1.Print Next i End Sub

Private Sub Command2_Click() Picture2.Cls For i = 0 To 3 For j = i To 3

Picture2.Print Tab(j * 6); b(i, j); Next j

Picture2.Print Next i End Sub

Private Sub Command3_Click() Picture3.Cls sa = 0

For i = 0 To 3

sa = sa + a(i, i) Next i sb = 0

For i = 0 To 3

sb = sb + b(i, 3 - i) Next i

Picture3.Print \数组主对角线元素和为:\ Picture3.Print \数组副对角线元素和为:\End Sub

E.6

Private Sub Form_Click() n = InputBox(\输入n值\ReDim a6%(n + 1, n + 1)

a6(0, 0) = 1: a6(1, 1) = 1: a6(1, 0) = 1 For i = 2 To n + 1 For j = 2 To i

a6(i, j) = a6(i - 1, j - 1) + a6(i - 1, j) Picture1.Print a6(i, j); \ Next j

Picture1.Print Next End Sub

E.7

Private Sub Form_Load() List1.Clear

List1.AddItem \大学计算机基础\ List1.AddItem \程序设计\ List1.AddItem \程序设计\ List1.AddItem \程序设计\ List1.AddItem \多媒体技术与应用\ List1.AddItem \数据库技术与应用\ List1.AddItem \网络技术与应用\ List1.AddItem \硬件技术基础\ List1.AddItem \软件技术技术基础\End Sub

Private Sub List1_Click()

If List2.ListCount >= 5 Then

MsgBox (\超过5门课程,不能再选\ Exit Sub Else

List2.AddItem List1.Text

List1.RemoveItem List1.ListIndex End If End Sub

E.8

Sub Combo1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case 48 To 57, 13 Case Else

KeyAscii = 0 End Select

If KeyAscii = 13 Then

Combo1.AddItem Combo1.Text Combo1.Text = \ End If End Sub

Private Sub Command1_Click() Dim min%, max%

min = Val(Combo1.List(0)) max = Val(Combo1.List(0)) imin = 0 imax = 0

For i = 1 To Combo1.ListCount - 1 If Val(Combo1.List(i)) > max Then imax = i

max = Combo1.List(i)

ElseIf Val(Combo1.List(i)) < min Then imin = i

min = Combo1.List(i) End If Next i

t = Combo1.List(0)

Combo1.List(0) = Combo1.List(imin) Combo1.List(imin) = t

t = Combo1.List(Combo1.ListCount - 1)

Combo1.List(Combo1.ListCount - 1) = Combo1.List(imax) Combo1.List(imax) = t End Sub

E.9

Private Sub Form_Click()

For i = 0 To Screen.FontCount - 1

If Asc(Left(Screen.Fonts(i), 1)) < 0 Then

Picture1.Print Screen.Fonts(i)

Picture2.FontName = Screen.Fonts(i) Picture2.Print \商丘师范学院\ End If Next i End Sub

E.10

Private Type clerk

number As String * 3 name As String * 5 salary As Integer End Type

Dim a(0 To 4) As clerk, n%

Private Sub Command1_Click() If n >= 5 Then

MsgBox (\输入人数超过数组声明的个数\Else

With a(n)

.number = Text1 .name = Text2 .salary = Text3

Picture1.Print a(n).number, a(n).name, a(n).salary Print End With n = n + 1 Text1 = \ Text2 = \ Text3 = \End If End Sub

Private Sub Command2_Click() Dim t As clerk, i%, j% For i = 0 To n - 1 For j = i To n - 1

If a(i).salary < a(j + 1).salary Then t = a(i): a(i) = a(j + 1): a(j + 1) = t End If Next j Next i

Picture1.Cls

Picture1.Print \工号 姓名 工资\

For i = 0 To n - 1

Picture1.Print a(i).number, a(i).name, a(i).salary Print Next i End Sub

实验F F.1

Private Sub Form_Click() Dim a(1 To 10), amin, i% For i = 1 To 10

a(i) = -Int(Rnd * 101 + 300) Print a(i); Next i

Call s(a(), amin) Print

Print \End Sub

Sub s(b(), min) Dim i%

min = b(LBound(b))

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

F.2

Private Sub Command1_Click() Dim mm%, nn% mm = Val(Text1) nn = Val(Text2)

Picture1.Print mm; Tab(6); nn; Tab(12); gcd(mm, nn) End Sub

Function gcd%(ByVal m%, ByVal n%) If m < n Then t = m: m = n: n = t r = m Mod n

Do While (r <> 0)

m = n: n = r: r = m Mod n Loop gcd = n End Function

F.3

Dim x!

Private Sub Command1_Click()

Print \调用标准函数Sin的结果\End Sub

Private Sub Command2_Click()

Print \调用自定义函数 MySin的结果\End Sub

Function MySin(x!) As Double Dim i%, t!, s! t = x s = t i = 1

Do While Abs(t) > 0.00001

t = -1 * t * x * x / ((i + 1) * (i + 2)) s = s + t i = i + 2 Loop

MySin = s End Function

Private Sub Command3_Click()

x = InputBox(\输入要计算正弦函数的角度值x\x = x * 3.14 / 180 End Sub

F.4

Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 Then

If Not IsNumeric(Text1) Then

MsgBox \输入非数字串,重新输入\ Text1.Text = \ Text1.SetFocus Else

If IsH(Text1) Then

Picture1.Print Text1; \★ \ Else

Picture1.Print Text1 End If Text1 = \ End If End If End Sub

Function IsH(ss As String) As Boolean Dim i%, Ls% IsH = True

ss = Trim(ss) Ls = Len(ss)

For i = 1 To Ls \\ 2

If Mid(ss, i, 1) <> Mid(ss, Ls + 1 - i, 1) Then IsH = False Exit Function End If Next i End Function

F.5

Function prime(ByVal m As Integer) As Boolean prime = True Dim i%

For i = 2 To m - 1

If (m Mod i) = 0 Then prime = False: Exit Function '注意冒号和exit的范围 Next i

End Function

Private Sub Command1_Click() n = 0

For i = 6 To 100 Step 2 For j = 3 To i \\ 2 If prime(j) Then If prime(i - j) Then

List1.AddItem i & \n = n + 1 End If End If Next j Next i

Picture1.Print \和100之间共有\对素数和\End Sub

F.6

Dim a%()

Private Sub Form_Click()

Print \以内的完数为:\ For i = 1 To 1000 If IsWs(i) Then Print i; \

For j = 1 To UBound(a) Print \ Next j

Print End If Next i End Sub

Function IsWs(m) As Boolean Dim s% s = 0

For i = 1 To m \\ 2

If m Mod i = 0 Then ReDim Preserve a(j) a(j) = i j = j + 1 s = s + i End If Next i

If m = s Then IsWs = True End Function

F.7

Private Sub DeleStr(s1 As String, ByVal s2 As String) Dim i%

ls2 = Len(s2) i = InStr(s1, s2) Do While i > 0

s1 = Left(s1, i - 1) + Mid(s1, i + ls2) ' 在s1中去除s2子串 i = InStr(s1, s2) Loop End Sub

Private Sub Command1_Click() ' 调用DeleStr子过程 Dim ss1 As String ss1 = Text1

Call DeleStr(ss1, Text2) Text3 = ss1 End Sub

Private Sub Command2_Click()

End Sub

F.8

Private Sub Command1_Click() Dim maxw$

maxlen Text1 & \

Text2 = maxw End Sub

Sub maxlen(s$, maxw$) Dim word$ maxw = \

Do While s <> \ i = InStr(s, \

word = Left(s, i - 1)

If Len(word) > Len(maxw) Then maxw = word s = Mid(s, i + 1) Loop End Sub

实验G G.1

Private Sub Command1_Click() List1.Clear

List1.AddItem Combo1

If Option1 Then List1.AddItem \ If Option2 Then List1.AddItem \ If Option3 Then List1.AddItem \ List1.AddItem Text1

If Check1 Then List1.AddItem \声卡\ If Check2 Then List1.AddItem \ If Check3 Then List1.AddItem \网络适配器\End Sub

Private Sub Text1_LostFocus() st = UCase(Trim(Text1)) le = Len(st)

If Not IsNumeric(Left(st, le - 2)) Or Right(st, 2) <> \ MsgBox \有不合法字符!\ Text1 = \

Text1.SetFocus End If End Sub

G.2

Private Sub Check1_Click()

Picture1.Font.Bold = Not Picture1.Font.Bold End Sub

Private Sub Check2_Click()

Picture1.Font.Italic = Not Picture1.Font.Bold End Sub

Private Sub Command1_Click() Picture1.Cls If Option1 Then

Picture1.Print Sin(Val(Text1)) ElseIf Option2 Then

Picture1.Print Exp(Val(Text1)) ElseIf Option3 Then

Picture1.Print Sqr(Val(Text1)) End If End Sub

Private Sub return_Click(Index As Integer) Form6.Show Unload Form2 End Sub

G.3

Private Sub HScroll1_Change() Text1 = VScroll1.Value Text2 = HScroll1.Value Text3 = HScroll2.Value

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), \ Text5 = Format(Val(Text4) + Val(Text1), \End Sub

Private Sub HScroll2_Change() Text1 = VScroll1.Value Text2 = HScroll1.Value Text3 = HScroll2.Value

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), \ Text5 = Format(Val(Text4) + Val(Text1), \End Sub

Private Sub VScroll1_Change() Text1 = VScroll1.Value Text2 = HScroll1.Value Text3 = HScroll2.Value

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), \ Text5 = Format(Val(Text4) + Val(Text1), \End Sub

G.4

Dim t As Single

Private Sub Command1_Click() t = InputBox(\输入倒计时分钟数\t = t * 60

ProgressBar1.Min = 0 ProgressBar1.Max = t ProgressBar1.Value = t End Sub

Private Sub Command2_Click() Timer1.Interval = 1000

ProgressBar1.Visible = True End Sub

Private Sub Timer1_Timer() Dim m%, s% t = t - 1

ProgressBar1.Value = t m = t \\ 60 s = t Mod 60

Label1 = m & \分\秒\If t = 0 Then

MsgBox \时间到!\ Timer1.Interval = 0

ProgressBar1.Visible = False End If End Sub

G.5

Private Sub Command1_Click() CommonDialog1.ShowColor

Label1.ForeColor = CommonDialog1.Color End Sub

Private Sub Command2_Click() CommonDialog1.ShowOpen

i = Shell(\End Sub

G.6

rivate Sub bold_Click()

Text1.FontBold = Not Text1.FontBold bold.Checked = Not bold.Checked End Sub

Private Sub del_Click() Text1 = \End Sub

Private Sub end_Click() End End Sub

Private Sub font12_Click() Text1.FontSize = 12 End Sub

Private Sub font16_Click() Text1.FontSize = 16 End Sub

Private Sub Form_Load() bold.Checked = False italic.Checked = False End Sub

Private Sub italic_Click()

Text1.FontItalic = Not Text1.FontItalic italic.Checked = Not italic.Checked End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu menu2 End Sub

G.7

Private Sub Command1_Click() CommonDialog1.ShowColor

Label1.ForeColor = CommonDialog1.Color End Sub

Private Sub Command2_Click() CommonDialog1.ShowOpen

i = Shell(\End Sub

Private Sub return_Click(Index As Integer) Form6.Show

Unload Form5 End Sub

G.8 G.9

Private Sub 结束_Click() End End Sub

Private Sub 删除最大值_Click() max = List1.List(0)

For i = 1 To List1.ListCount - 1

If max < List1.List(i) Then max = List1.List(i): j = i Next i

List1.RemoveItem j End Sub

Private Sub 删除最小值_Click() min = List1.List(0)

For i = 1 To List1.ListCount - 1

If min > List1.List(i) Then min = List1.List(i): j = i Next i

List1.RemoveItem j End Sub

Private Sub 随机产生_Click() Randomize For i = 0 To 9

List1.List(i) = Int(Rnd * 30 + 70) Next i End Sub

Private Sub 添加数据_Click() List1.AddItem Int(Rnd * 30 + 70) End Sub

Private Sub 统计_Click()

Dim max%, min%, ave!, m%, n% max = List1.List(0) min = List1.List(0) ave = List1.List(0) m = 0 n = 0

For i = 1 To List1.ListCount - 1

If max < List1.List(i) Then max = List1.List(i): m = i If min > List1.List(i) Then min = List1.List(i): n = i ave = ave + List1.List(i) Next i

Form2.Text1 = List1.List(n) Form2.Text2 = List1.List(m)

Form2.Text3 = Format(ave / List1.ListCount, \Form2.Show

End Sub

G.10

Private Sub experimentG2_Click() Form2.Show End Sub

Private Sub experimentG3_Click() Form3.Show End Sub

Private Sub experimentG4_Click() Form4.Show End Sub

Private Sub experimentG5_Click() Form5.Show End Sub

Private Sub font_12_Click() Text1.FontSize = 12 End Sub

Private Sub font_16_Click() Text1.FontSize = 16 End Sub

Private Sub rnd10_Click() Randomize For i = 0 To 9

List1.List(i) = Int(Rnd * 30 + 70) Next i End Sub

Private Sub stat_Click()

Dim max%, min%, ave!, m%, n% max = List1.List(0) min = List1.List(0) ave = List1.List(0) m = 0 n = 0

For i = 1 To List1.ListCount - 1

If max < List1.List(i) Then max = List1.List(i): m = i If min > List1.List(i) Then min = List1.List(i): n = i ave = ave + List1.List(i) Next i

stat2.Text1 = List1.List(n) stat2.Text2 = List1.List(m)

stat2.Text3 = Format(ave / List1.ListCount, \stat2.Show End Sub

Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu caidan2, vbPopupMenuCenterAlign End Sub

Private Sub xie_Click()

Text1.Font.Italic = Not Text1.Font.Bold End Sub

实验H H.1

Private Sub Command1_Click() Open \Print #1, \王海涛\Print #1, \周文英\Print #1, \陈建东\Open \Write #2, \王海涛\Write #2, \周文英\Write #2, \陈建东\Close

i1 = Shell(\i2 = Shell(\End Sub

Private Sub Command2_Click()

Dim no As String, name As String, s As Integer Open \Do While Not EOF(1) Line Input #1, linedata List1.AddItem linedata Loop

Open \Do While Not EOF(2) Input #2, no, name, s

List2.AddItem no & name & s Loop Close End Sub

H.2

Private Sub Command1_Click() Dim fib%(0 To 9), i%

Open \For i = 0 To 9

If i = 0 Or i = 1 Then fib(i) = i Else

fib(i) = fib(i - 1) + fib(i - 2) End If

Print #1, \Next i Close #1

i = Shell(\End Sub

Private Sub Command2_Click() Dim st$, n%, sum%

Open \Do While Not EOF(1) Input #1, st, n sum = sum + n

List1.AddItem st & \Loop Close #1

List1.AddItem \合计:\List1.AddItem \平均:\End Sub

H.3

H.4

Private Type studtype

no As String * 4 name As String * 6 mark As Single End Type

Dim student As studtype, stud(1 To 5) As studtype, t As studtype

Private Sub Command1_Click()

Open \With student .no = \

.name = \星期一\ .mark = 66 End With

Put #1, 1, student With student .no = \

.name = \星期二\ .mark = 99 End With

Put #1, 2, student With student .no = \

.name = \星期三\ .mark = 88 End With

Put #1, 3, student With student .no = \

.name = \星期四\ .mark = 55 End With

Put #1, 4, student With student .no = \

.name = \星期五\ .mark = 77 End With

Put #1, 5, student Close #1 End Sub

Private Sub Command2_Click()

Open \For i = 1 To 5 Get #1, i, student

Print student.no, student.name, student.mark stud(i) = student Next i Close #1 For i = 1 To 5 For j = i + 1 To 5

If stud(i).mark > stud(j).mark Then t = stud(i): stud(i) = stud(j): stud(j) = t Next Next i

Open \For i = 1 To 5 Put #2, i, stud(i) Next i Close #1 End Sub

Private Sub Command3_Click()

Open \For i = 1 To 5 Get #1, i, stud(i)

Print stud(i).no; stud(i).name; stud(i).mark Next i Close #1

End Sub

H.5

Private Type studtype no As Integer

name As String * 20 sex As String * 1 mark As Single End Type

Dim std As studtype Dim record As Integer

Private Sub Command1_Click() With std

.no = Val(Text1.Text) .name = Text2.Text

.sex = IIf(Option1.Value, \.mark = Val(Text3.Text) End With

Open \record = LOF(1) / Len(std) + 1 Label1.Caption = record Put #1, record, std Close #1 End Sub

Private Sub Command2_Click()

Open \record = Val(Text4.Text) Get #1, record, std Text1.Text = std.no Text2.Text = std.name If std.sex = \Option1.Value = True Else

Option2.Value = True End If

Text3.Text = std.mark

record = LOF(1) / Len(std) Close #1 End Sub

Private Sub Command3_Click() With std

.no = Val(Text1.Text) .name = Text2.Text

.sex = IIf(Option1.Value, \.mark = Val(Text3.Text) End With

Open \record = Val(Text4.Text) Put #1, record, std Close #1 End Sub

Private Sub Command4_Click()

Open \record = 1

Get #1, record, std Text1.Text = std.no Text2.Text = std.name If std.sex = \Option1.Value = True Else

Option2.Value = True End If

Text3.Text = std.mark

record = LOF(1) / Len(std) Close #1

Text4.Text = 1 End Sub

Private Sub Command5_Click()

Open \record = Val(Text4.Text) - 1 If Val(Text4.Text) <= 1 Then

MsgBox \超出记录范围\警告\Else

Get #1, record, std Text1.Text = std.no Text2.Text = std.name If std.sex = \Option1.Value = True Else

Option2.Value = True End If

Text3.Text = std.mark

record = LOF(1) / Len(std) Close #1

Text4.Text = Val(Text4.Text) - 1 End If End Sub

Private Sub Command6_Click()

Open \record = Val(Text4.Text) + 1

If record > Val(Label1.Caption) Then

MsgBox \超出记录范围\警告\Else

Get #1, record, std Text1.Text = std.no Text2.Text = std.name

If std.sex = \Option1.Value = True Else

Option2.Value = True End If

Text3.Text = std.mark

record = LOF(1) / Len(std) Close #1

Text4.Text = Val(Text4.Text) + 1 End If End Sub

Private Sub Command7_Click()

Open \record = Val(Label1.Caption) Get #1, record, std Text1.Text = std.no Text2.Text = std.name If std.sex = \Option1.Value = True Else

Option2.Value = True End If

Text3.Text = std.mark

record = LOF(1) / Len(std) Close #1

Text4.Text = record End Sub

Private Sub Form_Load()

Open \Label1.Caption = LOF(1) / Len(std) Close #1 End Sub

H.6

Private Type studtype no As Integer

name As String * 6 mark As Integer End Type

Dim std As studtype Dim record As Integer

Private Sub Command1_Click() With std

.no = Val(Text1.Text) .name = Text2.Text .mark = Val(Text3.Text) End With

Open \record = LOF(1) / Len(std) + 1 Put #1, record, std Close #1

Text1.Text = \Text2.Text = \Text3.Text = \Text1.SetFocus End Sub

Private Sub Command2_Click() Dim sum, count As Integer

Open \For i = 1 To record Get #1, i, std

sum = sum + std.mark count = count + 1

List1.AddItem std.no & \ \Next i

List1.AddItem \总分\

List1.AddItem \平均成绩\Close #1 End Sub

H.7

Private Sub Command1_Click()

Dim inputdata1, inputdata2, inputdata3 As String * 1

Dim v, byt1() As Byte, byt2() As Byte, flen1 As Long, flen2 As Long, fnum, fnum3 As Integer Text1.Text = \

CommonDialog1.ShowOpen

fname1 = CommonDialog1.FileName CommonDialog2.ShowOpen

fname2 = CommonDialog2.FileName Open fname1 For Binary As #1 Open fname2 For Binary As #2

Do While Not EOF(1) inputdata1 = Input(1, #1)

Text1.Text = Text1.Text + inputdata1 Loop

Do While Not EOF(2) inputdata2 = Input(1, #2)

Text2.Text = Text2.Text + inputdata2 Loop

fnum3 = FreeFile

Open \Put #fnum3, 1, byt1()

Put #fnum3, flen1 + 1, byt2() Do While Not EOF(fnum3) inputdata3 = Input(1, #fnum3)

Text3.Text = Text3.Text + inputdata3 Loop

If Dir(fname1) <> \ fnum = FreeFile

Open fname1 For Binary As #fnum flen1 = LOF(fnum) ReDim byt1(flen1 - 1) Get #fnum, 1, byt1() Close fnum End If

If Dir(fname2) <> \ fnum = FreeFile

Open fname2 For Binary As #fnum flen2 = LOF(fnum) ReDim byt2(flen2 - 1) Get #fnum, 1, byt2() Close fnum End If Close End Sub J-4

Private Sub Command1_Click() Dim strb() As Byte

CommonDialog1.ShowOpen

Open CommonDialog1.FileName For Binary As #1

f1 = LOF(1) ReDim strb(f1) Get #1, , strb

Adodc1.Recordset.Fields(\照片\Close #1

Image1.Picture = LoadPicture(CommonDialog1.FileName) End Sub

VB程序设计教程(第3版)课后实验答案

实验AA.1PrivateSubCommand1_Click()Label3=Text1EndSubA.2PrivateSubForm_Load()Timer1.Interval=0EndSubPrivateSubCommand1_Click()'自动
推荐度:
点击下载文档文档为doc格式
0wjfb4h43r92i2p9mddy
领取福利

微信扫码领取福利

微信扫码分享