VB实验2 答案顺序结构程序设计.docx
《VB实验2 答案顺序结构程序设计.docx》由会员分享,可在线阅读,更多相关《VB实验2 答案顺序结构程序设计.docx(12页珍藏版)》请在冰点文库上搜索。
![VB实验2 答案顺序结构程序设计.docx](https://file1.bingdoc.com/fileroot1/2023-5/10/7df8599e-9c44-4254-9f80-f673a79aeefb/7df8599e-9c44-4254-9f80-f673a79aeefb1.gif)
VB实验2答案顺序结构程序设计
实验2顺序结构程序设计
2014年月日
姓名:
金梦仙
学号:
136330862
班级:
法政2班
短号:
658399
一、实验目的:
1.掌握表达式、赋值语句的正确书写规则。
2.掌握常用运算符的使用,理解运算符优先级。
3.掌握常用标准函数的使用。
4.掌握InputBox函数和MsgBox函数的使用。
二、实验内容:
请在窗体的合适位置写上你的姓名和学号(以后所有实验均有此要求)
1.完成实践教材实验2“三、实验内容”的题。
2.进入10.22.98.189\edu课程网站,并将此实验报告和代码文件一起打包压缩交到课程作业相应处,请务必在作业有效期内上交。
附部分原代码
P21
TextBox3.Text=Math.(Val()+Val(TextBox2.Text))
TextBox4.Text=(TextBox3.Text)
TextBox5.Text=(Val(TextBox3.Text),2)
P23
IfNotIsNumeric(TextBox1.Text)Then
MsgBox("输入有非数字字符,请重新输入",,"数据检验")
TextBox1.Text=""
TextBox1.Focus()
EndIf
三、实验步骤及结果:
(实验结果截图,代码拷贝过来。
)
1.
(1)
即时窗口=视图/其他窗口/输入immed
?
16/4-2^5*8/4mod5\2
4.0
?
int(199.555*100+0.5)/100
199.56
?
123+mid("123456",3,2)
157.0
?
ucase(mid("adcdefgh",3,4))
"CDEF"
?
"今天是"+today()
"今天是-3-10"
?
123&instr(“”,””)
"1235"
?
10>5and“人”>”人民”ortrue>false
False
?
10mod3=1or"电脑"<>"计算机"
True
?
math.Round(123.456,2)+math.Sqrt(9)
126.46
?
"abcdefg"like"*de*"or"A"="a"and2>1
True
(2)
第一个函数
x=1:
y=14
?
math.Sin(30*3.14/180)+(math.Exp
(2)+math.Log(10))/math.Sqrt(x+y+1)
2.9226804006242761
第二个函数
x=1:
y=0.5
?
math.Cos(75*3.14/180)+math.sqrt(x+math.Exp(3))/math.abs(x-y)-math.Log(3*x)
8.3446458014779559{Double}
Double:
8.3446458014779559{Double}
(3)
代码:
PublicClassForm1
PrivateSubTextBox1_TextChanged(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTextBox1.TextChanged
TextBox3.Text=Math.Sqrt(Val(TextBox1.Text)+Val(TextBox2.Text))
TextBox4.Text=Int(Val(TextBox3.Text))结果取整
TextBox5.Text=Math.Round(Val(TextBox3.Text),2)结果取两位小数
EndSub
PrivateSubTextBox2_TextChanged(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesTextBox2.TextChanged
TextBox3.Text=Math.Sqrt(Val(TextBox1.Text)+Val(TextBox2.Text))
TextBox4.Text=Int(Val(TextBox3.Text))
TextBox5.Text=Math.Round(Val(TextBox3.Text),2)
EndSub
EndClass
2.中等题
(1)
PublicClassForm1
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Randomize()随机
TextBox1.Text=Int(Rnd()*(99-10+1)+10)
TextBox2.Text=Int(Rnd()*(999-100+1)+100)
TextBox3.Text=Int(Rnd()*(9999-1000+1)+1000)
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
TextBox4.Text=(Val(TextBox1.Text)+Val(TextBox2.Text)+Val(TextBox3.Text))/3
TextBox4.Text=Format(Val(TextBox4.Text),"0.0")取一个小数点
EndSub
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
End
EndSub
EndClass
(2)
PublicClassForm1
PrivateSubTextBox1_KeyPress(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.KeyPressEventArgs)HandlesTextBox1.KeyPress
Dimn,a,b,c,d,mAsInteger
IfAsc(e.KeyChar)=13Then
n=Val(TextBox1.Text)
a=nMod10取个位
b=n\10Mod10取十位
c=n\100Mod10取百位
d=n\1000取千位
2:
m=a*1000+b*100+c*10+d
Label2.Text="逆序后整数为"&m
IfNotIsNumeric(TextBox1.Text)Then
MsgBox("输入有非数字字符,请重新输入",,"数据检验")
TextBox1.Text=""
TextBox1.Focus()
EndIf
EndIf
EndSub
PrivateSubTextBox1_MouseUp(ByValsenderAsObject,ByValeAsSystem.Windows.Forms.MouseEventArgs)HandlesTextBox1.MouseUp
EndSub
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
EndSub
EndClass
(3)
代码
PublicClassForm1
PrivateSubLabel2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesLabel2.Click
EndSub
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Label2.Text=Button1.Text&"函数的结果"
TextBox2.Text=Len(TextBox1.Text)求字符串的长度
EndSub
PrivateSubButton5_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton5.Click
Label2.Text=Button5.Text&"函数的结果"
Dimb$
b=InputBox("输入查找字串","inster函数")
TextBox2.Text=InStr(TextBox1.Text,b)
EndSub
PrivateSubButton4_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton4.Click
Label2.Text=Button4.Text&"函数的结果"
Dima%
a=Val(InputBox("请输入重复出现的次数(必须为数字)","strdup函数"))
TextBox2.Text=StrDup(a,TextBox1.Text)
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
Label2.Text=Button2.Text&"函数的结果"
TextBox2.Text=Trim(TextBox1.Text)
EndSub
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
Label2.Text=Button3.Text&"函数的结果"
Dimc%
c=Val(InputBox("从字符串左边第几个位置开始取","mid函数"))
Dimd%
d=Val(InputBox("向右取几个字符","mid函数"))
Ifc>0And0TextBox2.Text=Mid(TextBox1.Text,c,d)
EndIf
EndSub
PrivateSubButton6_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton6.Click
Label2.Text=Button6.Text&"函数的结果"
Dimm$字符串
m=InputBox("用什么来代替","replace函数")
Dimn$
n=InputBox("代替什么字符","replace函数")
TextBox2.Text=Replace(TextBox1.Text,n,m)m代替n
EndSub
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
EndSub
EndClass
(4)
vb
画图
Word
代码
PrivateSubButton1_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton1.Click
Shell("C:
\ProgramFiles(x86)\MicrosoftVisualStudio8\Common7\IDE\devenv.exe",1)
EndSub
PrivateSubButton2_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton2.Click
Shell("%windir%\system32\mspaint.exe",1)
EndSub
PrivateSubButton3_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesButton3.Click
Shell("D:
\ProgramFiles\MicrosoftOffice2007\MicrosoftOfficeWordPortable.exe",1)
EndSub
PrivateSubForm1_Load(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)HandlesMyBase.Load
EndSub
EndClass
四、实验心得:
1.即时窗口=视图/其他窗口/输入immed
2.TextBox4.Text=Int(Val(TextBox3.Text))结果取整
TextBox5.Text=Math.Round(Val(TextBox3.Text),2)结果取两位小数
3.Randomize()随机
TextBox1.Text=Int(Rnd()*(99-10+1)+10)2位数
TextBox2.Text=Int(Rnd()*(999-100+1)+100)3位数
TextBox3.Text=Int(Rnd()*(9999-1000+1)+1000)4位数
Int(rnd()*(上界-下界+1)+下界)
4.TextBox4.Text=Format(Val(TextBox4.Text),"0.0")取一个小数点
TextBox5.Text=Math.Round(Val(TextBox3.Text),2)结果取两位小数
5.Dimn,a,b,c,d,mAsInteger
IfAsc(e.KeyChar)=13Then
n=Val(TextBox1.Text)
a=nMod10取个位
b=n\10Mod10取十位
c=n\100Mod10取百位
d=n\1000取千位
m=a*1000+b*100+c*10+d
Label2.Text="逆序后整数为"&m
6IfNotIsNumeric(TextBox1.Text)Then
MsgBox("输入有非数字字符,请重新输入",,"数据检验")
Msgbox(提示,按钮样式,标题),省略部分要用逗号占位符跳过
7.inputbox(提示,标题,默认值)
8.shell函数在书本P52
是调用各种应用程序
Shell(路径,窗口类型)
9在VB上表示变量X在一定数值范围内(1应该X>1ANDX<=10!
!
!
!
教师评语: