vb程序设计题.docx

上传人:b****6 文档编号:5646138 上传时间:2022-12-30 格式:DOCX 页数:79 大小:1.40MB
下载 相关 举报
vb程序设计题.docx_第1页
第1页 / 共79页
vb程序设计题.docx_第2页
第2页 / 共79页
vb程序设计题.docx_第3页
第3页 / 共79页
vb程序设计题.docx_第4页
第4页 / 共79页
vb程序设计题.docx_第5页
第5页 / 共79页
点击查看更多>>
下载资源
资源描述

vb程序设计题.docx

《vb程序设计题.docx》由会员分享,可在线阅读,更多相关《vb程序设计题.docx(79页珍藏版)》请在冰豆网上搜索。

vb程序设计题.docx

vb程序设计题

五、设计题 (请按照题目要求进行答题)

1:

编程,输入x,求下列分段函数的值,计算结果输出到Text控件中。

PrivateSubCommand1_Click()

DimxAsSingle

x=InputBox("输入x","计算分段函数")

Ifx<=3Then

Text1=Str(1+x)

Else

Text1=Str(x+3)^0.25

EndIf

EndSubPrivateSubCommand1_Click()

DimxAsSingle

x=InputBox("输入x","计算分段函数")

Ifx<=3Then

Text1=Str(1+x)

Else

Text1=Str(x+3)^0.25

EndIf

EndSub

2:

如下图,编程,点击按钮可以移动列表框中所选中表项的位置。

说明:

通过load事件添加列表项,运行时,先选择列表框中的选项,才可以使用“上移”或“下移”按钮。

当选项是第一项时不能再上移,当选项是最后一项时不能再下移。

参考答案:

PrivateSubCommand1_Click()

DimxAsSingle

x=InputBox("输入x","计算分段函数")

Ifx<=3Then

Text1=Str(1+x)

Else

Text1=Str(x+3)^0.25

EndIf

EndSub

参考答案:

PrivateSubCommand1_Click()'上移

DimiAsInteger

i=List1.ListIndex

List1.AddItemList1.Text,i-1'先将选中的内容添加到上一项前

List1.RemoveItemi+1'将原来选中的那项删除

List1.ListIndex=i-1'将光标重新指向已经上移后的选项

EndSub

PrivateSubCommand2_Click()'下移

DimiAsInteger

i=List1.ListIndex

List1.AddItemList1.Text,i+2'先将选中的内容添加到下一项后

List1.RemoveItemi'将原来选中的那项删除

List1.ListIndex=i+1'将光标重新指向已经下移后的选项

EndSub

PrivateSubList1_Click()

Command1.Enabled=True:

Command2.Enabled=True

IfList1.ListIndex=0Then

Command1.Enabled=False'第一项不能上移

EndIf

IfList1.ListIndex=List1.ListCount-1Then

Command2.Enabled=False'最后一项不能下移

EndIf

EndSub

3:

编写程序,将磁盘文件d:

\student.dat中若干个学生的姓名、出生年月、外语成绩和计算机成绩显示在列表框中,并且能实现以下功能:

若文本框内容不为空,单击命令按钮“追加记录”则将文本框中的文本追加至列表框中;单击命令按钮“删除记录”则可删除列表框的选中项;首次运行时文件可以为空,单击“保存文件”命令按钮则保存文件。

界面设计如图所示。

参考答案:

PrivateSubForm_Load()

DimssAsString

Open"d:

\student.dat"ForInputAs#1

WhileNotEOF

(1)

LineInput#1,ss:

List1.AddItemss

Wend

Close#1

EndSub

PrivateSubCommand1_Click()

IfTrim(Text1.Text)<>""ThenList1.AddItemText1.Text

Text1.Text=""

EndSub

PrivateSubCommand2_Click()

List1.RemoveItemList1.ListIndex

EndSub

PrivateSubCommand3_Click()

DimIAsInteger

Open"d:

\student.dat"ForOutputAs#1

ForI=0ToList1.ListCount-1:

Print#1,List1.List(I):

NextI

Close#1

EndSub

PrivateSubCommand4_Click()

End

EndSub

4:

设计一个简单的画板程序,可以根据选择的颜色,用鼠标的左键模拟画笔,在绘图区画线或画矩形;单击“清除”按钮后清除图片框的内容。

(提示:

每次画矩形时可先清除其它图案)

参考答案:

Dimx1AsSingle,y1AsSingle

PrivateSubCommand1_Click()

CommonDialog1.ShowColor

Picture1.ForeColor=CommonDialog1.Color

EndSub

PrivateSubCommand2_Click()

Picture1.Cls

EndSub

PrivateSubPicture1_MouseDown(ButtonAsInteger,ShiftAsInteger,

XAsSingle,YAsSingle)

Picture1.CurrentX=X

Picture1.CurrentY=Y

x1=X

y1=Y

EndSub

PrivateSubPicture1_MouseMove(ButtonAsInteger,ShiftAsInteger,

XAsSingle,YAsSingle)

IfButton=1Then

IfOption1.ValueThen

Picture1.Line-(X,Y)

Else

Picture1.Cls

Picture1.Line(x1,y1)-(X,Y),,B

EndIf

EndIf

EndSub

五、设计题 (请按照题目要求进行答题)

第1题:

编程,用随机函数产生2个10~100的整数,输出最大值。

参考答案:

PrivateSubCommand1_Click()

Dimxasinteger,yasinteger,maxasinteger

Randomize

x=Int(Rnd*91+10)

y=Int(Rnd*91+10)

Max=x

Ify>MaxTheny=Max

PrintMax

EndSub

第2题:

如下图,编写事件过程Command1_Click和Text3_Keypress(KeyasciiAsInteger)事件,运行时单击“出题”按钮,自动在Text1和Text2中显示两个2位随机正整数;用户在Text3中输入运算结果,按回车后计算机对运算结果进行评判,并将评判结果在Label2中显示(对了或错了)。

要求文本框Text3只接受数字和回车键。

(回车键的Ascii码为13,数字键的Ascii码为48到57)

参考答案:

DimxAsInteger,yAsInteger

PrivateSubCommand1_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

EndSub

PrivateSubForm_Load()

Text1.Text="":

Text2.Text="":

Text3.Text=""

Label2.Visible=False

Randomize

EndSub

PrivateSubText3_KeyPress(KeyAsciiAsInteger)

If(KeyAscii<48OrKeyAscii>57)AndKeyAscii<>13Then

KeyAscii=0

EndIf

IfKeyAscii=13Then

Label2.Visible=True

IfVal(Text3.Text)=x+yThen

Label2.ForeColor=vbBlue:

Label2.Caption="对了"

Else

Label2.ForeColor=vbRed:

Label2.Caption="错了"

EndIf

Command1.Enabled=True

EndIf

EndSub

第3题:

在窗体上添加一个文本框和一个命令按钮。

当用户在文本框中输入密码并单击命令按钮后,判断输入的密码是否为“123”,如果不是,则弹出对话框提示“密码不正确!

单击“确定”,请再试一次”,并将文本框内容清空,焦点定位在文本框上;如果输入的密码是“123”,则打开文本文件e:

\aaa.txt,向文本框中写入:

第I次登录日期时间(如:

第9次登录2008-6-3010:

11:

00)。

要求程序运行前先建一个文件e:

\aaa.txt,初始内容为:

第1次登录2008-6-110:

41:

31,之后每次按顺序在文件末尾写入第2次当前日期当前时间、第3次登录当前日期当前时间……。

参考答案:

PrivateSubCommand1_click()

DimnAsInteger,stasstring

IfText1.Text="123"Then

Open"e:

\aaa.txt"ForInputAs#1

DoWhileNotEOF

(1)

LineInput#1,st

Loop

st=Mid(st,2,1)

n=1+Val(st)

Close#1

Open"e:

\aaa.txt"ForAppendAs#1

Print#1,"第";Trim(n);"次登录";Now()

Close#1

Else

MsgBox"密码不正确!

单击“确定”,请再试一次"

Text1.Text=""

Text1.SetFocus

EndIf

EndSub

第4题:

设计一程序,界面如图所示。

程序运行时,在图片框上画出一内切的椭圆;选择组合框中各选项可以改变椭圆的填充样式,单击“颜色”命令按钮可以改变椭圆的边框颜色,单击“清除”按钮后清除图片框的内容。

参考答案:

PrivateSubCombo1_Click()

Picture1.FillStyle=Combo1.ListIndex

Picture1.Cls

Form_Activate

EndSub

PrivateSubCommand1_Click()

CommonDialog1.ShowColor

Picture1.ForeColor=CommonDialog1.Color

Picture1.Cls

Form_Activate

EndSub

PrivateSubCommand2_Click()

Picture1.Cls

EndSub

PrivateSubForm_Activate()

x=Picture1.ScaleWidth/2

y=Picture1.ScaleHeight/2

Ifx>=yThenr=xElser=y

Picture1.Circle(x,y),r,,,,y/x

EndSub参考答案:

PrivateSubCombo1_Click()

    Picture1.FillStyle = Combo1.ListIndex

    Picture1.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 >= y Then r = x Else r = y

    Picture1.Circle (x, y), r, , , , y / x

End Sub参考答案:

PrivateSubCombo1_Click()

    Picture1.FillStyle = Combo1.ListIndex

    Picture1.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 >= y Then r = x Else r = y

    Picture1.Circle (x, y), r, , , , y / x

End Sub参考答案:

PrivateSubCombo1_Click()

    Picture1.FillStyle = Combo1.ListIndex

    Picture1.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 >= y Then r = x Else r = y

    Picture1.Circle (x, y), r, , , , y / x

End Sub参考答案:

PrivateSubCombo1_Click()

    Picture1.FillStyle = Combo1.ListIndex

    Picture1.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 >= y Then r = x Else r = y

    Picture1.Circle (x, y), r, , , , y / x

End Sub参考答案:

PrivateSubCombo1_Click()

    Picture1.FillStyle = Combo1.ListIndex

    Picture1.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 >= y Then r = x Else r = y

    Picture1.Circle (x, y), r, , , , y / x

End Sub

五、设计题

 1:

编程,求算式1+1/2!

+1/3!

+1/4!

+……前10项的和。

PrivateSubCommand1_Click()

DimIasinteger,sassingle,aassingle

a=1:

s=0

Fori=1To10

a=a/i

s=s+a

Nexti

Print"1+1/2!

+1/3!

+……=";s

EndSub

2:

设计一个用于输入学生信息的应用程序。

界面如图所示(民族有汉族、苗族、壮族等;政治面貌有:

群众、团员、党员)。

要求:

(1)输入完毕后单击确定按钮,能在另一窗口显示输入的信息

(2)单击取消按钮,将取消所输入的信息。

PublicinfoAsString

DimsAsString

PrivateSubCommand1_Click()

info="学号为"+Text1.Text+",姓名为"+Text2.Text+",性别为"+s+

",民族为"+Combo1.Text+",政治面貌为"+Combo2.Text+”.”

Form2.Show

EndSub

PrivateSubCommand2_Click()

CallForm_Load

EndSub

PrivateSubForm_Load()

Text1.Text="":

Text2.Text=""

Option1.Value=True:

Option2.Value=False

Combo1.Text="汉族":

Combo2.Text="群众"

EndSub

PrivateSubOption1_Click()

s="男"

EndSub

PrivateSubOption2_Click()

s="女"

EndSub

3:

编程实现两个文本文件的合并,要求用驱动器、目录、文件列表框选择文件1,单击按钮1后,调用通用对话框选择文件2;单击按钮2后,将文件2的内容添加到文件1原来内容之后,然后删除文件2。

DimfnAsString

PrivateSubCommand1_Click()

CommonDialog1.Filter="(*.txt)|*.txt"

CommonDialog1.ShowOpen

EndSub

PrivateSubCommand2_Click()

DimsAsString

OpenfnForAppendAs#1

OpenCommonDialog1.FileNameForInputAs#2

DoWhileNotEOF

(2)

LineInput#2,s

Print#1,s

Loop

Close

KillCommonDialog1.FileName

EndSub

PrivateSubDir1_Change()

File1.Path=Dir1.Path

EndSub

PrivateSubDrive1_Change()

Dir1.Path=Drive1.Drive

EndSub

PrivateSubFile1_Click()

IfRight(File1.Path,1)="\"Then

fn=File1.Path+File1.FileName

Else

fn=File1.Path+"\"+File1.FileName

EndIf

EndSub

PrivateSubForm_Load()

File1.Pattern="*.txt"

EndSub

第4题:

设计一程序,界面如图所示。

程序运行后,单击“计时开始”命令按钮,开始计时,并在文本框1中以如图所示格式显示计时时间,单击“计时结束”命令按钮后,停止计时,并根据时间计算上网费用,计算的方法如下(不足15分钟忽略不计,超过15分钟则按1小时计算)。

参考答案:

DimnAsInteger,hAsInteger,mAsInteger

PrivateSubCommand1_Click()

Timer1.Enabled=True

EndSub

PrivateSubCommand2_Click()

Timer1.Enabled=False

Ifm>=15

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 经管营销

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1