VB程序设计基础__申石磊_答案.doc_第1页
VB程序设计基础__申石磊_答案.doc_第2页
VB程序设计基础__申石磊_答案.doc_第3页
VB程序设计基础__申石磊_答案.doc_第4页
VB程序设计基础__申石磊_答案.doc_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

VB程序设计课后习题参考答案2-1Private Sub Form_Click() ClsEnd SubPrivate Sub Form_DblClick() FontSize = 40 FontName = 隶书 ForeColor = vbRed Print 热烈庆祝 FontSize = 60 FontName = 宋体 ForeColor = vbGreen Print XX会召开End Sub2-2Private Sub Command1_Click() Label1.Move Width * Rnd, Height * RndEnd Sub2-3Private Sub Command1_Click() Label1.Visible = TrueEnd SubPrivate Sub Command2_Click() Label1.Visible = FalseEnd Sub2-4Private Sub Timer1_Timer() Text1.Text = Now Caption = Text1.TextEnd Sub2-5Private Sub Timer1_Timer() If BackColor = vbRed Then BackColor = vbYellow Else BackColor = vbRed End IfEnd Sub2-6Private Sub Timer1_Timer() If Label1.Left -Label1.Height Then Label1.Top = Label1.Top - 100 Else Label1.Top = Form1.Height End IfEnd Sub3-2Private Sub Form_Click() Dim n As Integer Randomize n = Int(90 * Rnd) + 10) Print nEnd Sub3-3Private Sub Timer1_Timer() Cls Print XX学院 FontSize = Int(71 * Rnd + 10)End Sub3-4Private Sub Command1_Click() n = Text1.Text Text2.Text = Hex(Asc(Right(Text1.Text, 1)End SubPrivate Sub Command2_Click() Text1.Text = Text2.Text = Text1.SetFocusEnd Sub3-5Private Sub Command1_Click() Dim m As Date m = Text1.Text n = Date Text2.Text = n - mEnd SubPrivate Sub Command2_Click() Text1.Text = Text2.Text = Text1.SetFocusEnd Sub3-6 Dim b As String Dim n As String Dim m As StringPrivate Sub Command1_Click() m = Left(Text1, 1) n = Right(Text1, 1) b = Mid(Text1, 2, 1) Text2.Text = n & b & mEnd SubPrivate Sub Command2_Click() Text1.Text = Text2.Text = Text1.SetFocusEnd Sub3-7Private Sub Form_click() r = InputBox(请输入字符) Print Left(r, 1) & Mid(r, 3, 1) & Right(r, 1)End Sub3-8Private Sub Timer1_Timer() Label1.FontSize = Rnd * 40 + 10 Label1.BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256) Label1.ForeColor = QBColor(Int(16 * Rnd)End Sub3-10Private Sub Form_Click() 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元张数: & fEnd Sub 4-1Private Sub Command1_Click() Dim p As Single, r As Single, n As Single p = Text1 r = Text2 n = Text3 s = p * (1 + r) n MsgBox 您的本息是: & Round(s, 2)End Sub4-2Private Sub Form_click() Dim max As Single, min As Single a! = InputBox(请输入第一个实数) b! = InputBox(请输入第二个实数) If a b Then Print a, b Else Print b, a End If End Sub4-3Private Sub Form_click() Cls a! = InputBox(请输入年份) If a Mod 4 = 0 And a Mod 100 0 & a Mod 400 = 0 Then Print 您刚刚输入的年份为闰年! Else Print 您刚刚输入的年份不是闰年! End IfEnd Sub4-4Private Sub Form_click() 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 End IfEnd Sub4-5Private Sub Command1_Click() Dim x! x = Text1 If x 60 Then Text2 = 重考! ElseIf x 70 Then Text2 = 及格! ElseIf x 80 Then Text2 = 中等! ElseIf x 90 Then Text2 = 良好! Else Text2 = 优秀! End IfEnd SubPrivate Sub Command2_Click() Text1 = Text2 = End Sub4-6Private Sub Command1_Click() Dim x! x = Text1 Select Case x Case Is 60 Text2 = 重考! Case Is 70 Text2 = 及格! Case Is 80 Text2 = 中等! Case Is 90 Text2 = 良好! Case Else Text2 = 优秀! End SelectEnd SubPrivate Sub Command2_Click() Text1 = Text2 = End Sub4-7 Dim s As StringPrivate Sub Form_Click() s = InputBox(请输入年龄) If s 30 Then Print 您的年龄为; s, , 请参加长跑运动 ElseIf s 40 Then Print 您的年龄为; s, , 请参加跳绳运动 ElseIf s 100 Print 1-2+3-4+5-6+99-100=; sEnd Sub4-12Private Sub Form_Click() Dim n As Integer Dim m As Integer Dim s As Integer m% = InputBox(请输入正整数m(m=1), 阶乘) n% = InputBox(请输入正整数n(n=m), 阶乘) For m = m To n t = 1 For i = 1 To m t = t * i Next i s = s + t Next m Print m!+(m+1)!+n!=; sEnd Sub4-13Private Sub Form_click() Dim a% Dim b% Dim c% 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 aEnd Sub4-14Private Sub Form_click() For n = 1 To 500 For i = 1 To n - 1 If n Mod i = 0 Then s = s + i Next i If s = n Then Print i; s = 0 Next n PrintEnd Sub4-15Private Sub Form_click() Dim i As Integer Dim n As Long n = 1 t = 1 Do While t 0.00001 e = e + t i = i + 1 n = n * i t = 1 / n Loop Print e的近似值是; eEnd Sub51Private SubForm_Click()Dim i%, x(1 To 5)As StringFor i = LBound(x)To UBound(x)x(i) =InputBox( x ( & i & ) )Next iFor i = UBound(x)To LBound(x) Step -1Print x(i)Next iPrintEnd Sub5-2Private Sub Form_Click() Dim i%, s s = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = 0 To 9 If s(i) Mod 3 = 0 Then Print s(i); i Else Print End If Next i PrintEnd Sub5-3Option Base 1Private Sub Command1_Click() Dim x() x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) t = x(1) For i = 1 To 9 x(i) = x(i + 1) Print x(i); Next x(10) = t Print x(i)End SubPrivate Sub Command2_Click() Dim x() x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) t = x(10) For i = 9 To 1 Step -1 x(i + 1) = x(i) Next i x(1) = t For i = 1 To 10 Print x(i); Next iEnd Sub5-4Private Sub Form_Click() Dim a(5, 5) For i = 1 To 5 Sum = 0 For j = 1 To 5 a(i, j) = Int(Rnd * 51 + 10) Sum = Sum + a(i, j) Print a(i, j); Next j Print Sum Next i Print For i = 1 To 5 For j = 1 To 5 If i = j Then Print a(i, j); Next j Next i PrintEnd Sub5-5Private Sub Form_Click() Dim a() As Integer Dim Max As Integer, iMax As Integer, jMax As Integer Dim Min As Integer, iMin As Integer, jMin As Integer M = InputBox(输入M) N = InputBox(输入N) ReDim a(M, N) For i = 1 To M For j = 1 To N a(i, j) = Int(Rnd * 900) + 100 Next j Next i For i = 1 To M For j = 1 To N Print a(i, j); Next j Print Next i Max = a(1, 1): Min = a(1, 1) For i = 1 To M For j = 1 To N If a(i, j) Max Then Max = a(i, j): iMax = i: jMax = j f a(i, j) Min Then Min = a(i, j): iMin = i: jMin = j Next j Next i Print Max=; Max, iMax=; iMax, jMax=; jMax Print Min=; Min, iMin=; iMin, jMin=; jMin Erase a End Sub5-6Private Sub Form_Click() n = InputBox(n) ReDim a(n, n) For i = 1 To n For j = 1 To n If i = j Then a(i, j) = 1 Else a(i, j) = 0 End If Next j Next i For i = 1 To n For j = 1 To n Print a(i, j); Next j Print Next i Erase aEnd Sub5-7Private Sub Form_Click() Dim x As Variant x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) For i = 1 To 10 Print x(i); Next i Print For i = 1 To 7 x(i) = x(i + 3) Print x(i); Next i Print ReDim Preserve x(i) x = Array(1, 2, 3, 4, 5, 6, 7) or i = 1 To 7 Print x(i); Next i PrintEnd Sub5-8 Option Base 1Private Sub Command1_Click(Index As Integer) Select Case Index Case 1 Text3 = Val(Text1) + Val(Text2) Case 2 Text3 = Text1 - Text2 Case 3 Text3 = Text1 * Text2 Case 4 Text3 = Text1 / Text2 End SelectEnd Sub6-1Private Sub Form_click() Dim x As Single Dim y As Single Dim z As Single x = InputBox(请输入圆柱体的半径) y = InputBox(请输入圆柱体的高度) volume x, y, z Print 圆柱体的体积为; zEnd Sub 6-2窗体模块Private Sub Form_click() Dim a!, b!, c! a = InputBox(请输入圆柱体的半径) b = InputBox(请输入圆柱体的高度) c = volume(a, b) Print 圆柱体的体积为; cEnd Sub标准模块Function volume(r As Single, h As Single) As Single volume = 3.14 * r * r * hEnd Function6-3Private Sub Form_Click() Dim y% y = InputBox(请输入一个整数) Call week(y)End Sub6-4窗体模块Private Sub Form_click() Dim a As Integer, b As String a = InputBox(请输入一个整数) b = week(a) Print b标准模块Function week(s As Integer) As String Select Case s Case 0 week = 星期日 Case 1 week = 星期一 Case 2 week = 星期二 Case 3 week = 星期三 Case 4 week = 星期四 Case 5 week = 星期五 Case 6 week = 星期六 Case Else MsgBox 数据无效 End SelectEnd Function6-5窗体模块Private Sub Form_click() Dim n As Single, w As String n = InputBox(请输入一个正整数) Select Case w Case judge = 0 Print 此数为偶数 Case Else Print 此数为奇数 Print w End SelectEnd Sub标准模块Function w(n As Single) As String w = n 2 Select Case w Case w = 0 Print 此数为偶数 Case Else Print 此数为奇数End Function6-7Sub lenUL(ByVal s As String, U%, L%) Dim i As Integer For i = 1 To Len(s) Select Case Mid(s, i, 1) Case A To Z U = U + 1 Case a To z L = L + 1 End Select Next iEnd SubPrivate Sub Command1_Click() Dim U%, L% Call lenUL(Text1, U, L) Text2 = U Text3 = LEnd SubPrivate Sub Command2_Click() Text1 = Text2 = Text3 = Text1.SetFocusEnd Sub7-1Private Sub Command1_Click() Image1.Picture = LoadPicture(c:图片picture5.JPG)End SubPrivate Sub Command2_Click() Image1.Picture = LoadPicture(c:图片picture3.JPG)End SubPrivate Sub Command3_Click() Image1.Picture = LoadPicture(c:图片picture4.JPG)End Sub7-2Private Sub Command1_Click() Static a As Integer a = a + 1 If a Mod 2 0 Then Image1.Picture = LoadPicture(c:图片p2.JPG) Image2.Picture = LoadPicture(c:图片p1.JPG) Else Image1.Picture = LoadPicture(c:图片p1.JPG) Image2.Picture = LoadPicture(c:图片p2.JPG) End IfEnd SubPrivate Sub Form_Load() Image1.Picture = LoadPicture(c:图片p1.JPG) Image2.Picture = LoadPicture(c:图片p2.JPG)End Sub7-3Private Sub Timer1_Timer() Form1.Picture = Image3.Picture Image3.Picture = Image2.Picture Image2.Picture = Image1.Picture Image1.Picture = Form1.PictureEnd Sub7-4Private Sub Form_Load() Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer() If Image2.Top = -Image2.Height Then Image2.Top = Image2.Top - 100 Else Image2.Top = -Image2.Height End IfEnd Sub 7-5Private Sub Form_Load() HScroll1.Min = 0 HScroll1.Max = Image1.Width Image1.Width = 0End SubPrivate Sub HScroll1_Scroll() Image1.Width = HScroll1 Label1 = 当前图片宽度: & HScroll1.ValueEnd Sub7-6Private Sub Command1_Click() For i = ProgressBar1.Min To ProgressBar1.Max ProgressBar1.Value = i (For j = 1 To 20000 Next j) (延时) Next iEnd Sub7-7Private Sub Command1_Click() a = IIf(Option1, 男, 女) b = IIf(Option3, 硕士, 博士) c = IIf(Check1, 音乐, ) d = IIf(Check2, 演讲, ) e = IIf(Check3, 体育, ) f = IIf(Check4, 集邮, ) Text2 = 姓名: & Text1.Text & , & 性别: & a & , & 学历: & b & , & 特长: & c & , & d & , & e & , & fEnd SubPrivate Sub Command2_Click() Text1 = Text2 = Text1.SetFocusEnd Sub7-8Private Sub Form_Load() Combo1.AddItem 湖北省 Combo1.AddItem 武汉市 Combo1.AddItem XX大学 Combo1.AddItem XX校区 Combo1.AddItem 经管学院 Combo1.AddItem 12级 Combo1.AddItem 物流管理专业 Combo1.AddItem XX Combo1.AddItem 毕业于 Combo1.AddItem XX省 Combo1.AddItem XX市 Combo1.AddItem XX中学 Combo1.AddItem XX班End SubPrivate Sub Combo1_Click() List1.AddItem Combo1.TextEnd SubPrivate Sub List1_DblClick() List1.RemoveItem List1.ListIndexEnd Sub8-1Dim x As Single, y As SinglePrivate Sub Pt1_Click() Pt1.Scale (0, 100)-(100, 0) Pt1.DrawWidth = 2 For x = 0 To 100 Step 0.02 Pt1.PSet (x, 2), vbRed Next x For y = 0 To 100 Step 0.02 Pt1.PSet (2, y), vbRed Next y Pt1.CurrentX = 2 Pt1.CurrentY = 5 Pt1.Print (0,0)End Sub8-2Private Sub P1_Click() P1.Scale (0, 100)-(100, 0) P1.FillStyle = 7 P1.DrawWidth = 5 P1.FillColor = vbGreen P1.Line (10, 90)-(90, 10), vbRed, BEnd Sub8-3Private Sub Form_click() Scale (-100, 100)-(100, -100) Line (-100, 0)-(100, 0) Line (0, -100)-(0, 100) CurrentX = 2 CurrentY = 6 Print (0,0)End Sub8-4Private Sub Form_click()Scale (-100, 100)-(100, -100) Line (-100, 0)-(100, 0) Line (0, -100)-(0, 100) For x = -100 To 100 Step 0.02 y = 10 * Sin(x) - 10 * Cos(2 * x) PSet (x, y), vbRed Next xEnd Sub8-7Private Sub Form_Click() Timer1.Enabled = TrueEnd SubPrivate Sub Timer1_Timer() Randomize Scale (-120, 120)-(120, -120) DrawWidth = 2 If r 100 Then r = r + 2 For r = 0 To 100 Circle (0, 0), r, RGB(255 * Rnd, 255 * Rnd, 255 * Rnd) Next r End IfEnd Sub9-1Private Sub Text1_KeyPress(K As Integer) K = K + 3End Sub9-2Private Sub Text1_KeyPress(K As Integer) Select Case K Case 48 To 57 K = K Case Else K = 0 End SelectEnd Sub9-6Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) MousePointer = 5End SubPrivate Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) MousePointer = 11End SubPrivate Sub Picture1_Click() MouseIcon = Picture1.MouseIcon Picture1.MouseIcon = Image1.MouseIcon Image1.MouseIcon = Form1.MouseIconEnd Sub 9-7Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Shift = vbCtrlMask And Button = vbLeftButton Then Print Ctrl+左键+移动 If Shift = vbAltMask And Button = vbRightButton Then Print Alt+右键+移动End SubSub Form_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single) ClsEnd Sub9-8Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = vbLeftButton Then Picture1.Print Str(X) + , + Str(Y) BackColor = RGB(Rnd * 256, Rnd * 256, Rnd * 256)End Sub10-1Private Sub Command1_Click() Open d:data1.txt For Output As #1 Write #1, 1201, 张三; 4550 Write #1, 1203, 李四; 3886 Write #1, 1205, 王五; 2235 Close #1End SubPrivate Sub Command2_Click() Open d:data1.txt For Input As #2 Do While Not EOF(2) Input #2, X1, X2, x3 Print X1, X2, x3 Loop CloseEnd Sub 10-2Private Sub Command1_Click() Open d:data1.txt For Append As #3 Write #3, Text1, Text2, Text3 Text1 = Text2 = Text3 = CloseEnd SubPrivate Sub Command2_Click() Close EndEnd Sub10-5Private Sub Form_Click() Dim sum As Long For n = 1 To 100 Step 2 sum = sum + n Next n Print sum End SubPrivate Sub Form_Load() Open d:data5.txt For Output As #1 For n = 1 To 100 Step 1 Print #1, n; Next n CloseEnd Sub10-6Private Sub Form_Click()Dim sum As Integer For n = 10 To 100 Step 10 sum = sum + n Next Print sumEnd SubPrivate Sub Form_Load() Open d:data6.dat For Random As #1 For n = 1 To 100 Put #1, , n NextEnd Sub11-1Private Sub Command1_Click() CommonDialog1.Action = 3 Text1.BackColor = CommonDialog1.ColorEnd SubPrivate Sub Command2_Click() CommonDialog1.Flags = 263 CommonDialog1.Action = 4 Text1.FontSize = CommonDialog1.FontSize Text1.FontName = CommonDialog1.FontName Text1.FontBold = CommonDialog1.FontBold Text1.FontS

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论