VB程序设计教程课后实验答案

阅读: 评论:0

2024年2月4日发(作者:)

VB程序设计教程课后实验答案

VB程序设计教程课后实验答案

实验A

A.1

Private Sub Command1_Click()

Label3 = Text1

End Sub

A.2

Private Sub Form_Load()

al = 0

End Sub

Private Sub Command1_Click() '自动

al = 200

End Sub

Private Sub Command2_Click() '手动

al = 0

Call MyMove

End Sub

Private Sub Timer1_Timer()

Call MyMove

End Sub

Sub MyMove()

- 50

If < 0 Then =

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( + "n_")

Print "欢迎使用VB"

End Sub

Private Sub Form_DblClick()

Cls

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

Picture = LoadPicture("") '

End Sub

Private Sub Form_Load()

Caption = "装入窗体"

Picture = LoadPicture( + "n_")

Print "装入图"

End Sub

Private Sub Form_Resize() ' 该事件的作用窗体始终与图一样大

'Caption = "窗体大小不变"

' = 260 * 16 ' 260是图的宽度,象素单位

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

End Sub

A.5

Sub Command1_Click()

me = "隶书"

ze = 25

End Sub

Sub Command2_Click()

= t

me = me

ze = ze

End Sub

实验B

B.1

Private Sub Command1_Click()

Text2 = Format(5 / 9 * (Val(Text1) - 32), "0.00")

End Sub

Private Sub Command2_Click()

Text1 = Format(9 / 5 * Val(Text2) + 32, "0.00")

End Sub

Private Sub Command1_Click()

Dim f!, c! ' 声明两个变量

f = Val(Text1)

c = 5 / 9 * (f - 32)

Text2 = Format(c, "0.00") ' 保留两位小数

End Sub

Private Sub Command2_Click()

Dim ff!, cc! ' 声明两个变量

cc = Val(Text2)

ff = 9 / 5 * cc + 32

Text1 = Format(ff, "0.00") ' 保留两位小数

End Sub

B.2

Private Sub Command1_Click()

Label2 = Format(Val(Text1) * Val(Text1) * 3.14, "0.00")

End Sub

Private Sub Command2_Click()

Label3 = Format(Val(Text1) * 3.14 * 2, "0.00")

End Sub

Private Sub Text1_LostFocus()

If Not IsNumeric() Then

MsgBox "输入有非数字字符,请重新输入", , "警告"

= ""

us

End If

End Sub

Private Sub Command1_Click()

Label2 = Format(Val(Text1) * Val(Text1) * 3.14, "0.00")

End Sub

Private Sub Command2_Click()

Label3 = Format(Val(Text1) * 3.14 * 2, "0.00")

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

If Not IsNumeric() Then

= ""

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 =

dx = UCase(x)

cd = Len(x)

Print "大写字母为:"; dx

Print "字符串长度为:"; cd

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, "★"); Spc(18 - 4 * (i - 1)); 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, "★"); String(10 - (2 * i - 1), "☆"); String(2 * i - 1, "★")

Next i

End Sub

Private Sub Command2_Click()

Cls

End Sub

B.8

Private Sub Form_Click()

x = Val(InputBox("输入一正实数", "计算", 0))

pf = Format(x * x, "0.000")

pfg = Format(Sqr(x), "0.000")

lf = Format(x * x * x, "0.000")

lfg = Format(x ^ (1 / 3), "0.000")

Print "平方为:"; pf; Space(5); "平方根为:"; pfg; Space(5); "立方为:"; lf; Space(5); "立方根为:"; 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("input x")

y = InputBox("input y")

z = InputBox("input z")

Print " x y z"

Print " 排序前"; x; " "; y; " "; z

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 " 排序后" & x & " " & y & "

End Sub

Private Sub Command2_Click()

Dim x!, y!, z!

x = InputBox("input x")

y = InputBox("input y")

z = InputBox("input z")

Print " x y z"

Print " 排序前"; x; " "; y; " "; z

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 " 排序后" & x & " " & y & "

End Sub

C.4

Dim a(3) As Integer

Private Sub Command1_Click()

For i = 0 To 2

a(i) = Int(Rnd * 100 + 200)

a(i)

Next i

End Sub

Private Sub Command2_Click()

" & z

" & z

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

a(0)

a(1)

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 = ""

us

Else

y = n / 2 - m

If y < 0 Then

MsgBox ("脚数必须≥2倍的头数,请重新输入")

Text2 = ""

us

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, "0.00")

Text5 = Format(-b / t, "0.00")

ElseIf de > 0 Then

Text4 = Format((-b + Sqr(de)) / t, "0.00")

Text5 = Format((-b - Sqr(de)) / t, "0.00")

Else

Text4 = Format(-b / t, "0.00") & "+" & Format(Sqr(Abs(de)) / t, "0.00") & "i"

Text5 = Format(-b / t, "0.00") & "-" & Format(Sqr(Abs(de)) / t, "0.00") & "i"

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 = ""

us

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 = "Monday"

Case 2

Text2 = "Tuesday"

Case 3

Text2 = "Wednesday"

Case 4

Text2 = "Thursday"

Case 5

Text2 = "Friday"

Case 6

Text2 = "Saturday"

Case 7

Text2 = "Sunday"

Case Is > 7, Is < 1

MsgBox "数字为1~7,重新输入"

Text1 = ""

us

End Select

End Sub

或者

Private Sub Text1_LostFocus()

If Text1 > 7 Or Text1 < 1 Then

MsgBox "数字为1~7,重新输入"

Text1 = ""

us

Else

Text2 = Choose(Text1, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

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 "For结构"; s, i; "项"

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=" & n & "时,pi="; 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 "a="; a, "n="; n

For i = 1 To n

t = t * 10 + a

s = s + t

Print t;

Next i

Print

Print "s="; s

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()

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()

"x课安排在 "; "y课安排在 "; "z课安排在 "

""

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), "周一", "周二", "周三", "周四", "周五", "周六", "周日")

" "; X1; " "; Y1; " "; z1

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 "Max="; Max; " Min="; Min; " Avg="; 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, "◆"); a(i)

Print

Next i

End Sub

E.3

Dim a%(19)

Private Sub Command1_Click()

For i = 0 To 19

a(i) = Int(Rnd * 101)

a(i); " ";

If (i + 1) Mod 4 = 0 Then

Next i

End Sub

Private Sub Command2_Click()

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 "s("; i; ")的人数有 "; Format(s(i), "0"); "个"

Next i

End Sub

E.4

Private Sub Command1_Click()

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: d(i);

If i Mod 5 = 0 Then

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()

For i = 0 To 3

For j = 0 To i

a(i, j); " ";

Next j

Next i

End Sub

Private Sub Command2_Click()

For i = 0 To 3

For j = i To 3

Tab(j * 6); b(i, j);

Next j

Next i

End Sub

Private Sub Command3_Click()

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

"A数组主对角线元素和为:"; sa

"B数组副对角线元素和为:"; sb

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)

a6(i, j); "";

Next j

Next

End Sub

E.7

Private Sub Form_Load()

m "大学计算机基础"

m "C/C++程序设计"

m "VB程序设计"

m "Web程序设计"

m "多媒体技术与应用"

m "数据库技术与应用"

m "网络技术与应用"

m "硬件技术基础"

m "软件技术技术基础"

End Sub

Private Sub List1_Click()

If unt >= 5 Then

MsgBox ("超过5门课程,不能再选")

Exit Sub

Else

m

Item dex

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

m

= ""

End If

End Sub

Private Sub Command1_Click()

Dim min%, max%

min = Val((0))

max = Val((0))

imin = 0

imax = 0

For i = 1 To unt - 1

If Val((i)) > max Then

imax = i

max = (i)

ElseIf Val((i)) < min Then

imin = i

min = (i)

End If

Next i

t = (0)

(0) = (imin)

(imin) = t

t = (unt - 1)

(unt - 1) = (imax)

(imax) = t

End Sub

E.9

Private Sub Form_Click()

For i = 0 To unt - 1

If Asc(Left((i), 1)) < 0 Then

(i)

me = (i)

"商丘师范学院"

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

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

"工号 姓名 工资"

For i = 0 To n - 1

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 "min="; amin

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)

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的结果"; Sin(x)

End Sub

Private Sub Command2_Click()

Print "调用自定义函数 MySin的结果"; MySin(x)

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 "输入非数字串,重新输入"

= ""

us

Else

If IsH(Text1) Then

Text1; " ★ "

Else

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

m i & " =" & j & " +" & i - j

n = n + 1

End If

End If

Next j

Next i

" 6和100之间共有"; n; "对素数和"

End Sub

F.6

Dim a%()

Private Sub Form_Click()

Print "1000以内的完数为:"

For i = 1 To 1000

If IsWs(i) Then

Print i; "=1";

For j = 1 To UBound(a)

Print "+"; a(j);

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 & " ", maxw

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()

m Combo1

If Option1 Then m "Pentium II"

If Option2 Then m "Pentium I"

If Option3 Then m "Celeron"

m Text1

If Check1 Then m "声卡"

If Check2 Then m "Modem"

If Check3 Then m "网络适配器"

End Sub

Private Sub Text1_LostFocus()

st = UCase(Trim(Text1))

le = Len(st)

If Not IsNumeric(Left(st, le - 2)) Or Right(st, 2) <> "MB" Then

MsgBox "有不合法字符!"

Text1 = ""

us

End If

End Sub

G.2

Private Sub Check1_Click()

= Not

End Sub

Private Sub Check2_Click()

= Not

End Sub

Private Sub Command1_Click()

If Option1 Then

Sin(Val(Text1))

ElseIf Option2 Then

Exp(Val(Text1))

ElseIf Option3 Then

Sqr(Val(Text1))

End If

End Sub

Private Sub return_Click(Index As Integer)

Unload Form2

End Sub

G.3

Private Sub HScroll1_Change()

Text1 =

Text2 =

Text3 =

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00")

Text5 = Format(Val(Text4) + Val(Text1), "0.00")

End Sub

Private Sub HScroll2_Change()

Text1 =

Text2 =

Text3 =

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00")

Text5 = Format(Val(Text4) + Val(Text1), "0.00")

End Sub

Private Sub VScroll1_Change()

Text1 =

Text2 =

Text3 =

Text4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00")

Text5 = Format(Val(Text4) + Val(Text1), "0.00")

End Sub

G.4

Dim t As Single

Private Sub Command1_Click()

t = InputBox("输入倒计时分钟数")

t = t * 60

= 0

= t

= t

End Sub

Private Sub Command2_Click()

al = 1000

e = True

End Sub

Private Sub Timer1_Timer()

Dim m%, s%

t = t - 1

= t

m = t 60

s = t Mod 60

Label1 = m & "分" & s & "秒"

If t = 0 Then

MsgBox "时间到!"

al = 0

e = False

End If

End Sub

G.5

Private Sub Command1_Click()

lor

lor =

End Sub

Private Sub Command2_Click()

en

i = Shell("C: " + me, vbNormalFocus)

End Sub

G.6

rivate Sub bold_Click()

ld = Not ld

d = Not d

End Sub

Private Sub del_Click()

Text1 = ""

End Sub

Private Sub end_Click()

End

End Sub

Private Sub font12_Click()

ze = 12

End Sub

Private Sub font16_Click()

ze = 16

End Sub

Private Sub Form_Load()

d = False

d = False

End Sub

Private Sub italic_Click()

alic = Not alic

d = Not d

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()

lor

lor =

End Sub

Private Sub Command2_Click()

en

i = Shell("C: " + me, vbNormalFocus)

End Sub

Private Sub return_Click(Index As Integer)

Unload Form5

End Sub

G.8

G.9

Private Sub 结束_Click()

End

End Sub

Private Sub 删除最大值_Click()

max = (0)

For i = 1 To unt - 1

If max < (i) Then max = (i): j = i

Next i

Item j

End Sub

Private Sub 删除最小值_Click()

min = (0)

For i = 1 To unt - 1

If min > (i) Then min = (i): j = i

Next i

Item j

End Sub

Private Sub 随机产生_Click()

Randomize

For i = 0 To 9

(i) = Int(Rnd * 30 + 70)

Next i

End Sub

Private Sub 添加数据_Click()

m Int(Rnd * 30 + 70)

End Sub

Private Sub 统计_Click()

Dim max%, min%, ave!, m%, n%

max = (0)

min = (0)

ave = (0)

m = 0

n = 0

For i = 1 To unt - 1

If max < (i) Then max = (i): m = i

If min > (i) Then min = (i): n = i

ave = ave + (i)

Next i

1 = (n)

2 = (m)

3 = Format(ave / unt, "0.00")

End Sub

G.10

Private Sub experimentG2_Click()

End Sub

Private Sub experimentG3_Click()

End Sub

Private Sub experimentG4_Click()

End Sub

Private Sub experimentG5_Click()

End Sub

Private Sub font_12_Click()

ze = 12

End Sub

Private Sub font_16_Click()

ze = 16

End Sub

Private Sub rnd10_Click()

Randomize

For i = 0 To 9

(i) = Int(Rnd * 30 + 70)

Next i

End Sub

Private Sub stat_Click()

Dim max%, min%, ave!, m%, n%

max = (0)

min = (0)

ave = (0)

m = 0

n = 0

For i = 1 To unt - 1

If max < (i) Then max = (i): m = i

If min > (i) Then min = (i): n = i

ave = ave + (i)

Next i

1 = (n)

2 = (m)

3 = Format(ave / unt, "0.00")

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()

= Not

End Sub

实验H

H.1

Private Sub Command1_Click()

Open "c:score" For Output As #1

Print #1, "051023", "王海涛", 66

Print #1, "052498", "周文英", 88

Print #1, "050992", "陈建东", 77

Open "c:score1" For Output As #2

Write #2, "051023", "王海涛", 66

Write #2, "052498", "周文英", 88

Write #2, "050992", "陈建东", 77

Close

i1 = Shell("" + " c:score", vbNormalNoFocus)

i2 = Shell("" + " c:score1", vbNormalNoFocus)

End Sub

Private Sub Command2_Click()

Dim no As String, name As String, s As Integer

Open "c:score" For Input As #1

Do While Not EOF(1)

Line Input #1, linedata

m linedata

Loop

Open "c:score1" For Input As #2

Do While Not EOF(2)

Input #2, no, name, s

m no & name & s

Loop

Close

End Sub

H.2

Private Sub Command1_Click()

Dim fib%(0 To 9), i%

Open "c:" For Output As #1

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, """Fib(" & i & ")""," & fib(i)

Next i

Close #1

i = Shell("" + " c:", vbNormalNoFocus)

End Sub

Private Sub Command2_Click()

Dim st$, n%, sum%

Open "c:" For Input As #1

Do While Not EOF(1)

Input #1, st, n

sum = sum + n

m st & "=" & n

Loop

Close #1

m "合计:" & sum

m "平均:" & sum / 10

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 "d:" For Random As #1 Len = Len(student)

With student

.no = "0001"

.name = "星期一"

.mark = 66

End With

Put #1, 1, student

With student

.no = "0002"

.name = "星期二"

.mark = 99

End With

Put #1, 2, student

With student

.no = "0003"

.name = "星期三"

.mark = 88

End With

Put #1, 3, student

With student

.no = "0004"

.name = "星期四"

.mark = 55

End With

Put #1, 4, student

With student

.no = "0005"

.name = "星期五"

.mark = 77

End With

Put #1, 5, student

Close #1

End Sub

Private Sub Command2_Click()

Open "d:" For Random As #1 Len = Len(student)

For i = 1 To 5

Get #1, i, student

Print , ,

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 "d:" For Random As #2 Len = Len(student)

For i = 1 To 5

Put #2, i, stud(i)

Next i

Close #1

End Sub

Private Sub Command3_Click()

Open "d:" For Random As #1 Len = Len(student)

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()

.name =

.sex = IIf(, "1", "0")

.mark = Val()

End With

Open "c:score" For Random As #1 Len = Len(std)

record = LOF(1) / Len(std) + 1

n = record

Put #1, record, std

Close #1

End Sub

Private Sub Command2_Click()

Open "c:score" For Random As #1 Len = Len(std)

record = Val()

Get #1, record, std

=

=

If = "1" Then

= True

Else

= True

End If

=

record = LOF(1) / Len(std)

Close #1

End Sub

Private Sub Command3_Click()

With std

.no = Val()

.name =

.sex = IIf(, "1", "0")

.mark = Val()

End With

Open "c:score" For Random As #1 Len = Len(std)

record = Val()

Put #1, record, std

Close #1

End Sub

Private Sub Command4_Click()

Open "c:score" For Random As #1 Len = Len(std)

record = 1

Get #1, record, std

=

=

If = "1" Then

= True

Else

= True

End If

=

record = LOF(1) / Len(std)

Close #1

= 1

End Sub

Private Sub Command5_Click()

Open "c:score" For Random As #1 Len = Len(std)

record = Val() - 1

If Val() <= 1 Then

MsgBox "超出记录范围", 2 + vbExclamation, "警告": Close #1

Else

Get #1, record, std

=

=

If = "1" Then

= True

Else

= True

End If

=

record = LOF(1) / Len(std)

Close #1

= Val() - 1

End If

End Sub

Private Sub Command6_Click()

Open "c:score" For Random As #1 Len = Len(std)

record = Val() + 1

If record > Val(n) Then

MsgBox "超出记录范围", 2 + vbExclamation, "警告": Close

Else

Get #1, record, std

=

=

If = "1" Then

= True

Else

= True

End If

=

record = LOF(1) / Len(std)

Close #1

= Val() + 1

End If

End Sub

Private Sub Command7_Click()

Open "c:score" For Random As #1 Len = Len(std)

record = Val(n)

Get #1, record, std

=

=

If = "1" Then

= True

Else

= True

End If

=

record = LOF(1) / Len(std)

Close #1

= record

End Sub

Private Sub Form_Load()

Open "c:score" For Random As #1 Len = Len(std)

n = 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()

.name =

.mark = Val()

End With

Open "c:score1" For Random As #1 Len = Len(std)

record = LOF(1) / Len(std) + 1

Put #1, record, std

Close #1

= ""

= ""

= ""

us

End Sub

Private Sub Command2_Click()

Dim sum, count As Integer

Open "c:score1" For Random As #1 Len = Len(std)

For i = 1 To record

Get #1, i, std

sum = sum +

count = count + 1

m & " " & &

Next i

m "总分" & sum

m "平均成绩" & sum / count

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

= "": = "": = ""

en

fname1 = me

en

fname2 = me

Open fname1 For Binary As #1

Open fname2 For Binary As #2

Do While Not EOF(1)

inputdata1 = Input(1, #1)

= + inputdata1

Loop

Do While Not EOF(2)

inputdata2 = Input(1, #2)

= + inputdata2

Loop

fnum3 = FreeFile

Open "c:" For Binary As #fnum3

Put #fnum3, 1, byt1()

Put #fnum3, flen1 + 1, byt2()

Do While Not EOF(fnum3)

inputdata3 = Input(1, #fnum3)

= + inputdata3

Loop

If Dir(fname1) <> "" Then

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) <> "" Then

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

en

Open me For Binary As #1

f1 = LOF(1)

ReDim strb(f1)

Get #1, , strb

("照片").AppendChunk strb

Close #1

e = LoadPicture(me)

End Sub

VB程序设计教程课后实验答案

本文发布于:2024-02-04 10:13:01,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170701278153257.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:输入   窗体   技术
留言与评论(共有 0 条评论)
   
验证码:
排行榜

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23