1、vb程序设计题五、设计题 (请按照题目要求进行答题)1:编程,输入x,求下列分段函数的值,计算结果输出到Text控件中。Private Sub Command1_Click()Dim x As Singlex = InputBox(输入x, 计算分段函数)If x = 3 ThenText1 = Str(1 + x)ElseText1 = Str(x + 3) 0.25End IfEnd SubPrivate Sub Command1_Click()Dim x As Singlex = InputBox(输入x, 计算分段函数)If x = 3 ThenText1 = Str(1 + x)El
2、seText1 = Str(x + 3) 0.25End IfEnd Sub2:如下图,编程,点击按钮可以移动列表框中所选中表项的位置。说明:通过load事件添加列表项,运行时,先选择列表框中的选项,才可以使用“上移”或“下移”按钮。当选项是第一项时不能再上移,当选项是最后一项时不能再下移。参考答案:Private Sub Command1_Click()Dim x As Singlex = InputBox(输入x, 计算分段函数)If x = 3 ThenText1 = Str(1 + x)ElseText1 = Str(x + 3) 0.25End IfEnd Sub参考答案:Priva
3、te Sub Command1_Click() 上移Dim i As Integeri = List1.ListIndexList1.AddItem List1.Text, i - 1 先将选中的内容添加到上一项前List1.RemoveItem i + 1 将原来选中的那项删除List1.ListIndex = i - 1 将光标重新指向已经上移后的选项End SubPrivate Sub Command2_Click() 下移Dim i As Integeri = List1.ListIndexList1.AddItem List1.Text, i + 2 先将选中的内容添加到下一项后Li
4、st1.RemoveItem i 将原来选中的那项删除List1.ListIndex = i + 1 将光标重新指向已经下移后的选项End SubPrivate Sub List1_Click()Command1.Enabled = True: Command2.Enabled = TrueIf List1.ListIndex = 0 ThenCommand1.Enabled = False 第一项不能上移End IfIf List1.ListIndex = List1.ListCount - 1 ThenCommand2.Enabled = False 最后一项不能下移End IfEnd S
5、ub3:编写程序,将磁盘文件d:student.dat中若干个学生的姓名、出生年月、外语成绩和计算机成绩显示在列表框中,并且能实现以下功能:若文本框内容不为空,单击命令按钮“追加记录”则将文本框中的文本追加至列表框中;单击命令按钮“删除记录”则可删除列表框的选中项;首次运行时文件可以为空,单击“保存文件”命令按钮则保存文件。界面设计如图所示。参考答案:Private Sub Form_Load()Dim ss As StringOpen d:student.dat For Input As #1 While Not EOF(1)Line Input #1, ss: List1.AddItem
6、ssWendClose #1 End SubPrivate Sub Command1_Click() If Trim(Text1.Text) Then List1.AddItem Text1.TextText1.Text = End SubPrivate Sub Command2_Click() List1.RemoveItem List1.ListIndexEnd SubPrivate Sub Command3_Click() Dim I As IntegerOpen d:student.dat For Output As #1For I = 0 To List1.ListCount - 1
7、: Print #1, List1.List(I): Next IClose #1End SubPrivate Sub Command4_Click() EndEnd Sub4:设计一个简单的画板程序,可以根据选择的颜色,用鼠标的左键模拟画笔,在绘图区画线或画矩形;单击“清除”按钮后清除图片框的内容。(提示:每次画矩形时可先清除其它图案) 参考答案: Dim x1 As Single, y1 As Single Private Sub Command1_Click() CommonDialog1.ShowColor Picture1.ForeColor = CommonDialog1.Colo
8、r End Sub Private Sub Command2_Click() Picture1.Cls End Sub Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Picture1.CurrentX = X Picture1.CurrentY = Y x1 = X y1 = Y End Sub Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single,
9、 Y As Single) If Button = 1 Then If Option1.Value Then Picture1.Line -(X, Y) Else Picture1.Cls Picture1.Line (x1, y1)-(X, Y), , B End If End If End Sub五、设计题 (请按照题目要求进行答题)第1题:编程,用随机函数产生2个10100的整数,输出最大值。 参考答案: Private Sub Command1_Click() Dim x as integer,y as integer,max as integer Randomize x = Int(
10、Rnd * 91 + 10) y = Int(Rnd * 91 + 10) Max = x If y Max Then y = Max Print Max End Sub第2题:如下图,编写事件过程Command1_Click和Text3_Keypress(Keyascii As Integer)事件,运行时单击“出题”按钮,自动在Text1 和Text2中显示两个2位随机正整数;用户在Text3中输入运算结果,按回车后计算机对运算结果进行评判,并将评判结果在Label2中显示(对了或错了)。要求文本框Text3只接受数字和回车键。(回车键的Ascii码为13,数字键的Ascii码为48到57
11、) 参考答案: Dim x As Integer, y As Integer Private Sub Command1_Click() x = Int(Rnd * 90) + 10: y = Int(Rnd * 90) + 10 Text1.Text = CStr(x): Text2.Text = CStr(y) Text3.Text = Text3.SetFocus Label2.Visible = False: Command1.Enabled = False End Sub Private Sub Form_Load() Text1.Text = : Text2.Text = : Tex
12、t3.Text = Label2.Visible = False Randomize End Sub Private Sub Text3_KeyPress(KeyAscii As Integer) If (KeyAscii 57) And KeyAscii 13 Then KeyAscii = 0 End If If KeyAscii = 13 Then Label2.Visible = True If Val(Text3.Text) = x + y Then Label2.ForeColor = vbBlue: Label2.Caption = 对了 Else Label2.ForeColo
13、r = vbRed :Label2.Caption = 错了 End If Command1.Enabled = True End If End Sub第3题:在窗体上添加一个文本框和一个命令按钮。当用户在文本框中输入密码并单击命令按钮后,判断输入的密码是否为“123”,如果不是,则弹出对话框提示“密码不正确!单击 “确定”, 请再试一次”,并将文本框内容清空,焦点定位在文本框上;如果输入的密码是“123”,则打开文本文件e:aaa.txt,向文本框中写入:第I次登录 日期 时间(如:第9次登录 2008-6-30 10:11:00)。要求程序运行前先建一个文件e:aaa.txt,初始内容为:
14、第1次登录 2008-6-1 10:41:31,之后每次按顺序在文件末尾写入第2次 当前日期 当前时间、第3次登录 当前日期 当前时间。 参考答案: Private Sub Command1_click() Dim n As Integer, st as string If Text1.Text = 123 Then Open e:aaa.txt For Input As #1 Do While Not EOF(1) Line Input #1, st Loop st = Mid(st, 2, 1) n = 1 + Val(st) Close #1 Open e:aaa.txt For App
15、end As #1 Print #1, 第; Trim(n); 次登录; Now() Close #1 Else MsgBox 密码不正确!单击 “确定”, 请再试一次 Text1.Text = Text1.SetFocus End If End Sub第4题:设计一程序,界面如图所示。程序运行时,在图片框上画出一内切的椭圆;选择组合框中各选项可以改变椭圆的填充样式,单击“颜色”命令按钮可以改变椭圆的边框颜色,单击“清除”按钮后清除图片框的内容。 参考答案: Private Sub Combo1_Click() Picture1.FillStyle = Combo1.ListIndex Pic
16、ture1.Cls Form_Activate End Sub Private Sub Command1_Click() CommonDialog1.ShowColor Picture1.ForeColor = CommonDialog1.Color Picture1.Cls Form_Activate End Sub Private Sub Command2_Click() Picture1.Cls End Sub Private Sub Form_Activate() x = Picture1.ScaleWidth / 2 y = Picture1.ScaleHeight / 2 If x
17、 = y Then r = x Else r = y Picture1.Circle (x, y), r, , , , y / x End Sub参考答案:Private Sub Combo1_Click()Picture1.FillStyle=Combo1.ListIndexPicture1.ClsForm_ActivateEndSubPrivateSubCommand1_Click()CommonDialog1.ShowColorPicture1.ForeColor=CommonDialog1.ColorPicture1.ClsForm_ActivateEndSubPrivateSubCo
18、mmand2_Click()Picture1.ClsEndSubPrivateSubForm_Activate()x=Picture1.ScaleWidth/2y=Picture1.ScaleHeight/2Ifx=yThenr=xElser=yPicture1.Circle(x,y),r,y/xEndSub参考答案:Private Sub Combo1_Click()Picture1.FillStyle=Combo1.ListIndexPicture1.ClsForm_ActivateEndSubPrivateSubCommand1_Click()CommonDialog1.ShowColo
19、rPicture1.ForeColor=CommonDialog1.ColorPicture1.ClsForm_ActivateEndSubPrivateSubCommand2_Click()Picture1.ClsEndSubPrivateSubForm_Activate()x=Picture1.ScaleWidth/2y=Picture1.ScaleHeight/2Ifx=yThenr=xElser=yPicture1.Circle(x,y),r,y/xEndSub参考答案:Private Sub Combo1_Click()Picture1.FillStyle=Combo1.ListIn
20、dexPicture1.ClsForm_ActivateEndSubPrivateSubCommand1_Click()CommonDialog1.ShowColorPicture1.ForeColor=CommonDialog1.ColorPicture1.ClsForm_ActivateEndSubPrivateSubCommand2_Click()Picture1.ClsEndSubPrivateSubForm_Activate()x=Picture1.ScaleWidth/2y=Picture1.ScaleHeight/2Ifx=yThenr=xElser=yPicture1.Circ
21、le(x,y),r,y/xEndSub参考答案:Private Sub Combo1_Click()Picture1.FillStyle=Combo1.ListIndexPicture1.ClsForm_ActivateEndSubPrivateSubCommand1_Click()CommonDialog1.ShowColorPicture1.ForeColor=CommonDialog1.ColorPicture1.ClsForm_ActivateEndSubPrivateSubCommand2_Click()Picture1.ClsEndSubPrivateSubForm_Activat
22、e()x=Picture1.ScaleWidth/2y=Picture1.ScaleHeight/2Ifx=yThenr=xElser=yPicture1.Circle(x,y),r,y/xEndSub参考答案:Private Sub Combo1_Click()Picture1.FillStyle=Combo1.ListIndexPicture1.ClsForm_ActivateEndSubPrivateSubCommand1_Click()CommonDialog1.ShowColorPicture1.ForeColor=CommonDialog1.ColorPicture1.ClsFor
23、m_ActivateEndSubPrivateSubCommand2_Click()Picture1.ClsEndSubPrivateSubForm_Activate()x=Picture1.ScaleWidth/2y=Picture1.ScaleHeight/2Ifx=yThenr=xElser=yPicture1.Circle(x,y),r,y/xEndSub五、设计题 1:编程,求算式1+1/2!+1/3!+1/4!+前10项的和。 Private Sub Command1_Click() Dim I as integer, s as single,a as single a = 1:
24、s = 0 For i = 1 To 10 a = a / i s = s + a Next i Print 1+1/2!+1/3!+=; s End Sub 2:设计一个用于输入学生信息的应用程序。界面如图所示(民族有汉族、苗族、壮族等;政治面貌有:群众、团员、党员)。要求: (1)输入完毕后单击确定按钮,能在另一窗口显示输入的信息 (2)单击取消按钮,将取消所输入的信息。 Public info As String Dim s As String Private Sub Command1_Click() info = 学号为 + Text1.Text + ,姓名为 + Text2.Text
25、 + , 性别为 + s + ,民族为 + Combo1.Text + , 政治面貌为 + Combo2.Text+”.” Form2.Show End Sub Private Sub Command2_Click() Call Form_Load End Sub Private Sub Form_Load() Text1.Text = :Text2.Text = Option1.Value = True :Option2.Value = False Combo1.Text = 汉族 :Combo2.Text = 群众 End Sub Private Sub Option1_Click() s
26、 = 男 End Sub Private Sub Option2_Click() s = 女 End Sub 3:编程实现两个文本文件的合并,要求用驱动器、目录、文件列表框选择文件1,单击按钮1后,调用通用对话框选择文件2;单击按钮2后,将文件2的内容添加到文件1原来内容之后,然后删除文件2。 Dim fn As String Private Sub Command1_Click() CommonDialog1.Filter = (*.txt)|*.txt CommonDialog1.ShowOpen End Sub Private Sub Command2_Click() Dim s As
27、String Open fn For Append As #1 Open CommonDialog1.FileName For Input As #2 Do While Not EOF(2) Line Input #2, s Print #1, s Loop Close Kill CommonDialog1.FileName End Sub Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Private Su
28、b File1_Click() If Right(File1.Path, 1) = Then fn = File1.Path + File1.FileName Else fn = File1.Path + + File1.FileName End If End Sub Private Sub Form_Load() File1.Pattern = *.txt End Sub第4题:设计一程序,界面如图所示。程序运行后,单击“计时开始”命令按钮,开始计时,并在文本框1中以如图所示格式显示计时时间,单击“计时结束”命令按钮后,停止计时,并根据时间计算上网费用,计算的方法如下(不足15分钟忽略不计,超过15分钟则按1小时计算)。 参考答案: Dim n As Integer, h As Integer, m As Integer Private Sub Command1_Click() Timer1.Enabled = True End Sub Private Sub Command2_Click() Timer1.Enabled = False If m = 15
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1