VB申石磊课后编程答案

习题2-1
Private Sub Form_Click()
 FontSize = 40
 FontName = "隶书"
 ForeColor = vbRed
 Print "热烈庆祝"
 FontSize = 60
 FontName = "宋体"
 ForeColor = vbGreen
 Print "XX会召开"
End Sub
Private Sub Form_DblClick()
 Cls
End Sub
习题2-2
Private Sub Command1_Click()
 Form1.Move Width * Rnd, Height * Rnd

Form1.BackColor=QBColor (15*Rnd)
End Sub
习题2-3
‘先分别设置三个命令按钮的Style的属性为1-Graphical

Private Sub Command1_Click()

Command1.Caption = "设置"

Command1.BackColor = vbRed

Label1.Caption = "标签和按钮属性的练习"

Label1.Font = "宋体"

Label1.FontSize = 20

Label1.BorderStyle = 1

Label1.ForeColor = &HFF0057

Label1.BackColor = QBColor(15 * Rnd)

End Sub

Private Sub Command2_Click()

Command2.Caption = "消失"

Command2.BackColor = vbBlue

Label1.Visible = False

End Sub

Private Sub Command3_Click()

Command3.Caption = "显示"

Command3.BackColor = vbGreen

Label1.Visible = True

End Sub

习题2-4
Private Sub Timer1_Timer()
 Text1.Text = Now
 Caption = Text1.Text
End Sub
习题2-5
Private Sub Timer1_Timer()

If BackColor = vbRed Then

BackColor = vbBlue

Else

BackColor = vbRed

End If

End Sub

习题2-6
Private Sub Timer1_Timer()
 If Label1.Left < Form1.Width Then
 Label1.Left = Label1.Left +10
 Else
 Label1.Left = -Label1.Left
 End If
End Sub
习题2-7
Private Sub Command1_Click()

Text1.Text = "文本框的显示"

End Sub

Private Sub Command2_Click()

Text1.Text = ""

End Sub

Private Sub Command3_Click()

End

End Sub

习题2-8
Private Sub Text1_Change()

Text2.Text = Text1.Text

End Sub

Private Sub Text2_Change()

Text1.Text = Text2.Text

End Sub
习题2-9
Private Sub Command1_Click()

Call Text1_LostFocus

End Sub

Private Sub Text1_Click()

Call Text1_GotFocus

End Sub

Private Sub Text1_GotFocus()

Text1.Text = "获得焦点"

End Sub

Private Sub Text1_LostFocus()

Text1.Text = "失去焦点"

End Sub
习题2-10
Private Sub Timer1_Timer()

If Text1.Top < Form1.Height Then

Text1.Top = Text1.Top + 10

Else

Text1.Top = 0

End If

End Sub

习题3-3
Private Sub Form_Click()

Dim a As Single

Dim b As Single

Dim c As Single

Dim s As Single

Dim area As Single

a = InputBox("输入a值")

b = InputBox("输入b值")

c = InputBox("输入c值")

s = (a + b + c) / 2

area = Sqr(s * (s - a) * (s - b) * (s - c))

Print "area="; area

End Sub
习题3-4
Private Sub Form_Click()

Dim a As Integer, s As Single

a = Int(100 * Rnd + 1)

s = Sqr(a)

Print a, s

End Sub
习题3-5
Private Sub Timer1_Timer()

 Cls

 Randomize

 Print "资源环境学院"

 FontSize = Int(76 * Rnd + 5)

End Sub

习题3-6
 Private Sub Form_KeyPress(KeyAscii AsInteger)

Dim s As String

Form1.Cls

s = InputBox("请输入5个字符","字符子串引用")

If Len(s) = 5 Then

Print s

Print Right(s, 1) & Mid(s, 3, 1) &Left(s, 1)

End If

End Sub

习题3-7
Private Sub Command1_Click()

If Val(Text1.Text) >= 10 AndVal(Text1.Text) < 100 Then

Text2.Text = Right(Val(Text1.Text), 1)& Left(Val(Text1.Text), 1)

End If

End Sub
习题3-8
Private Sub Form_Click()

 Dimr As Long, a As Long, b As Long

 Dimc As Long, d As Long, e As Long, f As Long

 r =InputBox("请输入您的工资:")

 a =r \ 100

 b =(r - 100 * a) \ 50

 c =(r - 100 * a - b * 50) \ 10

 d =(r - 100 * a - b * 50 - c * 10) \ 5

 e =(r - 100 * a - b * 50 - c * 10 - d * 5) \ 2

 f =r - 100 * a - b * 50 - c * 10 - d * 5 - e * 2

 Print "您的工资是:" & r

 Print "100元张数:" & a

 Print "50元张数:" & b

 Print "10元张数:" & c

 Print "5元张数:" & d

 Print "2元张数:" & e

 Print "1元张数:" & f

End Sub
习题3-9

Private Sub Command1_Click()

Dim TaskID As Integer

TaskID = Shell("C:\Windows\notepad.exe",vbNormalFocus)

End Sub

Private Sub Command2_Click()

Shell"C:\Windows\System32\mspaint.exe", vbNormalFocus

End Sub
习题3-10
Private Sub Timer1_Timer()

Label1.Caption = "我国2018年8月8日举办冬奥会"

Dim s As String

s = Choose(Weekday(Date), "星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六")

Label2.Caption = "今天:"& Date & Space(3) & s

Label3.Caption = "距开幕还有:"& #8/8/2018# - Date & "天"

End Sub

 

习题4-1
Private Sub Form_Click()

Dim s As Single, p As Single, r As Single,n As Integer

p = InputBox("输入存款金额")

n = InputBox("输入存款年数")

r = InputBox("输入利率")

s = p * (1 + r) ^ n

MsgBox n & "年后本金和利息之和为:"& Format(s, "##.##")

End Sub
习题4-2
Private Sub Form_click()
 Dim a As Single, b As Single
 a = InputBox("请输入第一个实数")
 b = InputBox("请输入第二个实数")
 If a > b Then
 Print a, b
 Else
 Print b, a
 End If
End Sub
习题4-3
Private Sub Form_click()
 Dim a As Integer

 Cls

 a =InputBox("请输入年份")

 If aMod 4 = 0 And a Mod 100 <> 0 Or a Mod 400 = 0 Then

 Print Str(a) & "年是闰年!"

 Else

 Print Str(a) & "年不是闰年!"

 EndIf

End Sub
习题4-4
Private Sub Form_click()
 Dim a!, b!, c!, d!

Dim m!, n!, r!, i!

a = InputBox("a=")

 b =InputBox("b=")

 c =InputBox("c=")

 d =b * b - 4 * a * c

 If d>= 0 Then

 m =(-b + Sqr(d)) / (2 * a)

 n =(-b - Sqr(d)) / (2 * a)

 Print " X1 = "; m, ",","X2="; n

 Else

 r =(-b) / (2 * a)

 i =Sqr(Abs(-d)) / (2 * a)

 Print "X1 = "; r & "+"& Abs(i) & "i", ",", "X2 = "; r &"-" & Abs(i) & "i"

 EndIf

End Sub
习题4-5
Private Sub Form_Click()
Dim x!

 x =InputBox("输入分数:")

 If x< 60 Then

 Print "不及格!"

 ElseIf x < 70 Then

 Print "及格!"

 ElseIf x < 80 Then

 Print "一般!"

 ElseIf x < 90 Then

 Print "良好!"

 Else

 Print "优秀!"

 EndIf

End Sub
习题4-6
Private Sub Form_Click()

 Dimx!, y!

 x =InputBox("输入x的值:")

 Select Case x

 CaseIs < 0

 y =-x

 Case0 To 10

 y =x

 Case10 To 20

 y =x ^ 2 + 1

 CaseElse

 y =x ^ 2 - 1

 EndSelect

 Print "y="; y

End Sub
习题4-7
Private Sub Form_Click()

 Dims As Integer

 s =InputBox("请输入年龄")

 Select Case s

 CaseIs > 50

Print "您的年龄为" &Str(s) & "," & "请参加慢跑运动"

 Case40 To 50

Print "您的年龄为" &Str(s) & "," & "请参加竞走运动"

 Case30 To 40

Print "您的年龄为" &Str(s) & "," & "请参加跳绳运动"

 CaseElse

Print "您的年龄为" &Str(s) & "," & "请参加长跑运动"

 EndSelect

 EndSub

 

习题4-8

Private Sub Form_Click()

Dim i%, j%

For i = 1 To 100

If i Mod 2 = 0 Then

Print i;

j = j + 1

If j Mod 10 = 0 Then Print

End If

Next i

End Sub

习题4-9

Private Sub Form_Click()

Dim i%,j%

For i = 1 To 1000

If i Mod 3 = 2 Or i Mod 5 = 3 Or i Mod 7 =2 Then

j = j + 1

Print i;

If j Mod 10 = 0 Then Print

End If

Next i

End Sub

习题4-11

Private Sub Form_Click()

Dim i%, a&

While i < 101

a = a + i * (-1) ^ (i + 1)

i = i + 1

Wend

Print a

End Sub

习题4-12

Private Sub Form_Click()

Dim m&, n&, a&, b&, i%, j%

a = 1

m = InputBox("输入m")

n = InputBox("输入n")

For i = m To n

For j = 1 To i

a = a * j

Next j

b = b + a

Next i

Print b

End Sub

习题4-13

Private Sub Form_Click()

Dim a%, b%, c%, i%, j%, k%

For a = 1 To 20

For b = 1 To 20

For c = 1 To 20

If a * a + b * b = c * c Then

Print a, b, c

End If

Next c

Next b

Next a

End Sub

习题4-14

Option Explicit

Private Sub Form_Click()

Dim a%, b%, i%

For a = 1 To 500

b = a

For i = 1 To a - 1

If a Mod i = 0 Then b = b - i

Next i

If b = 0 Then Print a

Next a

End Sub

习题4-15

Private Sub Form_Click()

n = 1: s = 1: t = 1

While t > 0.00001

i = i + 1

n = n * i

s = s + 1 / n

t = 1 / n

Wend

Print s End Sub

习题5-1

Option Base 1

Private Sub Form_Click()

Dim A(10) As String * 1

For i = 1 To 10

A(i) = Chr(64 + i)

Next i

For i = 1 To 10

Print A(i);

Next i

Print

For i = 1 To 5

t = A(i)

A(i) = A(11 - i)

A(11 - i) = t

Next i

For i = 1 To 10

Print A(i);

Next i

End Sub

习题5-2

Option Base 1

Private Sub Form_Click()

Dim A

 A =Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For i = 1 To 10

If A(i) Mod 3 = 0 Then

Print A(i), i

End If

Next i

End Sub

习题5-3

Option Base 1

Private Sub Form_Click()

Dim A

 A =Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

For i = 1 To 5

t = A(i)

A(i) = A(11 - i)

A(11 - i) = t

Next i

For i = 1 To 10

Print A(i);

Next i

End Sub

习题5-4

Option Base 1

Private Sub Form_Click()

Dim a(5, 5)

 

For i = 1 To 5

For j = 1 To 5

a(i, j) = Int(51 * Rnd + 10)

Next j

Next i

For i = 1 To 5

For j = 1 To 5

Print a(i, j);

t = t + a(i, j)

Next j

Print Spc(6), t

t = 0

Next i

Print

Print

For i = 1 To 5

For j = 1 To 5

If i = j Then Print a(i, j);

Next j

Next i

End Sub

习题5-5

Option Base 1

Private Sub Form_Click()

Dim a(), b&, c&

m = InputBox("第一维上限")

n = InputBox("第二维上限")

ReDim a(m, n)

For i = 1 To m

For j = 1 To n

a(i, j) = Int(900 * Rnd + 100)

Next j

Next i

For i = 1 To m

For j = 1 To n

Print a(i, j);

Next j

Print

Next i

Print

b = a(1, 1)

For i = 1 To m

For j = 1 To n

If a(i, j) > b Then b = a(i, j)

Next j

Next i

For i = 1 To m

For j = 1 To n

If a(i, j) = b Then Print "最大值:"& b & "行号:" & i & "列号:" & j

Next j

Next i

c = a(1, 1)

For i = 1 To m

For j = 1 To n

If a(i, j) < c Then c = a(i, j)

Next j

Next i

For i = 1 To m

For j = 1 To n

If a(i, j) = c Then Print "最小值:"& c & "行号:" & i & "列号:" & j

Next j

Next i

End Sub

习题5-7

Type stutype

id As String * 6

name As String * 6

score As Single

End Type

Option Base 1

Private Sub Form_Click()

Dim a(3) As stutype

Dim i%

For i = 1 To 3

a(i).id = InputBox("学号")

a(i).name = InputBox("姓名")

a(i).score = InputBox("分数")

Next i

For i = 1 To 3

Print a(i).id; a(i).id; a(i).score

Next i

End Sub

习题5-8

Private Sub Option1_Click(Index As Integer)

Text1.ForeColor = vbRed

Select Case Index

Case 0

Text1.FontSize = 10

Case Else

Text1.FontSize = 30

End Select

End Sub

Private Sub Option2_Click(Index As Integer)

Text1.ForeColor = vbBlue

Select Case Index

Case 0

Text1.Font = "黑体"

Case Else

Text1.Font = "隶书"

End Select

End Sub

习题5-9

Option Base 1

Private Sub Form_Click()

Dim a(3, 4)

Dim i%, j%

For i = 1 To 3

For j = 1 To 4

a(i, j) = InputBox("输入数组元素")

Next j

Next i

For i = 1 To 3

For j = 1 To 4

Print a(i, j),

Next j

Print

Next i

Print

Print

For j = 1 To 4

t = a(1, j)

a(1, j) = a(3, j)

a(3, j) = t

Next j

For j = 1 To 3

t = a(j, 2)

a(j, 2) = a(j, 4)

a(j, 4) = t

Next j

For i = 1 To 3

For j = 1 To 4

Print a(i, j),

Next j

Print

Next i

End Sub

习题5-10

Option Base 1

Private Sub Form_Click()

Dim i As Integer, j As Integer

Dim arr(10, 10) As Integer

For i = 1 To 10

For j = 1 To i

If j = 1 Then

arr(i, j) = 1

Else

arr(i, j) = arr(i - 1, j - 1) + arr(i - 1,j)

End If

Next j

Next i

For i = 1 To 10

For j = 1 To i

Print arr(i, j);

Next j

Print

Next i

End Sub

习题6-1

Sub area(r, h)

r = InputBox("r")

h = InputBox("h")

g = 3.14 * r * r * 2 + (2 * 3.14 * r * h)

Print g

End Sub

Private Sub Form_Click()

Call area(r, h)

End Sub

习题6-3

Sub zh(r)

s = Choose(r + 1, "星期日","星期一", "星期二", "星期三", "星期四", "星期五", "星期六")

Print s

End Sub

Private Sub Form_Click()

r = InputBox("r")

If r < 0 Or r > 6 Then

MsgBox "错误,请更新输入"

End

End If

Call zh(r)

End Sub

习题6-4

和6-3一样,只是sub过程在标准模块中定义

习题6-5

Function pd(t)

If t Mod 2 = 0 Then

pd = "偶数"

Else

pd = "奇数"

 EndIf

End Function

Private Sub Form_Click()

m = InputBox("m")

Call pd(m)

Print pd(m)

End Sub

习题6-6

详见课本例题6-5

习题6-7

Option Base 1

Private s$

Sub tongji(s)

t = Len(s)

For i = 1 To t

If Asc(Mid(s, i, 1)) >= 65 AndAsc(Mid(s, i, 1)) <= 90 Then k = k + 1

If Asc(Mid(s, i, 1)) >= 97 AndAsc(Mid(s, i, 1)) <= 122 Then m = m + 1

Next i

Label1.Caption = k & "个大写字母"

Label2.Caption = m & "个小写字母"

End Sub

Private Sub Command1_Click()

s = Text1

Call tongji(s)

End Sub

习题6-8

Sub PI(s)

For i = 1 To s

t = t + (-1) ^ (i - 1) * (1 / (2 * i - 1))

Next i

Print t * 4

End Sub

Private Sub form_Click()

s = InputBox("输入数")

Call PI(s)

End Sub

习题6-9

Function fact(n)

p = 1

For i = 1 To n

p = p * i

Next i

fact = p

End Function

Sub sum(s)

t = 1

For i = 1 To s

t = t + 1 / (fact(i))

Next i

Print t

End Sub

Private Sub form_Click()

s = InputBox("输入数")

Call sum(s)

End Sub

习题6-10

Function fact(n%)

p = 1

For i = 1 To n

p = p * i

Next i

fact = p

End Function

Function power(x#, n%)

t = 1

For i = 1 To n

t = t * x

Next i

power = t

End Function

Sub sum(x#, s#)

Dim i%, t#

t = 1

For i = 1 To s

t = t + power(x, i) / fact(i)

Next i

Print t

End Sub

Private Sub form_Click()

Dim x#, s#

x = InputBox("输入数x")

s = InputBox("输入数s")

Call sum(x, s)

End Sub

习题6-11

Sub sxh()

For i = 100 To 999

If i = Mid(i, 1, 1) * Mid(i, 1, 1) * Mid(i,1, 1) + Mid(i, 2, 1) * Mid(i, 2, 1) * Mid(i, 2, 1) + Mid(i, 3, 1) * Mid(i, 3,1) * Mid(i, 3, 1) Then

Print i,

End If

Next i

End Sub

Private Sub Form_Click()

Call sxh

End Sub

习题6-12

Sub zf(x, n)

For i = 1 To (n + 1) / 2

For j = 1 To n

If j < i Or j > n + 1 - i Then

Print " ",

Else

Print x,

End If

Next j

Print

Print

Print

Next i

End Sub

Private Sub Form_Click()

Call zf("*", 15)

End Sub

习题6-13

Dim s$

Function szzh(m)

r = m Mod 8

If m > 8 Then Call szzh(m \ 8)

Text1 = Text1 + Mid(s, r + 1, 1)

End Function

Private Sub Form_Click()

s = "0123456789ABCDEF"

m = Val(Text1)

Call szzh(m)

End Sub

习题6-14

 

习题6-15

Function age(n)

If n = 1 Then p = 10

If n > 1 Then p = age(n - 1) + 2

age = p

End Function

Private Sub Form_Click()

Call age(5)

Print age(5)

End Sub

习题7-1

Private Sub Command1_Click()

Open "d:\sydata1.txt" For OutputAs #1

Print #1, "601", "杨大昌",1550

Print #1,

Print #1, "603", "李文圣",1886

Print #1,

Print #1, "605", "张  燕", 2235

Print #1,

Write #1, "601", "杨大昌",1550

Write #1,

Write #1, "603", "李文圣",1886

Write #1,

Write #1, "605", "张  燕", 2235

Write #1,

Close #1

End Sub

Private Sub Command2_Click()

Dim s As String

Open "d:\sydata1.txt" For InputAs #2

Do While Not EOF(2)

Line Input #2, s

Print s

Loop

Close #2

End Sub

Private Sub Command3_Click()

Open "d:\sydata1.txt" For AppendAs #3

Dim num As String, name As String, gz AsInteger

num = InputBox("编号")

name = InputBox("姓名")

gz = InputBox("工资")

Print #3, num, name, gz

Print #3,

End Sub

习题7-2

Private Sub Command1_Click()

Open "d:\sydata2.txt" For OutputAs #1

Print #1, Text1

Text1.Text = ""

Close #1

End Sub

Private Sub Command2_Click()

Dim s As String

Open "d:\sydata2.txt" For InputAs #2

Do While Not EOF(2)

Line Input #2, s

Text1.Text = Text1.Text + s + vbNewLine

Loop

Close #2

End Sub

习题7-3

Private Type sy

n As String

m As String

p As Integer

End Type

Dim ms As sy

Private Sub Form_load()

Open "d:\sydata1.txt" For InputAs #1

Open "d:\sydata3.txt" For OutputAs #2

Do While Not EOF(1)

 Input #1, ms.n, ms.m, ms.p

Print ms.n, ms.m, ms.p

Print #2, ms.n, ms.m, ms.p + 500

Loop

Close

End Sub

Private Sub Form_click()

Open "d:\sydata3.txt" For InputAs #3

Do While Not EOF(3)

Line Input #3, t

Print t

Print

Loop

End Sub

习题7-7

Private Type sy

num As String

name As String

gz As Integer

End Type

Dim t As sy

Private Sub Command1_Click()

For i = 1 To 3

t.num = InputBox("num")

t.name = InputBox("name")

t.gz = Val(InputBox("gz"))

Put #1, i, t

Next i

End Sub

Private Sub Command2_Click()

For i = 1 To 3

Get #1, i, t

Print t.num, t.name, t.gz

Next i

End Sub

Private Sub Form_Load()

Open "d:\sydata7.dat" For RandomAs #1

End Sub

Private Sub Form_Unload(Cancel As Integer)

Close

Unload Me

EndSub
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值