vb实验答案

合集下载

《程序设计基础—VB程序实验》

《程序设计基础—VB程序实验》

《程序设计基础—VB程序实验》说明:实验1和实验2均为电子实验报告,后面8个实验是纸制实验报告。

实验1 计算机基本操作实验目的⑴熟悉计算机,能够正确进行开、关机操作。

⑵熟悉键盘,掌握键盘上所有字符的输入,掌握正确的指法,能够进行中、英文切换。

⑶理解操作系统概念,掌握Windows XP的基本操作,能够利用“控制面板”进行一些参数的设置。

⑷知道“回收站”的功能,学会有关文件、文件夹的基本操作。

⑸学会在计算机上搜索或查找相关的文件或文件夹,掌握通配符的使用。

实验内容⑴熟悉上机实验的计算机,熟悉键盘,学会中、英文输入方式的转换。

⑵学会用正确的指法进行输入,知道键盘上所有字符的输入。

⑶利用“控制面板”修改系统时间、桌面背景、鼠标属性。

⑷在桌面上新建一个文件夹,该文件夹以学生自己的名字+班级+学号的后3位命名。

⑸用文本方式或Word文档,写一段字数不少于300字的自我介绍,以1.doc命名并保存到上述文件夹中。

⑹将上述文件夹复制到ftp://10.10.9.222上。

实验2 常用软件的基本操作实验目的⑴了解Word功能,掌握Word的基本操作,包括页眉/页脚的设置、分栏、页面设置、添加底纹等。

⑵学会文件的保存与打开、打印预览、段落的间隔、首字符位置改变等,能够在Word中插入图片、表格、艺术字、特殊符号等。

⑶了解Excel的功能,掌握Excel的基本操作,包括单元格属性的设置、数字的几种填充形式、单元格的合并与恢复、边框的设置等。

⑷掌握Excel的基本公式的使用,能够用公式进行基本计算。

掌握Excel中的图形制作方法,能够根据表格做出图形。

实验内容⑴将ftp上自己文件夹中的1.doc文件复制到桌面上,然后做如下处理:①将标题“自我介绍”居中;②将文章中第二段的内容进行分栏,要求中间有分割线;③插入一个4行4列的表格,内容为学号、姓名、某门课的成绩;④任意插入一张图片;⑤在文章中的任意位置加底纹。

将做好的文档以2.doc命名并保存。

答案:计算并输出圆的周长和面积。在窗体上显示和及平均值。利用InputBox函数将字符串进行相加和联接

答案:计算并输出圆的周长和面积。在窗体上显示和及平均值。利用InputBox函数将字符串进行相加和联接
VB实验报告书(实验四)
班级:管08A-5学号:10901060201姓名:张跃洋
实验题目
顺序结构
实验目的
1.掌握赋值语句的使用方法
2.掌握数据输入、输出函数和语句,尤其是InputBox函数
实验内容
程序1:编程输入圆的半径,计算并输出圆的周长和面积。
方法一:用Inputbox函数实现圆半径的输入,用Print方法输出周长和面积。
c = "请输入第三个数""输入并计算"
d = "请输入第个数""输入并计算"
yi = InputBox(a, "输入成绩")
er = InputBox(b, "输入成绩")
san = InputBox(c, "输入成绩")
si = InputBox(d, "输入成绩")
qh = Val(yi) + Val(er) + Val(san) + Val(si)
方法二:用文本框控件实现半径的输入,用标签控件实际周长和面积的输出。
程序2:从键盘上输入4个数,编写程序,计算并输出这4个数的和及平均值。通过InputBox函数输入数据,在窗体上显示和及平均值。
Private Sub Command1_Click()
a = "请输入第一个数""输入并计算"
b = "请输入第二个数""输入并计算"
Text2.Text = Val(a) + Val(b)
End Sub
编译、运行过程中出现的主要问题及解决方法
&和+的区别:两个数值运算时

vb6.0实验六习题与答案

vb6.0实验六习题与答案

vb6.0实验六习题与答案实验六专业_1、认识常用控件:单选按钮、复选框、框架、列表框、组合框、滚动条、滑动器(slider)、时钟、图形控件;2.学习在表单中创建常用控件2、学习在属性窗口和在程序代码中设置属性3、学习调用控件的方法4.学习编写每个控件、鼠标和键盘的事件过程。

二、实验内容1、单选按钮、复选框、框架1)将表格F1的标题设置为“复选框、框架和单选按钮”;将表格中复选框ch1、ch2ch1和CH2的高度和宽度分别设置为360和1000;程序运行时,check2处于非零状态。

FR1帧包含单选按钮OP1和op2。

2)设计窗体,要求①标题为“设置字体”;② 带边框的标签控件Label1以表格形式设置,高度和宽度分别为450和3800,标题为“欢迎VB”,字体大小为“4”,并显示在中间;③标题为“字体”框架包含两个标题分别为“楷体_gb2312”和“华文新魏”的单选按钮op1和op2;④ 标题为“Italic”的框架包含两个标题为“是”和“否”的单选按钮。

⑤ 编写相应控件的事件进程,使程序运行时,点击“Kaiti_gb2312”,标签控件的字体为Kaiti_gb2312,点击“Chinese New Wei”,标签控件的字体为Chinese New Wei;单击Yes,label控件的字体将为斜体。

单击“否”取消标签控件字体的斜体化。

privatesuboption1_click()标签1。

fontname=option1。

说明文字endsubprivatesuboption2_uu单击()label1.fontname=option2.captionendsubprivatesuboption3_click()label1.fontitalic=trueendsubprivatesuboption4_click()label1.fontitalic=falseendsub3)在窗体是设置设置框text1,其中文字为:黑体、四号、居中;设置单选按钮option1和option2,标题分别为“福州大学”和“阳光学院”,编写行当的事件过程,使得程序时,单击“福州大学”或“阳光学院”,在文本框中显示“福州大学”或“阳光学院”。

(完整版)VB程序设计教程(第3版)课后实验完整答案

(完整版)VB程序设计教程(第3版)课后实验完整答案

A.1Private Sub Command1_Click()Label3 = Text1End SubA.2Private Sub Form_Load()Timer1.Interval = 0End SubPrivate Sub Command1_Click() '自动Timer1.Interval = 200End SubPrivate Sub Command2_Click() '手动Timer1.Interval = 0Call MyMoveEnd SubPrivate Sub Timer1_Timer()Call MyMoveEnd SubSub MyMove()Label1.Move Label1.Left - 50If Label1.Left < 0 Then Label1.Left = Form1.Width End SubA.3Private Sub Form_Click()Text1 = Val(Text1) + 1End SubPrivate Sub Form_Load()Text1 = 0End SubA.4Private Sub Form_Click()Caption = "单击窗体,改变图片"Picture = LoadPicture(App.Path + "\n_015.bmp")Print "欢迎使用VB"End SubPrivate Sub Form_DblClick()ClsCaption = "双击窗体,卸去图片"Picture = LoadPicture("") 'End SubPrivate Sub Form_Load()Caption = "装入窗体"Picture = LoadPicture(App.Path + "\n_016.bmp")Print "装入图"End SubPrivate Sub Form_Resize() ' 该事件的作用窗体始终与图一样大'Caption = "窗体大小不变"'Form1.Width = 260 * 16 ' 260 是Tongji-2.bmp 图的宽度,象素单位'Form1.Height = 260 * 16 + 200 ' 260 是图的高度,象素单位,200 是窗体的标题栏高度End SubA.5Sub Command1_Click()Text1.FontName = "隶书"Text1.FontSize = 25End SubSub Command2_Click()Text2.Text = Text1.SelTextText2.FontName = Text1.FontNameText2.FontSize = Text1.FontSizeEnd SubB.1Private Sub Command1_Click()Text2 = Format(5 / 9 * (Val(Text1) - 32), "0.00")End SubPrivate Sub Command2_Click()Text1 = Format(9 / 5 * Val(Text2) + 32, "0.00")End Sub或Private Sub Command1_Click()Dim f!, c! ' 声明两个变量f = Val(Text1)c = 5 / 9 * (f - 32)Text2 = Format(c, "0.00") ' 保留两位小数End SubPrivate Sub Command2_Click()Dim ff!, cc! ' 声明两个变量cc = Val(Text2)ff = 9 / 5 * cc + 32Text1 = Format(ff, "0.00") ' 保留两位小数End SubB.2Label2 = Format(Val(Text1) * Val(Text1) * 3.14, "0.00") End SubPrivate Sub Command2_Click()Label3 = Format(Val(Text1) * 3.14 * 2, "0.00")End SubPrivate Sub Text1_LostFocus()If Not IsNumeric(Text1.Text) ThenMsgBox "输入有非数字字符,请重新输入", , "警告"Text1.Text = ""Text1.SetFocusEnd IfEnd Sub或Private Sub Command1_Click()Label2 = Format(Val(Text1) * Val(Text1) * 3.14, "0.00") End SubPrivate Sub Command2_Click()Label3 = Format(Val(Text1) * 3.14 * 2, "0.00")End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)If KeyAscii = 13 ThenIf Not IsNumeric(Text1.Text) ThenText1.Text = ""End IfEnd IfEnd SubB.3Private Sub Command1_Click()n = Int(Log(2) / Log(1.008) + 1)Label1 = n & "年后人数超过26 亿"End SubB.4Private Sub Command1_Click()Dim x, dx, cdx = Text1.Textdx = UCase(x)cd = Len(x)Print "大写字母为:"; dxPrint "字符串长度为:"; cdEnd SubB.5Text1 = Int(Rnd * 900 + 100)End SubPrivate Sub Command2_Click()Dim x%, x1%, x2%, x3%x = Val(Text1)x1 = x Mod 10 ' 分离出的个位数x2 = (x Mod 100) \ 10 ' 分离出的十位数x3 = x \ 100 ' 分离出的百位数Label1 = x1 * 100 + x2 * 10 + x3End SubB.6Private Sub Form_Click()Label1 = Left(Text1, 11)Label2 = Mid(Text1, 12, 6)Label3 = Right(Text1, 5)End SubB.7Private Sub Command1_Click()PrintFor i = 1 To 5Print Tab(15 - i * 2); String(2 * i - 1, "★"); Spc(18 - 4 * (i - 1)); String(2 * i - 1, "★")Next iEnd SubPrivate Sub Command2_Click()ClsEnd Sub进一步要求:Private Sub Command1_Click()PrintFor i = 1 To 5Print Tab(15 - i * 2); String(2 * i - 1, "★"); String(10 - (2 * i - 1), "☆"); String(2 * i - 1, "★") Next iEnd SubPrivate Sub Command2_Click()ClsEnd SubB.8Private Sub Form_Click()x = Val(InputBox("输入一正实数", "计算", 0))pf = Format(x * x, "0.000")pfg = Format(Sqr(x), "0.000")lf = Format(x * x * x, "0.000")lfg = Format(x ^ (1 / 3), "0.000")Print "平方为:"; pf; Space(5); "平方根为:"; pfg; Space(5); "立方为:"; lf; Space(5); "立方根为:"; lfgEnd SubC.1Private Sub Form_Click()Dim x!, y!x = Val(InputBox("输入x 的值"))If x < 1000 Theny = xElseIf x < 2000 Theny = 0.9 * xElseIf x < 3000 Theny = 0.8 * xElsey = 0.7 * xEnd IfPrint yEnd SubC.2Private Sub Form_Click()Dim x!, y!x = Val(InputBox("输入上网时间"))If x < 10 Theny = 30ElseIf x < 50 Theny = 30 + 2.5 * (x - 10)Elsey = 30 + 2.5 * 40 + 2 * (x - 50)End IfIf y > 150 Theny = 150End IfPrint yEnd SubC.3Private Sub Command1_Click()Dim x!, y!, z!x = InputBox("input x")y = InputBox("input y")z = InputBox("input z")Print " x y z"Print " 排序前"; x; " "; y; " "; z Ifx < y Then t = x: x = y: y = tIf x < z Then t = x: x = z: z = tIf y < z Then t = y: y = z: z = tPrint " 排序后" & x & " " & y & " " & z End SubPrivate Sub Command2_Click()Dim x!, y!, z!x = InputBox("input x")y = InputBox("input y")z = InputBox("input z")Print " x y z"Print " 排序前"; x; " "; y; " "; z Ifx < y Then t = x: x = y: y = tIf y < z Thent = y: y = z: z = tIf x < y Thent = x: x = y: y = tEnd IfEnd IfPrint " 排序后" & x & " " & y & " " & z End SubC.4Dim a(3) As IntegerPrivate Sub Command1_Click()Picture1.ClsFor i = 0 To 2a(i) = Int(Rnd * 100 + 200)Picture1.Print a(i)Next iEnd SubPrivate Sub Command2_Click()Picture2.ClsDim z As IntegerFor i = 0 To 1If a(i) > a(i + 1) Thenz = a(i + 1)a(i + 1) = a(i)a(i) = zEnd IfNext iPicture2.Print a(0)Picture2.Print a(1)Picture2.Print a(2)End SubC.5Private Sub Text2_LostFocus()Dim m%, n%, y%m = Val(Text1)n = Val(Text2)If n Mod 2 <> 0 ThenMsgBox ("脚数必定为偶数")Text2 = ""Text2.SetFocusElsey = n / 2 - mIf y < 0 ThenMsgBox ("脚数必须≥2 倍的头数,请重新输入")Text2 = ""Text2.SetFocusElsex = n / 2 - mLabel2 = yLabel3 = m - yEnd IfEnd IfEnd SubC.6Private Sub Command1_Click()Dim a!, b!, c!, x1!, x2!, de!a = Text1b = Text2c = Text3de = b * b - 4 * a * ct = 2 * aIf de = 0 ThenText4 = Format(-b / t, "0.00")Text5 = Format(-b / t, "0.00")ElseIf de > 0 ThenText4 = Format((-b + Sqr(de)) / t, "0.00")Text5 = Format((-b - Sqr(de)) / t, "0.00")ElseText4 = Format(-b / t, "0.00") & "+" & Format(Sqr(Abs(de)) / t, "0.00") & "i" Text5 = Format(-b / t, "0.00") & "-" & Format(Sqr(Abs(de)) / t, "0.00") & "i" End IfEnd SubPrivate Sub Command2_Click()Text1 = ""Text2 = ""Text3 = ""Text4 = ""Text5 = ""End SubC.7Private Sub Text3_LostFocus()Select Case Trim(Text3)Case "+"Text4 = Val(Text1) + Val(Text2)Case "-"Text4 = Val(Text1) - Val(Text2)Case "*"Text4 = Val(Text1) * Val(Text2)Case "/"If Val(Text2) = 0 ThenMsgBox "分母为零,重新输入"Text2 = ""Text2.SetFocusElseText4 = Val(Text1) / Val(Text2)End IfEnd SelectEnd SubC.8Private Sub Text1_LostFocus()Select Case Trim(Text1)Case 1Text2 = "Monday"Case 2Text2 = "Tuesday"Case 3Text2 = "Wednesday"Case 4Text2 = "Thursday"Case 5Text2 = "Friday"Case 6Text2 = "Saturday"Case 7Text2 = "Sunday"Case Is > 7, Is < 1MsgBox "数字为1~7,重新输入"Text1 = ""Text1.SetFocusEnd SelectEnd Sub或者Private Sub Text1_LostFocus()If Text1 > 7 Or Text1 < 1 ThenMsgBox "数字为1~7,重新输入"Text1 = ""Text1.SetFocusElseText2 = Choose(Text1, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")End IfEnd SubD.1Private Sub Form_Click()For i = 1 To 9Print Tab(10 - i); String(2 * i - 1, Trim(Str(i)))Next iEnd SubD.2Private Sub Form_Click()For i = 1 To 10 Step 1Print Tab(i); String((20 - 2 * i), Chr(64 + i))Next iEnd SubD.3Private Sub Command1_Click()Dim s!, t!, i&s = 1t = 1For i = 1 To 100000t = t + is = s + 1 / tIf 1 / t < 0.00001 Then Exit ForNext iPrint "For 结构"; s, i; "项"End SubD.4Private Sub Command1_Click()Dim n&, pi#, i&n = InputBox("输入n 值")pi = 2For i = 1 To npi = pi * (2 * i) / (2 * i - 1) * (2 * i) / (2 * i + 1) Next iPrint "当n=" & n & "时,pi="; piEnd SubD.5Private Sub Form_Click()Dim s!, t!, i!, a%, n%a = Int(Rnd * 9 + 1)n = Int(Rnd * 6 + 5)t = 0: s = 0Print "a="; a, "n="; nFor i = 1 To nt = t * 10 + as = s + tPrint t;Next iPrintPrint "s="; sEnd SubD.6Private Sub Command1_Click()Dim s As Integers = 0For i = 1 To 9For j = 0 To 9For k = 0 To 9s = i * 100 + j * 10 + kIf s = i ^ 3 + j ^ 3 + k ^ 3 ThenPrint sEnd IfNext kNext jNext iEnd SubPrivate Sub Command3_Click()formd6.Hidemain.ShowEnd SubD.7Private Sub Command1_Click()Dim a!, x0!, x1!a = 27x0 = 2i = 0Doi = i + 1x1 = 2 * x0 / 3 + a / (3 * x0 * x0)If Abs(x1 - x0) < 0.00001 Then Exit Dox0 = x1LoopPrint x1, iEnd SubD.8Private Sub Command1_Click()s = 0x0 = 0.01For i = 1 To 30s = s + x0x0 = x0 * 2Next iPrint sEnd SubD.9Private Sub Command4_Click()Picture1.ClsPicture1.Print "x 课安排在"; "y 课安排在"; "z 课安排在"Picture1.Print ""For z = 5 To 6For x = 1 To z - 2For Y = x + 1 To z - 1X1 = Choose(Weekday(x), "周一", "周二", "周三", "周四", "周五", "周六", "周日")Y1 = Choose(Weekday(Y), "周一", "周二", "周三", "周四", "周五", "周六", "周日") z1 = Choose(Weekday(z), "周一", "周二", "周三", "周四", "周五", "周六", "周日") Picture1.Print " "; X1; " "; Y1; " "; z1Next YNext xNext zEnd SubE.1Private Sub Form_Click()Dim a(1 To 10) As IntegerFor i = 1 To 10a(i) = Int(Rnd * 71 + 30)Print a(i); " ";Next iMax = a(1)Min = a(1)Avg = a(1)For i = 2 To 10If a(i) > Max Then Max = a(i)If a(i) < Min Then Min = a(i)Avg = Avg + a(i)Next iAvg = Avg / 10PrintPrint "Max="; Max; " Min="; Min; " Avg="; AvgEnd SubE.2Private Sub Form_Click()Dim aa = Array(56, 78, 98, 88, 76, 78)For i = 0 To 5Print String(a(i) \ 5, "◆"); a(i)PrintNext iEnd SubE.3Dim a%(19)Private Sub Command1_Click()Picture1.ClsFor i = 0 To 19a(i) = Int(Rnd * 101)Picture1.Print a(i); " ";If (i + 1) Mod 4 = 0 Then Picture1.PrintNext iEnd SubPrivate Sub Command2_Click()Picture2.ClsDim s(5 To 9)For i = 0 To 19k = a(i) \ 10Select Case kCase 0 To 5s(5) = s(5) + 1Case 9 To 10 '90~100 分的人数s(9) = s(9) + 1Case 6 To 8 ' 存放其他三个分数段的下标有规律,根据K 获得s(k) = s(k) + 1End SelectNext iFor i = 5 To 9If s(i) <> 0 Then Picture2.Print "s("; i; ")的人数有"; Format(s(i), "0"); "个" Next iEnd SubE.4Private Sub Command1_Click()Picture1.ClsDim d%(1 To 10)For i3 = 1 To 10Randomized(i3) = Int(Rnd * 91 + 10)Next i3For i = 1 To 10For j = 1 To 10 - iIf d(j) < d(j + 1) Thent = d(j): d(j) = d(j + 1): d(j + 1) = tEnd IfNext jNext iFor i = 1 To 10: Picture1.Print d(i);If i Mod 5 = 0 Then Picture1.PrintNext iEnd SubE.5Dim a%(3, 3), b%(3, 3)Private Sub Form_Load()For i = 0 To 3For j = 0 To 3a(i, j) = Int(Rnd * 36 + 35)b(i, j) = Int(Rnd * 41 + 100)Next jNext iEnd SubPrivate Sub Command1_Click()Picture1.ClsFor i = 0 To 3For j = 0 To iPicture1.Print a(i, j); " ";Next jPicture1.PrintNext iEnd SubPrivate Sub Command2_Click()Picture2.ClsFor i = 0 To 3For j = i To 3Picture2.Print Tab(j * 6); b(i, j);Next jPicture2.PrintNext iEnd SubPrivate Sub Command3_Click()Picture3.Clssa = 0For i = 0 To 3sa = sa + a(i, i)Next isb = 0For i = 0 To 3sb = sb + b(i, 3 - i)Next iPicture3.Print "A 数组主对角线元素和为:"; sa Picture3.Print "B 数组副对角线元素和为:"; sb End SubE.6Private Sub Form_Click()n = InputBox("输入n 值")ReDim a6%(n + 1, n + 1)a6(0, 0) = 1: a6(1, 1) = 1: a6(1, 0) = 1For i = 2 To n + 1For j = 2 To ia6(i, j) = a6(i - 1, j - 1) + a6(i - 1, j) Picture1.Print a6(i, j); "";Next jPicture1.PrintNextEnd SubE.7Private Sub Form_Load()List1.ClearList1.AddItem "大学计算机基础"List1.AddItem "C/C++程序设计"List1.AddItem "VB 程序设计"List1.AddItem "Web 程序设计"List1.AddItem "多媒体技术与应用"List1.AddItem "数据库技术与应用"List1.AddItem "网络技术与应用"List1.AddItem "硬件技术基础"List1.AddItem "软件技术技术基础"End SubPrivate Sub List1_Click()If List2.ListCount >= 5 ThenMsgBox ("超过5 门课程,不能再选")Exit SubElseList2.AddItem List1.TextList1.RemoveItem List1.ListIndexEnd IfEnd SubE.8Sub Combo1_KeyPress(KeyAscii As Integer) Select Case KeyAsciiCase 48 To 57, 13Case ElseKeyAscii = 0End SelectIf KeyAscii = 13 ThenCombo1.AddItem Combo1.TextCombo1.Text = ""End IfEnd SubPrivate Sub Command1_Click()Dim min%, max%min = Val(Combo1.List(0))max = Val(Combo1.List(0))imin = 0imax = 0For i = 1 To Combo1.ListCount - 1If Val(Combo1.List(i)) > max Thenimax = imax = Combo1.List(i)ElseIf Val(Combo1.List(i)) < min Thenimin = imin = Combo1.List(i)End IfNext it = Combo1.List(0)Combo1.List(0) = Combo1.List(imin)Combo1.List(imin) = tt = Combo1.List(Combo1.ListCount - 1)Combo1.List(Combo1.ListCount - 1) = Combo1.List(imax) Combo1.List(imax) = tEnd SubE.9Private Sub Form_Click()For i = 0 To Screen.FontCount - 1If Asc(Left(Screen.Fonts(i), 1)) < 0 ThenPicture1.Print Screen.Fonts(i)Picture2.FontName = Screen.Fonts(i)Picture2.Print "商丘师范学院"End IfNext iEnd SubE.10Private Type clerknumber As String * 3name As String * 5salary As IntegerEnd TypeDim a(0 To 4) As clerk, n%Private Sub Command1_Click()If n >= 5 ThenMsgBox ("输入人数超过数组声明的个数")ElseWith a(n).number = Text1.name = Text2.salary = Text3Picture1.Print a(n).number, a(n).name, a(n).salary PrintEnd Withn = n + 1Text1 = ""Text2 = ""Text3 = ""End IfEnd SubPrivate Sub Command2_Click()Dim t As clerk, i%, j%For i = 0 To n - 1For j = i To n - 1If a(i).salary < a(j + 1).salary Thent = a(i): a(i) = a(j + 1): a(j + 1) = tEnd IfNext jNext iPicture1.ClsPicture1.Print "工号姓名工资" For i = 0 To n - 1Picture1.Print a(i).number, a(i).name, a(i).salaryPrintNext iEnd SubF.1Private Sub Form_Click()Dim a(1 To 10), amin, i%For i = 1 To 10a(i) = -Int(Rnd * 101 + 300)Print a(i);Next iCall s(a(), amin)PrintPrint "min="; aminEnd SubSub s(b(), min)Dim i%min = b(LBound(b))For i = LBound(b) + 1 To UBound(b)If b(i) < min Then min = b(i)Next iEnd SubF.2Private Sub Command1_Click()Dim mm%, nn%mm = Val(Text1)nn = Val(Text2)Picture1.Print mm; Tab(6); nn; Tab(12); gcd(mm, nn) End SubFunction gcd%(ByVal m%, ByVal n%)If m < n Then t = m: m = n: n = tr = m Mod nDo While (r <> 0)m = n: n = r: r = m Mod nLoopgcd = nEnd FunctionF.3Dim x!Private Sub Command1_Click()Print "调用标准函数Sin 的结果"; Sin(x)End SubPrivate Sub Command2_Click()Print "调用自定义函数MySin 的结果"; MySin(x) End SubFunction MySin(x!) As DoubleDim i%, t!, s!t = xs = ti = 1Do While Abs(t) > 0.00001t = -1 * t * x * x / ((i + 1) * (i + 2))s = s + ti = i + 2LoopMySin = sEnd FunctionPrivate Sub Command3_Click()x = InputBox("输入要计算正弦函数的角度值x") x = x * 3.14 / 180End SubF.4Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii = 13 ThenIf Not IsNumeric(Text1) ThenMsgBox "输入非数字串,重新输入"Text1.Text = ""Text1.SetFocusElseIf IsH(Text1) ThenPicture1.Print Text1; " ★"ElsePicture1.Print Text1End IfText1 = ""End IfEnd IfEnd SubFunction IsH(ss As String) As BooleanDim i%, Ls%IsH = Truess = Trim(ss)Ls = Len(ss)For i = 1 To Ls \ 2If Mid(ss, i, 1) <> Mid(ss, Ls + 1 - i, 1) ThenIsH = FalseExit FunctionEnd IfNext iEnd FunctionF.5Function prime(ByVal m As Integer) As Booleanprime = TrueDim i%For i = 2 To m - 1If (m Mod i) = 0 Then prime = False: Exit Function '注意冒号和exit 的范围Next iEnd FunctionPrivate Sub Command1_Click()n = 0For i = 6 To 100 Step 2For j = 3 To i \ 2If prime(j) ThenIf prime(i - j) ThenList1.AddItem i & " =" & j & " +" & i - jn = n + 1End IfEnd IfNext jNext iPicture1.Print " 6 和100 之间共有"; n; "对素数和"End SubF.6Dim a%()Private Sub Form_Click()Print "1000 以内的完数为:"For i = 1 To 1000If IsWs(i) ThenPrint i; "=1";For j = 1 To UBound(a)Print "+"; a(j);Next jPrintEnd IfNext iEnd SubFunction IsWs(m) As BooleanDim s%s = 0For i = 1 To m \ 2If m Mod i = 0 ThenReDim Preserve a(j)a(j) = ij = j + 1s = s + iEnd IfNext iIf m = s Then IsWs = TrueEnd FunctionF.7Private Sub DeleStr(s1 As String, ByVal s2 As String)Dim i%ls2 = Len(s2)i = InStr(s1, s2)Do While i > 0s1 = Left(s1, i - 1) + Mid(s1, i + ls2) ' 在s1 中去除s2 子串i = InStr(s1, s2)LoopEnd SubPrivate Sub Command1_Click() ' 调用DeleStr 子过程Dim ss1 As Stringss1 = Text1Call DeleStr(ss1, Text2)Text3 = ss1End SubPrivate Sub Command2_Click()End SubF.8Private Sub Command1_Click()Dim maxw$maxlen Text1 & " ", maxwText2 = maxwEnd SubSub maxlen(s$, maxw$)Dimword$ maxw = ""Do While s <> ""i = InStr(s, " ")word = Left(s, i - 1)If Len(word) > Len(maxw) Then maxw = words = Mid(s, i + 1)LoopEnd SubG.1Private Sub Command1_Click()List1.ClearList1.AddItem Combo1If Option1 Then List1.AddItem "Pentium II"If Option2 Then List1.AddItem "Pentium I"If Option3 Then List1.AddItem "Celeron"List1.AddItem Text1If Check1 Then List1.AddItem "声卡"If Check2 Then List1.AddItem "Modem"If Check3 Then List1.AddItem "网络适配器"End SubPrivate Sub Text1_LostFocus()st = UCase(Trim(Text1))le = Len(st)If Not IsNumeric(Left(st, le - 2)) Or Right(st, 2) <> "MB" Then MsgBox "有不合法字符!"Text1 = ""Text1.SetFocusEnd IfEnd SubG.2Private Sub Check1_Click()Picture1.Font.Bold = Not Picture1.Font.BoldEnd SubPrivate Sub Check2_Click()Picture1.Font.Italic = Not Picture1.Font.BoldEnd SubPrivate Sub Command1_Click()Picture1.ClsIf Option1 ThenPicture1.Print Sin(Val(Text1))ElseIf Option2 ThenPicture1.Print Exp(Val(Text1))ElseIf Option3 ThenPicture1.Print Sqr(Val(Text1))End IfEnd SubPrivate Sub return_Click(Index As Integer)Form6.ShowUnload Form2End SubG.3Private Sub HScroll1_Change()Text1 = VScroll1.ValueText2 = HScroll1.ValueText3 = HScroll2.ValueText4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00") Text5 = Format(Val(Text4) + Val(Text1), "0.00")End SubPrivate Sub HScroll2_Change()Text1 = VScroll1.ValueText2 = HScroll1.ValueText3 = HScroll2.ValueText4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00") Text5 = Format(Val(Text4) + Val(Text1), "0.00")End SubPrivate Sub VScroll1_Change()Text1 = VScroll1.ValueText2 = HScroll1.ValueText3 = HScroll2.ValueText4 = Format(Val(Text1) * (Text3 / 100) * (Text2 / 12), "0.00") Text5 = Format(Val(Text4) + Val(Text1), "0.00")End SubG.4Dim t As SinglePrivate Sub Command1_Click()t = InputBox("输入倒计时分钟数")t = t * 60ProgressBar1.Min = 0ProgressBar1.Max = tProgressBar1.Value = tEnd SubPrivate Sub Command2_Click()Timer1.Interval = 1000ProgressBar1.Visible = TrueEnd SubPrivate Sub Timer1_Timer()Dim m%, s%t = t - 1ProgressBar1.Value = tm = t \ 60s = t Mod 60Label1 = m & "分" & s & "秒"If t = 0 ThenMsgBox " 时间到!"Timer1.Interval = 0ProgressBar1.Visible = FalseEnd IfEnd SubG.5Private Sub Command1_Click()CommonDialog1.ShowColorLabel1.ForeColor = CommonDialog1.ColorEnd SubPrivate Sub Command2_Click()CommonDialog1.ShowOpeni = Shell("C:\WINDOWS\NOTEPAD.exe " + CommonDialog1.FileName, vbNormalFocus) End SubG.6rivate Sub bold_Click()Text1.FontBold = Not Text1.FontBoldbold.Checked = Not bold.CheckedEnd SubPrivate Sub del_Click()Text1 = ""End SubPrivate Sub end_Click()EndEnd SubPrivate Sub font12_Click()Text1.FontSize = 12End SubPrivate Sub font16_Click()Text1.FontSize = 16End SubPrivate Sub Form_Load()bold.Checked = Falseitalic.Checked = FalseEnd SubPrivate Sub italic_Click()Text1.FontItalic = Not Text1.FontItalicitalic.Checked = Not italic.CheckedEnd SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu menu2End SubG.7Private Sub Command1_Click()CommonDialog1.ShowColorLabel1.ForeColor = CommonDialog1.ColorEnd SubPrivate Sub Command2_Click()CommonDialog1.ShowOpeni = Shell("C:\WINDOWS\NOTEPAD.exe " + CommonDialog1.FileName, vbNormalFocus) End SubPrivate Sub return_Click(Index As Integer)Form6.ShowUnload Form5End SubG.8G.9Private Sub 结束_Click()EndEnd SubPrivate Sub 删除最大值_Click()max = List1.List(0)For i = 1 To List1.ListCount - 1If max < List1.List(i) Then max = List1.List(i): j = i Next iList1.RemoveItem jEnd SubPrivate Sub 删除最小值_Click()min = List1.List(0)For i = 1 To List1.ListCount - 1If min > List1.List(i) Then min = List1.List(i): j = i Next iList1.RemoveItem jEnd SubPrivate Sub 随机产生_Click()RandomizeFor i = 0 To 9List1.List(i) = Int(Rnd * 30 + 70)Next iEnd SubPrivate Sub 添加数据_Click()List1.AddItem Int(Rnd * 30 + 70)End SubPrivate Sub 统计_Click()Dim max%, min%, ave!, m%, n%max = List1.List(0)min = List1.List(0)ave = List1.List(0)m = 0n = 0For i = 1 To List1.ListCount - 1If max < List1.List(i) Then max = List1.List(i): m = i If min > List1.List(i) Then min = List1.List(i): n = i ave = ave + List1.List(i)Next iForm2.Text1 = List1.List(n)Form2.Text2 = List1.List(m)Form2.Text3 = Format(ave / List1.ListCount, "0.00") Form2.ShowEnd SubG.10Private Sub experimentG2_Click()Form2.ShowEnd SubPrivate Sub experimentG3_Click()Form3.ShowEnd SubPrivate Sub experimentG4_Click()Form4.ShowEnd SubPrivate Sub experimentG5_Click()Form5.ShowEnd SubPrivate Sub font_12_Click()Text1.FontSize = 12End SubPrivate Sub font_16_Click()Text1.FontSize = 16End SubPrivate Sub rnd10_Click()RandomizeFor i = 0 To 9List1.List(i) = Int(Rnd * 30 + 70)Next iEnd SubPrivate Sub stat_Click()Dim max%, min%, ave!, m%, n%max = List1.List(0)min = List1.List(0)ave = List1.List(0)m = 0n = 0For i = 1 To List1.ListCount - 1If max < List1.List(i) Then max = List1.List(i): m = i If min > List1.List(i) Then min = List1.List(i): n = i ave = ave + List1.List(i)Next istat2.Text1 = List1.List(n)stat2.Text2 = List1.List(m)stat2.Text3 = Format(ave / List1.ListCount, "0.00")stat2.ShowEnd SubPrivate Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) If Button = 2 Then PopupMenu caidan2, vbPopupMenuCenterAlignEnd SubPrivate Sub xie_Click()Text1.Font.Italic = Not Text1.Font.BoldEnd SubH.1Private Sub Command1_Click()Open "c:\score" For Output As #1Print #1, "051023", "王海涛", 66Print #1, "052498", "周文英", 88Print #1, "050992", "陈建东", 77Open "c:\score1" For Output As #2Write #2, "051023", "王海涛", 66Write #2, "052498", "周文英", 88Write #2, "050992", "陈建东", 77Closei1 = Shell("NOTEPAD.exe" + " c:\score", vbNormalNoFocus)i2 = Shell("NOTEPAD.exe" + " c:\score1", vbNormalNoFocus)End SubPrivate Sub Command2_Click()Dim no As String, name As String, s As IntegerOpen "c:\score" For Input As #1Do While Not EOF(1)Line Input #1, linedataList1.AddItem linedataLoopOpen "c:\score1" For Input As #2Do While Not EOF(2)Input #2, no, name, sList2.AddItem no & name & sLoopCloseEnd SubH.2Private Sub Command1_Click()Dim fib%(0 To 9), i%Open "c:\fb.dat" For Output As #1For i = 0 To 9If i = 0 Or i = 1 Thenfib(i) = iElsefib(i) = fib(i - 1) + fib(i - 2)End IfPrint #1, """Fib(" & i & ")""," & fib(i)Next iClose #1i = Shell("NOTEPAD.exe" + " c:\fb.dat", vbNormalNoFocus) End SubPrivate Sub Command2_Click()Dim st$, n%, sum%Open "c:\fb.dat" For Input As #1Do While Not EOF(1)Input #1, st, nsum = sum + nList1.AddItem st & "=" & nLoopClose #1List1.AddItem " 合计:" & sumList1.AddItem "平均:" & sum / 10End SubH.3H.4Private Type studtypeno As String * 4name As String * 6mark As SingleEnd TypeDim student As studtype, stud(1 To 5) As studtype, t As studtypePrivate Sub Command1_Click()Open "d:\score.dat" For Random As #1 Len = Len(student) With student.no = "0001".name = "星期一"。

第四版自动控制原理课后答案

第四版自动控制原理课后答案

4-3设单位反馈控制系统开环传递函数如下,试概略绘出相应的闭环根轨迹图(要求确定分离点坐标d ):(2) (1)()(21)K s G s s s +=+ 解:(1)系统开环传递函数为: */2(1)(1)()(0.5)(0.5)K s K s G s s s s s ++==++,*/2K K = (2)p1=0,p2=-0.5,z1=-1. n=2,m=1根轨迹有2条分支(3) n-m=1 1条渐近线,负实轴;(4)实轴上的根轨迹:(-∞,-1)(-0.5,0)(5) 分离点坐标计算如下:(0.5)0(1)s dd s s ds s =⎡⎤+=⎢⎥+⎣⎦ 220.50d d ++=,解方程得:1 1.7d =-,20.3d =-。

根轨迹如下图所示:Matlab:G=zpk([-1],[0 -0.5],1);figure;rlocus(G)4-4 已知单位反馈控制系统开环传递函数如下,试概略画出相应的闭环根轨迹图(要求算出起始角Pi θ):(2)*(20)()(1010)(1010)K s G s s s j s j +=+++-解:(1)有三个极点:10p =,21010p j =-+,31010p j =--,有一个零点 120z =-。

(2) n=3,根轨迹有3条分支,m=1,有n-m=2趋于无穷远处; 渐近线交点: 111010*********n m iji j a p z j j n m σ==----++===--∑∑ 渐近线与实轴夹角:0,(21)(21)23311,2a k k k n m k πππϕπ⎧=⎪++⎪===⎨--⎪=⎪⎩(3) 实轴上根轨迹:(-20, 0);(4) 起始角221212312(21)()()()180(101020)(1010)90180451359000p p p k p z p p p p j j θπθθ=++∠--∠--∠-=︒+∠-++-∠-+-︒=︒+︒-︒-︒=︒=-=︒根轨迹如下图所示:(Matlab:G=zpk([-20],[0 -10-10i -10+10i],1);figure;rlocus(G);) 4-5 设单位反馈控制系统的开环传递函数如下,要求:(1)确定*()(1)(10)K G s s s s =++产生纯虚根的开环增益。

大学vb 实验作业4 简单控件的使用(三)

大学vb  实验作业4 简单控件的使用(三)
4、设计一个简单的存款计算器。参考界面如下:
实验小结
实验内容
1、完成上课讲解的分离数字的例子。参考界面如下:
要求:将文本框中的数据按字符型数据进行处理,用函数实现。
2、输入一个三位数,将它逆置后输出,要求将文本框中的数据按数值型数据进行处理,用算术表达式实现(利用mod、\、*、+等运算符)。参考界面如下:
3、设计一个简单的时间显示牌,显示中秋节已过多少天。参考界面如下:
实验名称
实验四简单控件的使用(三)
实验时间第四周来自实验目的(1)掌握各类运算符和表达式的正确使用方法。
(2)掌握主要内部函数的使用。
(3)熟练利用立即窗口进行验证和测试。
(4)掌握基本语句的用法。
(5)进一步掌握编写简单顺序结构程序的方法。
实验环境
硬件:微型计算机
软件:Microsoft Visual basic 6.0

vb实验指导后题目-参考答案

vb实验指导后题目-参考答案

期末自测题(A卷)文件部分的选择题要做,其它类型的题目不用做,不作考察选择题其它部分还没有时间做一、单选题(每题1分,共25分)1、211.66.160.1 是Internet上的( )。

(A) IP地址 (B)域名 (C)密码 (D)软件编号2、在以下四个www网址中,()网址不符合www网址书写规则。

(A) (B) (C) (D) 3、下列字符中,其ASCII码值最小的一个是()。

(A) A (B) a (C) Z (D) X4、下列软件中属于浏览器的是()。

(A) Excel (B) Internet Explorer (C) QQ (D) Visual Basic5、电子邮件(E-mail)地址的正确形式是()。

(A)用户名@域名 (B)用户名#域名 (C)用户名/域名 (D)用户名.域名6、下面变量名不合法的是()。

(A) a2 (B) abcd (C) a$x (D) Cd_E7、下列表达式中,正确的Visual Basic表达式是()。

(A) 2sin45° (B) ∏*R2 (C) x÷y×5 (D) (x+1)/(x^2+1)/28、下面逻辑表达式的值为真的是()。

(A) "A"> "a" (B) "abc"> "bc"(C) "That"> "Thank" (D) 12> 12.19、下列过程定义语句说明不合法的是()。

(A) Sub f1(ByRef n() As Integer) (B) Sub f1(ByRef n As Integer)(C) Sub f1(ByVal n() As Integer) (D) Sub f1(ByVal n As Integer)10、执行下面的4个打印语句,将出错的是()。

vb实验报告答案全(2)(1)

vb实验报告答案全(2)(1)

实验一问答题:1、答:常见的有visual Basic、C++、Java、SQL。

用途特点:(参考课本4-5页)2、答:代码是一行或几行命令。

程序是针对某个任务编写的有序代码的集合。

软件是程序和文档的集合。

它们的关系是:代码 < 程序 < 软件3、答:面向对象程序设计中的每一个对象都应该能够接受数据、处理数据并将数据传达给其它对象,因此它们都可以被看作一个小型的“机器”,即对象。

它能够让人们更简单地设计并维护程序,使得程序更加便于分析、设计、理解。

4、答:参考课本第9页5、答:类——是创建对象实例的模板, 包含了创建对象的属性描述和行为特征的定义。

对象——是类的一个实例,继承了类的属性、方法。

控件——是具有图形界面的对象,它同样具有属性、方法、事件,是用来设计用户界面的重要元素。

属性——是对象的性质,用来描述和反应对象特征的参数。

事件——响应对象的动作,它发生在用户与程序交互时。

如单击控件、鼠标移动、键盘按下。

过程——指附在该对象上的程序代码,是事件的处理程序。

6、答:参考课本12页到17页。

实验二问答题:1、答通用属性有:Caption标题、Enabled可用性、Visible可见性、ForeColor前景颜色、BackColor背景颜色等。

2、答标签label是用来显示信息的,不具有输入功能。

属性有Caption、BackStyle、Alignment、BorderStyle、AutoSize。

文本框用来输入、输出数据,编辑文本。

属性有Text、 Maxlength、MultiLine、 ScrollBars、 Locked 、 PassWord 。

3、答结果为:标签里显示一个“abc”,图片框里显示3行“欢迎您”。

4、答要保存2次,分别是工程文件.vbp和窗体文件.frm。

实验一:一、选择1.D2.D3.B4.A5.B6.C7.CD8.D9.D10.编程机制是C、设计方式是B11.A12.D13.D14.C15.A二、填空1.windows2. 中断3.工具箱4.代码5.分类6.form2.show7. 出现语法错误8. 错9. 事件10. 面向对象11. 工程文件结构及各文件名12. 工程13. 设计程序代码14.双击窗体15.vb的安装文件夹16.属性17.方法18.font 19. form_load() 20.对象1.Private Sub Command1_Click()Print Sqr(8), 8 ^ (1 / 3)End Sub2.Private Sub Command1_Click()For i = 100 To 110Print Sqr(i)Next iEnd Sub3.Private Sub Form_Load()Print " 郑州大学在前进!"End Sub4. Private Sub label_click()PrintLabel.caption=”蓝天白云绿水青山”End sub实验二:一、选择1.B2.C3.B4.C5.C6.C7.B8.B9.D10.A11.C12.C13.C14.B15.A16.C17.B18.D19.C20.C二、填空1. Form_Click()2. Interval、5003..vbp .frm4.left top Height、Width twip5.picture6.load7. locked8. font Alignment9. GotFocus() LostFocus() 10. 属性窗口程序代码1.Private Sub Command1_Click()Label3 = Val(Text1) * Val(Text2)Private Sub Command2_Click()Label3 = Val(Text1) / Val(Text2)End Sub2.Private Sub Command1_Click()Picture1.Visible = TruePicture1.Picture = LoadPicture("d:\tu1.jpg") End SubPrivate Sub Command2_Click()Picture1.Visible = TruePicture1.Picture = LoadPicture("d:\tu2.jpg") End SubPrivate Sub Command3_Click()Picture1.Visible = False'也可以改为:Picture1.Picture = LoadPicture("") End Sub3.Private Sub Command1_Click()Label3 = Val(Text1) * Val(Text2) End Sub4.Private Sub Command1_Click() Text2 = Text1End SubPrivate Sub Command2_Click() Text1 = ""Text2 = ""End SubPrivate Sub Form_Load() Command1.Visible = False Command2.Visible = FalseEnd SubPrivate Sub Text1_Change() Command1.Visible = True Command2.Visible = TrueEnd Sub5.Private Sub Form_Load()Image1.Picture = LoadPicture("d:\tu3.jpg")End SubPrivate Sub Command1_Click()Timer1.Enabled = TrueEnd SubPrivate Sub Command2_Click()Timer1.Enabled = FalseEnd SubPrivate Sub Timer1_Timer()Image1.Left = Int(Rnd * (Form1.ScaleWidth - Image1.Width)) Image1.Top = Int(Rnd * (Form1.ScaleHeight - Image1.Height)) End实验三:一、选择1.A2.BC3.BC4.A5.A6.A7.BCD8.D9.B10.B 11.B二、判断题不合法的变量:3. 4. 5. 6. 8. 10. 11. 14.15. 16.三、1、abs(x+y)2. (3+x*y)^23. (-b+sqr(b^2-4*a*c))/(2*a)4.sin(30*3.14/180)+exp(2)四、1. int(rnd* 71+20) 或int(rnd* 71)+202. x<0 and y>0 or x>0 and y<0 或者x*y<03. x mod 3=0 or x mod 7=0或x/3=x\3 or x/7=x\74. x>=10 and x<205. x>="A" and x<="Z"6. left(s,3) 或mid(s,1,3)操作题:2.Private Sub Command1_Click()Dim n%n = Abs(Val(Text1))Label2 = Format(n ^ 2, "#.000") & Space(2) & Format(Sqr(n), "#.000") & Space(2) & Format(n ^ 3, "#.000") & Space(2) & Format(n ^ (1 / 3), "#.000")'上句的输出也可以用下面的print方式:' Print Format(n ^ 2, "#.000"); Spc(2); Format(Sqr(n), "#.000"); Spc(2); Format(n ^ 3, "#.000"); Spc(2); Format(n ^ (1 / 3), "#.000")End Sub3.Private Sub Command1_Click()Dim x%, y%x = Int(Rnd * 900 + 100)Print "产生的随机数:"; xy = (x Mod 10) * 100 + (x \ 10 Mod 10) * 10 + x \ 100Print "反序数:"; Format(y, "000")End SubPrivate Sub Command2_Click()Dim x%, y As Stringx = Int(Rnd * 900 + 100)Print "产生的随机数:"; xy = Trim(Str(x))Print "反序数:" & Right(y, 1) & Mid(y, 2, 1) & Left(y, 1)End Sub4.Private Sub Command1_Click()Dim m%, n%Randomizem = Val(Text1): n = Val(Text2)x = Int(Rnd * (n - m) + m)y = Int(Rnd * (n - m) + m)z = Int(Rnd * (n - m) + m)Label3 = m & "到" & n & "之间的3个随机数为: " & x & Space(3) & y & Space(3) & zEnd Sub5.Private Sub Command1_Click()Label1.Caption = DateEnd SubPrivate Sub Command2_Click()Label2.Caption = TimeEnd SubPrivate Sub Command3_Click()Label3.Caption = WeekdayName(Weekday(Date))End Sub实验四:一、选择1.C2.D3. C4.D5.A6.D7.B8.B9.A10.C二、填空1. 48.71E-01, -4.87122. 关系、逻辑、算术3. 900,100,mid(s,2,1)4.m、1,2,3 、print m & "月是第二季度"、end select三、程序结果1. X=4 y=132. 10操作题:1.方法一:Private Sub Command1_Click()x = InputBox("请输入弧度数值", "弧度转换", 0)xx = Val(x)a = x * 180 / 3.14aa = Int(a)b = (a - aa) * 60bb = Int(b)c = (b - bb) * 60cc = Format(c, "#.000")MsgBox "弧度" & x & "转换为" & aa & "度" & bb & "分" & cc & "秒" End Sub方法二:Option ExplicitPrivate Sub Command1_Click()Dim x!, a!, aa%, b!, bb%, c!, cc$x = Val(Text1)a = x * 180 / 3.14aa = Int(a)b = (a - aa) * 60bb = Int(b)c = (b - bb) * 60cc = Format(c, "#.000")Label2 = "弧度" & Text1 & "转换为" & aa & "度" & bb & "分" & cc & "秒"End Sub2.Private Sub Command1_Click()Dim x%x = Val(Text1)If x / 400 = x \ 400 Or x Mod 4 = 0 And x Mod 100 <> 0 ThenLabel2 = Text1 & "年是闰年"ElseLabel2 = Text1 & "年不是闰年"End IfEnd Sub3.Private Sub Command1_Click()Dim x%, y%, z%x = InputBox("输入第一个数")y = InputBox("输入第二个数")z = InputBox("输入第三个数")Print "输入的数是:"; x; y; zIf x > y Then t = x: x = y: y = tIf y > z Then t = y: y = z: z = tIf x > y Then t = x: x = y: y = tPrint "排序结果是:"; x; y; zEnd Sub4.方法一:Private Sub Command1_Click() x = Val(InputBox("请输入上网时间:")) If x <> -1 ThenSelect Case xCase Is < 10fei = 25Case 10 To 50fei = x * 2Case 50 To 100fei = x * 1.5fei = x * 1End SelectMsgBox "上网费用为:" & fei & "元" End IfEnd SubPrivate Sub Command2_Click()EndEnd Sub方法二:Private Sub Command1_Click() Dox = Val(InputBox("请输入上网时间:", , -1)) If x <> -1 ThenSelect Case xCase Is < 10fei = 25Case 10 To 50fei = x * 2Case 50 To 100fei = x * 1.5Case Is >= 100End Selectzongfei = zongfei & Space(3) & fei MsgBox zongfei, , "上网费用为"ElseExit DoEnd IfLoopEnd SubPrivate Sub Command2_Click()EndEnd Sub实验五一、选择B C B A C二、填空(1)for x=2 to 10s*(1-1/x)(2) 1 t>1E-6(3) j t(4) 5 11 51.Private Sub Command1_Click()For n = 1 To 100s = s + nNext nPrint sEnd SubPrivate Sub Command2_Click()n = 1Do While n <= 100s = s + nn = n + 1LoopPrint sEnd Sub2.Private Sub Command1_Click()For i = 0 To 20For j = 0 To 33If i * 5 + j * 3 + 1 / 3 * (100 - i - j) = 100 Then Print i, j, 100 - i - j Next jNext iEnd Sub3.Private Sub Command1_Click()n = 0DoIf n Mod 2 = 1 And n Mod 3 = 2 And n Mod 5 = 4 And n Mod 6 = 5 And n Mod 7 = 0 Then Exit Don = n + 1LoopPrint nEnd Sub4.'已知x,y,z分别是0~9中的一个数,求x,y,z的值,使得xxz+yzz=532. Private Sub Command1_Click()Dim x%, y%, z%, i%, j%For x = 0 To 9For y = 0 To 9For z = 0 To 9If (x * 100 + x * 10 + z) + (y * 100 + z * 10 + z) = 532 Then Print x, y, z NextNextNextEnd SubPrivate Sub Command2_Click()For x = 0 To 9For y = 0 To 9For z = 0 To 9If Val(x & x & z) + Val(y & z & z) = 532 Then Print x; y; z NextNextNextEnd Sub5.Private Sub Command1_Click()x = Val(Text1)y = x * 4n = 0Do While x <= yx = x * (1 + 0.1)n = n + 1LoopPrint x, nEnd Sub6.Private Sub Command1_Click()n = Val(InputBox("输入计算到第几项:", , 0))For i = 1 To ns = s + (-1) ^ (i + 1) * iNextPrint n, sEnd SubPrivate Sub Command2_Click()n = Val(InputBox("输入计算到第几项:", , 0))p = 1For i = 1 To ns = s + p * ip = -pNextPrint n, sEnd Sub7.'s=(0+1)+(0+1+2)+(0+1+2+3)+(0+1+2+3+4)+……(0+1+2+3+4+……n) Private Sub Command1_Click()Dim n%, s%n = Val(InputBox("输入计算到第几项:", , 0))s = 0: t = 0For i = 1 To nt = t + is = s + tNext iPrint sEnd SubPrivate Sub Command2_Click()Dim n%, s%n = Val(InputBox("输入计算到第几项:", , 0)) s = 0: t = 0For i = 1 To nt = 0For j = 1 To it = t + jNexts = s + tNextPrint sEnd Sub8.Private Sub Command1_Click()Dim s As Double, pi As Doublen = 1: s = 0Do While 1 / (2 * n - 1) >= 0.0001 '也可以是Do Until 1 / (2 * n - 1)< 0.0001s = s + (-1) ^ (n + 1) * 1 / (2 * n - 1)n = n + 1Looppi = 4 * sPrint piEnd Sub9.'5-9:输出所有的“水仙花数”.'所谓的“水仙花数”是指一个3位数,其各位数字的立方和等于该数本身。

VB实验及答案

VB实验及答案

对象名称
属性名称
Form1
Caption
Command1 Caption
Label1
Caption
Label2
Caption
Label3
Caption
Text1、Text2 Text
属性值 象限判断 判断 X= Y=
(3)编写事件代码: Private Sub Command1_Click() Dim x!, y!
Private Sub Command2_Click() Dim f!, c! c = Val(Text2.Text) f = 9 / 5 * c + 32 Text1 = Str(f)
End Sub
(4)程序的运行及保存。 4 求圆的面积及圆柱体的体积,界面如图 2-3 所示。
【步骤】 (1)设计应用程序的界面:在窗体上创建两个命令按钮 Command1 和 Command2,四个标签 Label1、Label2、Label3 和 Label4,四个文本框 Text1、Text2、Text3 和 Text4。 (2)设置对象的属性
【步骤】
( 1 ) 在 窗 体 中 创 建 一 个 标 签 Label1 、 一 个 命 令 按 钮 Command1。
(2)在属性窗口中设置对象的属性:
对象名称
属性名称
属性值
Form1
Caption
设计界面
Label1
Caption
请留意此处
Command1
Caption
单击此处
(3)在 Command1 的 Click 事件中编写程序代码:
x = Val(Text1) y = Val(Text2) If x > 0 Then

vb实验作业参考答案

vb实验作业参考答案

vb实验作业参考答案VB实验作业参考答案引言:Visual Basic(VB)是一种基于事件驱动的编程语言,广泛应用于Windows操作系统的软件开发。

在学习VB编程过程中,实验作业是非常重要的一部分,通过实践来巩固所学知识。

本文将提供一些VB实验作业的参考答案,帮助读者更好地理解和掌握VB编程。

一、VB基础知识实验作业参考答案1. 实验题目:编写一个VB程序,实现两个整数的加法运算,并将结果显示在文本框中。

参考答案:```Private Sub btnAdd_Click()Dim num1 As IntegerDim num2 As IntegerDim result As Integernum1 = CInt(txtNum1.Text)num2 = CInt(txtNum2.Text)result = num1 + num2txtResult.Text = CStr(result)End Sub```2. 实验题目:编写一个VB程序,实现输入一个数字,判断该数字是奇数还是偶数,并在消息框中显示结果。

参考答案:```Private Sub btnCheck_Click()Dim num As Integernum = CInt(txtNum.Text)If num Mod 2 = 0 ThenMsgBox "该数字是偶数"ElseMsgBox "该数字是奇数"End IfEnd Sub```二、VB控件应用实验作业参考答案1. 实验题目:编写一个VB程序,实现一个简单的计算器功能,包括加法、减法、乘法和除法操作。

参考答案:```Private Sub btnAdd_Click()Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 + num2txtResult.Text = CStr(result) End SubPrivate Sub btnSubtract_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 - num2txtResult.Text = CStr(result) End SubPrivate Sub btnMultiply_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text) num2 = CDbl(txtNum2.Text)result = num1 * num2txtResult.Text = CStr(result) End SubPrivate Sub btnDivide_Click() Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text)num2 = CDbl(txtNum2.Text)If num2 = 0 ThenMsgBox "除数不能为0"Elseresult = num1 / num2txtResult.Text = CStr(result)End IfEnd Sub```2. 实验题目:编写一个VB程序,实现一个简单的登录界面,包括用户名和密码输入框以及登录按钮。

VB实验教材习题答案 (1)

VB实验教材习题答案 (1)

第1章VB集成环境与VB概述一、选择题第2章常用控件与界面设计一、选择题第3章数据、表达式与简单程序设计一、选择题第4章选择分支与循环一、选择题二、填空题1.5 7 13 12 14 112. 3 1 5 3 4 13. right(a$,i)4. 45. 6-i6. 6 11 10 21 13 347. 5 14 138. Is else end select9. a*(2*i-1)*(-1)*x/(2*i+2) i+110. 0 n-111. 1 t*(-1)*x^2/((2*k-1)*(2*k)) Abs(t)>eps12. count1=count+1 count1>013. sum=0 sum+j st & k & “+”14. 9 x三、编程题1.Option ExplicitPrivate Sub Command1_Click()Dim s As String, i As Integer, ch As String * 1Dim a As Integer, b As Integer, c As Integers = Text1.TextFor i = 1 To Len(s)ch = Mid(s, i, 1)If (ch >= "0" And ch <= "9") Thena = a + 1ElseIf (ch >= "A" And ch <= "Z") Or (ch >= "a" And ch <= "z") Thenb = b + 1Elsec = c + 1End IfNextPrint "数字个数"; aPrint "字符个数"; bPrint "其他字符"; cEnd Sub2.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, n As IntegerDim a As Integer, b As Integer, c As IntegerFor i = 1 To 50n = Int(Rnd * 90 + 10)Print n;If i Mod 10 = 0 Then PrintIf n <= 40 Thena = a + 1ElseIf n <= 70 Thenb = b + 1Elsec = c + 1End IfNext iPrint "小于等于40的个数:"; aPrint "大于40小于等于70的个数:"; bPrint "大于70的个数:"; cEnd Sub3.Option ExplicitPrivate Sub Command1_Click()Dim x As SingleDim t As Long, n As Integerx = 1t = 1n = 1Do While 1 / t >= 0.0001x = x + 1 / tn = n + 1t = t * nLoopPrint xEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim k As Integer, sum As Integer, i As Integerk = InputBox("请输入一个正整数k", "输入框")For i = 1 To Len(CStr(k))sum = sum + Mid(k, i, 1)Next iMsgBox k & "的各位数字之和" & sumEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, iSum As LongFor i = 1 To 20iSum = iSum + i ^ 4NextPrint iSumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a As Integer, b As IntegerDim sum1 As Integer, sum2 As LongDim i As Integer, j As IntegerFor a = 2 To 3000sum1 = 0sum2 = 0For i = 1 To a \ 2If a Mod i = 0 Then sum1 = sum1 + i Next ib = sum1For j = 1 To b \ 2If b Mod j = 0 Then sum2 = sum2 + j Next jIf sum2 = a And a < b Then Print a, bNextEnd Sub7.Private Sub Command1_Click() '打印菱形Dim i As Integer, j As IntegerClsFor i = 1 To 6 '打印菱形上边6行Print Tab(20);Print Spc(6 - i);For j = 1 To 2 * (i - 1) + 1If j = 1 Or j = 2 * (i - 1) + 1 ThenPrint "*";ElsePrint " ";End IfNext jPrintNext iFor i = 5 To 1 Step -1 '打印下半部分Print Tab(20);Print Spc(6 - i);For j = 1 To 2 * (i - 1) + 1If j = 1 Or j = 2 * (i - 1) + 1 ThenPrint "*";ElsePrint " ";End IfNext jPrintNext iEnd Sub8.Option ExplicitPrivate Sub Command1_Click()Dim i As Integer, j As Integer, iSum As IntegerPrint "连续和为1250的正整数是:"For i = 1 To 500iSum = 0For j = i To 500iSum = iSum + jIf iSum >= 1250 Then Exit ForNextIf iSum = 1250 ThenPrint i; " ~"; jEnd IfNextEnd Sub9.Option ExplicitPrivate Sub Command1_Click()Dim a As Integer, b As Integer, c As IntegerFor a = 1 To 50For b = a To 50For c = b To 50If a ^ 2 + b ^ 2 = c ^ 2 ThenPrint a, b, cEnd IfNext cNext bNext aEnd Sub第5章数组一、选择题二、填空题1.Variant2.ReDim A(N)A(1)If M < A(I) Then M = A(I)3.Int(26 * Rnd) + 65Chr(t)i + 1 To 10A(i) > A(j)4.InStr(s, ",")Right(s, Len(s) - n)s5.T – 1I + 16.k = 6 - i – jmax = m7.j = 48j = j-38.i + 1i = i + 19. 23710. 3103511.numi + 1a(j)=temp三、编程题1.Option ExplicitOption Base 1Private Sub Command1_Click()Dim a(10) As IntegerDim i As Integer, max As IntegerFor i = 1 To 10a(i) = Int(90 * Rnd) + 10Print a(i);Next iPrinti = 1max = 0Do While i <= 10If a(i) Mod 3 = 0 ThenIf a(i) > max Thenmax = a(i)End IfEnd Ifi = i + 1LoopIf max = 0 ThenPrint "无要找的数"ElsePrint max & "是最大的能被3整除的数"End IfEnd Sub2.Option ExplicitOption Base 1Private Sub Command1_Click()Dim A() As IntegerDim I As Integer, m As IntegerDim K As IntegerReDim A(10)Print "原数组:";For I = 1 To 10A(I) = IPrint A(I);Next IPrintm = InputBox("????m")K = 9 + mReDim Preserve A(K)For I = 11 To KA(I) = A(I - 10)Next IFor I = 1 To 10A(I) = A(I + m - 1)Next IPrint "平移后的数组:";For I = 1 To 10Print A(I);Next IPrintEnd Sub3.Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, J As Integer, K As IntegerDim a() As Integer, n As Integer, p As Integer Dim s As String, Flag As Booleanp = 1For K = 10 To 100I = 1Do While I <= 10 And Not FlagJ = 1Do While J <= 10 And Not Flagn = I ^ 2 + J ^ 2If n = K ThenFlag = TrueReDim Preserve a(p)a(p) = Kp = p + 1s = I & Str(J) & Str(K)List1.AddItem sEnd IfJ = J + 1LoopI = I + 1LoopFlag = FalseNext KEnd Sub4.Option ExplicitPrivate Sub Command1_Click()Dim A(10) As IntegerDim B(10) As Integer, S As StringDim I As Integer, J As Integer, X As IntegerA(1) = Int(90 * Rnd) + 10S = A(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = A(J) Then Exit ForNext JIf J = I ThenA(I) = XS = S & " " & A(I)Exit DoEnd IfLoopNext IText1.Text = SB(1) = Int(90 * Rnd) + 10S = B(1)For I = 2 To 10DoX = Int(90 * Rnd) + 10For J = 1 To I - 1If X = B(J) Then Exit ForNext JIf J = I ThenB(I) = XS = S & " " & B(I)Exit DoEnd IfLoopNext IText2.Text = SS = ""For I = 1 To 10For J = 1 To 10If A(I) = B(J) ThenS = S & " " & A(I)End IfNext JNext IIf S = "" ThenText3.Text = "没有重复"ElseText3.Text = SEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub5.Option ExplicitPrivate Sub Command1_Click()Dim I As Integer, J As IntegerDim a() As Integer, K As Integer, Sum As Integer K = Val(Text1.Text)I = 1DoJ = K Mod 10ReDim Preserve a(I)a(I) = JSum = Sum + JK = K \ 10I = I + 1Loop Until K = 0Text2.Text = SumEnd Sub6.Option ExplicitPrivate Sub Command1_Click()Dim a(100, 2) As Integer, K As IntegerDim I As Integer, J As IntegerDim M As Integer, N As IntegerK = 1For I = 1 To 99For J = 1 To 99If I > J ThenM = I - JN = I + JIf Sqr(M) = Int(Sqr(M)) And Sqr(N) = Int(Sqr(N)) ThenList1.AddItem I & " " & Ja(K, 1) = Ia(K, 2) = JK = K + 1End IfEnd IfNext JNext IEnd Sub7.Option ExplicitOption Base 1Dim a(5, 5) As IntegerPrivate Sub Command1_Click() '生成数组并显示Dim i As Integer, j As IntegerPicture1.ClsPicture2.ClsPicture3.ClsRandomizeFor i = 1 To 5For j = 1 To 5a(i, j) = Int(90 * Rnd) + 10Picture1.Print a(i, j);NextPicture1.PrintNextEnd SubPrivate Sub Command2_Click()Dim i As Integer, j As Integer, max As IntegerDim b(5) As Integer, c(5) As IntegerFor i = 1 To 5max = a(i, 1)For j = 1 To 5If max < a(i, j) Thenmax = a(i, j)End Ifb(i) = maxIf max = a(i, j) Then c(i) = jNext jNext iFor i = 1 To 5Picture2.Print b(i)Picture3.Print c(i)NextEnd Sub8.Option ExplicitOption Base 1Dim a(5, 5) As IntegerPrivate Sub Command1_Click() '生成数组并显示Dim i As Integer, j As IntegerPicture1.ClsPicture2.ClsLabel3.Caption = "最大值?"Label4.Caption = "最小值?"RandomizeFor i = 1 To 5For j = 1 To 5a(i, j) = Int(90 * Rnd) + 10Picture1.Print a(i, j);NextPicture1.PrintNextEnd SubPrivate Sub Command2_Click() '交换并显示Dim i As Integer, j As Integer, t As IntegerDim maxi As Integer, mini As Integer, max As Integer, min As Integer max = 10: min = 99For i = 1 To 5For j = 1 To 5If max < a(i, j) Thenmax = a(i, j)maxi = iEnd IfIf min > a(i, j) Thenmin = a(i, j)mini = iEnd IfNextNextIf maxi <> mini ThenFor j = 1 To 5t = a(maxi, j)a(maxi, j) = a(mini, j)a(mini, j) = tNext jFor i = 1 To 5For j = 1 To 5Picture2.Print a(i, j);NextPicture2.PrintNextLabel3.Caption = "最大值" & max & "在第" & maxi & "行"Label4.Caption = "最小值" & min & "在第" & mini & "行"ElsePicture2.Print "最小值和最大值在一行"Label3.Caption = "最大值" & max & "在第" & maxi & "行"Label4.Caption = "最小值" & min & "在第" & mini & "行"End IfEnd SubPrivate Sub Form_click()Picture1.ClsPicture2.ClsLabel3.Caption = ""Label4.Caption = ""End Sub9.Option Base 1Private Sub Command1_Click()Dim i As Integer, j As Integer, n As Integer, a() As Integern = InputBox("请输入一个奇数N:")If n Mod 2 = 0 ThenMsgBox ("请重新输入奇数!")End IfReDim a(n, n)For i = 1 To nFor j = 1 To nIf i = (n + 1) / 2 And j = (n + 1) / 2 Thena(i, j) = -1ElseIf i = j Or i + j = n + 1 Thena(i, j) = 1Elsea(i, j) = 0End IfNext jFor i = 1 To nFor j = 1 To nPrint a(i, j);Next jPrintNext iEnd Sub10.Option Base 1Dim a(10) As IntegerPrivate Sub Command1_Click()Dim i As IntegerPrint "生成的数组:"RandomizeFor i = 1 To 10a(i) = Int(Rnd * 90) + 10Print a(i);NextPrintEnd SubPrivate Sub Command2_Click()Dim i As Integerx = InputBox("请输入要删除的数组元素下标:") Print "删除第" & x & "个数组元素后:"For i = x + 1 To 10a(i - 1) = a(i)NextFor i = 1 To 9Print a(i);NextEnd Sub11.Option ExplicitDim a(20) As Integer, i As IntegerPrivate Sub Command1_Click()RandomizeFor i = 1 To 20a(i) = Int(Rnd * 9) + 1Text1 = Text1 & a(i) & " "If i = 10 Then Text1 = Text1 & vbCrLf NextPrivate Sub Command2_Click()Dim j As Integer, k As Integer, b(20) As Boolean 'b(2)用来记录a(2)是否和前面的数相同,相同记为true,不再统计For i = 1 To 20k = 1If b(i) = False Then '如果b(i)=False,表示a(i)没有被统计过For j = i + 1 To 20If a(i) = a(j) Thenk = k + 1b(j) = TrueEnd IfNext jList1.AddItem a(i) & "出现次数:" & kEnd IfNextEnd SubPrivate Sub Command3_Click()Text1 = ""List1.ClearEnd Sub第6章过程一、选择题二、填空题1. 6 .2. 4 20 1013.第一行是 6 26 、第三行是-2 984.第一行10 6 、第二行 5 -55.第一行 6 5 、第三行8 76.第一行11 、第二行77.Fun(i)Fun=p8.第一行 4 、第二行2079.第一行29 、第二行28.710.J=kb( ) As Integerb( k-1) =b(k)i11.第一行gec 、第二行ge 、第四行 312.p1 = True And p2 = True 或p1 And p2= isp(i)Isp = Truem Mod i = 013.Convert(st)Len(s)"A" To "F"k = k + p * h14.isualVisualVisu15.s + an + 1x As Single, n As Integer 、p * (x + i) / ((2 * i - 1) * i * x)16.27617.Left(s, 1)Mid(s, i - 1, 1) = Mid(s, i, 1)n = 1c & CStr(n)18.chtempTrue19.val(mid(s,i,j))n as longprime=True20.A(2,2)的值是 6 ,A(3,1) 的值是8 ;A(4,2) 的值是 321.Option1(i)X22.1023.第一行7 6 ,第二行16 16 ,第三行,4524.第一行15 1 ,第二行35 3 ,第三行,50三、改错题1、Option Base 1Private Sub Command1_Click()Dim A() As Integer, I As Integer, J As Integer, Logic As Boolean Dim K As IntegerReDim A(1)A(1) = 50————————'K=1For I = 51 To 149K = 1 '位置错Logic = FalseCall Sub1(A, I, Logic)If Logic ThenK = K + 1ReDim A(K) 'ReDim Preserve A(K)A(K) = IEnd IfNext IFor I = 1 To UBound(A)Text1 = Text1 & Str$(A(I))If I Mod 5 = 0 Then Text1 = Text1 & vbCrLfNext IEnd SubPrivate Sub Sub1(A() As Integer, N As Integer, F As Boolean) Dim I As Integer, J As Integer, Ub As IntegerUb = UBound(A)For I = 1 To UbFor J = 2 To A(I)If A(I) Mod J = 0 And N Mod J = 0 ThenExit For 'Exit SubEnd IfNext JF = TrueNext IEnd Sub2、Option ExplicitOption Base 1Private Sub Command1_Click()Dim I As Integer, Flg As Boolean, S1 As Integer, S2 As IntegerDim J As Integer, K As Integer, P() As IntegerReDim P(1)P(1) = 2For I = 3 To 150 Step 2For J = 2 To Sqr(I)If I Mod J = 0 Then Exit ForNext JIf J > Sqr(I) ThenReDim Preserve P(UBound(P) + 1)P(UBound(P) + 1) = I 'P(UBound(P)) = IEnd IfNext IFor I = 80 To 125If fun(I, P, S1, S2) Then 'If fun(I, P, S1, S2)Text1 = Text1 & Str$(I) & "=" & Str(S1) & "*" & Str$(S2) & vbCrLf End IfNext IEnd SubPrivate Function fun(N As Integer, P() As Integer, S1 As Integer, S2 As Integer) As Boolean Dim I As Integer, J As IntegerFor I = 1 To UBound(P)For J = 1 To UBound(P) - 1If N = P(I) * P(J) ThenS1 = P(I)S2 = P(J)'fun = TrueExit Functionfun = True '位置错End IfNext JNext IEnd Function3、Option ExplicitPrivate Sub Get_Chess(Chess() As Integer, idx As Integer, Over As Integer) 'ByVal idx As IntegerDim Ub As Integer, Counter As Integer, K As IntegerUb = UBound(Chess)Chess(idx) = 0Counter = 1K = 0 '位置错Do Until Counter = Ub - 1Do Until K = 2idx = idx + 1If idx > Ub Then idx = 1K = K + Chess(idx)LoopChess(idx) = 0Counter = Counter + 1LoopFor Over = 1 To UbIf Chess(Over) <> 0 Then Exit ForNext OverEnd SubPrivate Sub Command1_Click()Dim Chess(32) As Integer, I As Integer, J As IntegerDim K As Integer, idx As IntegerFor I = 1 To 32For J = 1 To 32Chess(J) = 1Next JCall Get_Chess(Chess, I, K)If K = 23 ThenText1 = "从第" & Str$(I) & "号棋子开始取"Exit ForEnd IfNext IEnd Sub4、Option ExplicitPrivate Sub Form_Click()Dim S As String, Flg As BooleanDoS = InputBox("输入一个自然数")If S = "" Or Not IsNumeric(S) ThenMsgBox "请重新输入", 48 + vbOKOnlyElseExit DoEnd IfLoopjudge(S, Flg) ' Call judge(S, Flg) 或judge S, Flg If Flg ThenPrint S; "是回文数"ElsePrint S; "不是回文数"End IfEnd SubPrivate Sub judge(Ch As String, F As Boolean)Dim L As Integer, I As IntegerL = Len(Ch)F = True ' F = FalseFor I = 1 To LIf Mid$(Ch, I, 1) <> Mid$(Ch, L + 1 - I, 1) Then Exit Sub Next IF = False ' F = TrueEnd Sub四、编程题1.Private Function Prime(a As Integer) As BooleanDim i As IntegerFor i = 2 To Sqr(a)If a Mod i = 0 Then Exit FunctionNext iPrime = TrueEnd Function'方法一Private Sub Command1_Click()Dim m As Integer, i As Integerm = InputBox("请输入一个大于2 的偶数")If m <= 2 Or m Mod 2 <> 0 Then MsgBox "请输入大于2的偶数": Exit Sub If Prime(2) And Prime(m - 2) ThenPrint m & "=2 +" & m - 2ElseDoi = i + 1If Prime(2 * i + 1) And Prime(m - (2 * i + 1)) ThenPrint m & "=" & 2 * i + 1 & "+" & m - (2 * i + 1)Exit DoEnd IfLoop While 2 * i + 1 < m - (2 * i + 1)End If'方法二Private Sub Command2_Click()Dim m As Integer, i As Integer, p() As Integer, n As IntegerDim flg As Booleanm = InputBox("请输入大于2的偶数")If m <= 2 Or m Mod 2 <> 0 Then MsgBox "请输入大于2的偶数": Exit Sub ReDim p(1)p(1) = 2n = 1For i = 3 To m - 2 Step 2If Prime(i) Thenn = n + 1ReDim Preserve p(n)p(n) = iEnd IfNext iFor i = 1 To nFor j = 1 To nIf p(i) + p(j) = m ThenPrint m & "=" & p(i) & "+" & p(j)flg = TrueExit ForEnd IfNext jIf flg = True Then Exit ForNext iEnd Sub2.Private Function Fact(a As Integer) As LongIf a = 0 Or a = 1 ThenFact = 1ElseFact = a * Fact(a - 1)End IfEnd FunctionPrivate Sub Command1_Click()Dim n As Integer, m As Integer, result As Singlen = Val(Text1.Text)m = Val(Text2.Text)If n >= m Thenresult = Fact(n) / Fact(m) / Fact(n - m)Text3.Text = resultMsgBox "要求n>=m,请重新输入"Text1.Text = ""Text2.Text = ""Text1.SetFocusEnd IfEnd SubPrivate Sub Command2_Click()Text1.Text = ""Text2.Text = ""Text3.Text = ""Text1.SetFocusEnd Sub3.Private Function Istgs(n As Integer) As BooleanDim s As String, L As IntegerL = Len(CStr(n))s = CStr(n ^ 2)If Right(s, L) = n Then Istgs = TrueEnd FunctionPrivate Sub Command1_Click()Dim n As IntegerFor n = 10 To 300If Istgs(n) Then List1.AddItem n & "^2" & "=" & n ^ 2 Next nEnd SubPrivate Sub Command2_Click()EndEnd Sub4.Private Sub Command1_Click()Dim a(3, 4) As Integer, i As Integer, j As IntegerDim m As Integer, mj As IntegerRandomizeFor i = 1 To 3For j = 1 To 4a(i, j) = Int(900 * Rnd + 100)Print a(i, j);Next jPrintNext iFor i = 1 To 3m = 0Call max(a, i, m, mj)Print "第" & i & "行最大值为" & m & ",在第" & mj & "列"Next iEnd SubPrivate Sub max(a() As Integer, i As Integer, m As Integer, maxj As Integer) Dim j As Integerm = a(i, 1): maxj = 1For j = 2 To UBound(a, 2)If a(i, j) > m Thenm = a(i, j)maxj = jEnd IfNext jEnd SubPrivate Sub Command2_Click()EndEnd Sub5.Private Function Judge(n As Integer) As BooleanDim sum As Integersum = yz(n)If yz(sum) = 2 * n Then Judge = TrueEnd FunctionPrivate Function yz(n As Integer) As IntegerDim i As IntegerFor i = 1 To nIf n Mod i = 0 Then yz = yz + iNext iEnd FunctionPrivate Sub Command1_Click()Dim n As IntegerFor n = 1 To 500If Judge(n) Then Print nNext nEnd SubPrivate Sub Command2_Click()EndEnd Sub6.Private Sub Command1_Click()Dim n As Integer, j As Integer, flg As BooleanFor n = 2 To 30flg = FalseFor j = 2 To nIf pfs(j) And n Mod j = 0 Thenflg = TrueExit ForEnd IfNext jIf flg = False Then Print n;Next nEnd SubPrivate Function pfs(n As Integer) As BooleanIf Sqr(n) = Int(Sqr(n)) Then pfs = TrueEnd Function7.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd Function'判断某数是否为回文数Private Function Hws(n As Long) As BooleanDim i As Integer, s As String, L As Integers = CStr(n): L = Len(s)For i = 1 To Len(s) / 2If Mid(s, i, 1) <> Mid(s, L + 1 - i, 1) Then Exit Function Next iHws = TrueEnd FunctionPrivate Sub Command1_Click()Dim x As Long, t As Longx = InputBox("请输入一个正整数:")t = xDo While Hws(t) = FalsePrint t; "+"; fxs(t); "="; t + fxs(t)t = t + fxs(t)LoopEnd Sub8.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd FunctionPrivate Sub Command1_Click()Dim n As Long, t As LongFor n = 1 To 999t = fxs(n)If t ^ 2 = fxs(n ^ 2) And n Mod 10 <> 0 And n < t ThenList1.AddItem n & " " & tList2.AddItem n & "^2" & "=" & n ^ 2 & " " & t & "^2" & "=" & t ^ 2 End IfNext nEnd Sub9.Private Function fxs(n As Long) As LongDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext ifxs = sEnd FunctionPrivate Function Judge(n As Integer) As BooleanDim i As Integer, L As Integer, a(9) As Integer, t As IntegerIf Sqr(n) = Int(Sqr(n)) ThenL = Len(CStr(n))For i = 1 To Lt = Mid(CStr(n), i, 1)a(t) = 1Next iFor i = 0 To 9Sum = Sum + a(i)Next iIf Sum = L - 1 Then Judge = TrueEnd IfEnd FunctionPrivate Sub Command1_Click()Dim a As IntegerFor a = 300 To 2000If Judge(a) Then List1.AddItem aNext aEnd Sub10.Private Function F(n As Integer) As BooleanDim i As IntegerFor i = 1 To Len(CStr(n))If i Mod 2 = 1 And Mid(CStr(n), i, 1) Mod 2 <> 1 Then Exit FunctionIf i Mod 2 = 0 And Mid(CStr(n), i, 1) Mod 2 <> 0 Then Exit Function Next iF = TrueEnd FunctionPrivate Sub Command1_Click()Dim a(8) As Integer, i As Integer, j As IntegerRandomizeDot = Int(900 * Rnd + 100)For j = 1 To iIf t = a(j) Then Exit ForNext jIf j = i + 1 Theni = i + 1a(i) = tList1.AddItem a(i)If F(a(i)) Then List2.AddItem a(i)End IfLoop Until i = 8End SubPrivate Sub Command2_Click()List1.ClearList2.ClearEnd SubPrivate Sub CmdEnd_Click()EndEnd Sub11.Private Function prime(a As Integer) As BooleanDim i As IntegerFor i = 2 To Sqr(a)If a Mod i = 0 Then Exit FunctionNext iprime = TrueEnd FunctionPrivate Function nx(n As Integer) As IntegerDim i As Integer, s As StringFor i = 1 To Len(CStr(n))s = Mid(CStr(n), i, 1) & sNext inx = Val(s)End FunctionPrivate Sub Command1_Click()Dim n As Integer, t As IntegerFor n = 100 To 900t = nx(n)If prime(n) And prime(t) And n < t ThenList1.AddItem "(" & n & "," & t & ")"End IfNext nEnd SubPrivate Sub Command2_Click()List1.ClearEnd Sub12.Option Base 1Dim a(4, 5) As IntegerPrivate Sub Command1_Click()Dim i As Integer, j As IntegerRandomizeFor i = 1 To 4For j = 1 To 5a(i, j) = Int(Rnd * 90 + 10)Picture1.Print a(i, j);Next jPicture1.PrintNext iEnd SubPrivate Sub Fp(a() As Integer, i As Integer, maxj As Integer, Flg As Boolean) Dim max As Integer, j As Integer, t As Integermax = a(i, 1): maxj = 1For j = 2 To UBound(a, 2)If a(i, j) > max Thenmax = a(i, j)maxj = jEnd IfNext jFor t = 1 To UBound(a, 1)If a(t, maxj) > max ThenFlg = FalseExit SubEnd IfNextFlg = TrueEnd SubPrivate Sub Command2_Click()Dim i As Integer, m As Integer, mj As Integer, f As BooleanFor i = 1 To 4f = Falsem = 0: mj = 0Call Fp(a, i, mj, f)If f = True ThenText1.Text = Text1.Text & "(" & i & "," & mj & "):" & a(i, mj) & vbCrLf End IfNext iEnd SubPrivate Sub Command3_Click()Picture1.ClsText1 = ""End Sub13.Private Sub Command1_Click()Dim a(30) As Integer, i As Integer, j As IntegerDim Flag As BooleanRandomizeDot = Int(900 * Rnd + 100)For j = 1 To iIf t = a(j) Then Exit ForNext jIf j = i + 1 Theni = i + 1a(i) = tText1.Text = Text1.Text + Str(a(i))If i Mod 10 = 0 Then Text1.Text = Text1.Text + vbCrLfIf IsDown(a(i)) ThenList1.AddItem a(i)Flag = TrueEnd IfEnd IfLoop Until i = 30If Flag = False Then List1.AddItem "无降序数"End SubPrivate Function IsDown(n As Integer) As BooleanDim i As Integer, S As StringS = CStr(n)For i = 1 To Len(S) - 1If Mid(S, i, 1) < Mid(S, i + 1, 1) Then Exit FunctionNext iIsDown = TrueEnd FunctionPrivate Sub Command2_Click()List1.ClearText1 = ""End Sub14.Option ExplicitPrivate Sub CmdFind_Click()Dim i As Integer, p As String, st As String, st1 As StringFor i = 100 To 999st = ""If csum(i) = zyz(i, st) Thenp = CStr(i)st1 = Left(p, 1) & "+" & Mid(p, 2, 1) & "+" & Right(p, 1)List1.AddItem i & ": " & st1 & "=" & Left(st, Len(st) - 1)End IfNext iIf List1.ListCount = 0 ThenList1.AddItem "无满足要求的数!"End IfEnd SubPrivate Function csum(m As Integer) As Integer '求各位数字之和Dim i As Integer, p As Stringp = CStr(m)For i = 1 To Len(p)csum = csum + Val(Mid(p, i, 1))Next iEnd FunctionPrivate Function zyz(ByVal m As Integer, st As String) As Integer '求所有质因子之和Dim i As Integeri = 2DoIf m Mod i = 0 Thenzyz = zyz + ist = st & i & "+"m = m \ iElsei = i + 1End IfLoop Until m = 1End Function15.Private Sub Command1_Click()Dim k As Integer, i As Integerk = InputBox("请输入要求的数列项数:")List1.AddItem "共输出前" & k & "项数列"For i = 0 To kList1.AddItem p(i)NextEnd SubPrivate Function p(n As Integer) As IntegerIf n = 0 Or n = 1 Or n = 2 Thenp = 1Elsep = p(n - 2) + p(n - 3)End IfEnd Function第7章文件一、选择题二、填空题1.顺序文件和随机文件2. EOF3. Write和Print4.返回或设置当前的工作目录5.5116.文件的写语句7.Append8.LOF9.关闭10.Pattern11.【1】For Input 【2】#2 【3】Not EOF(2) 12.【1】”END”【2】Text1.Text 或者Text113.【1】Input 【2】ch 【3】Len(mystr) 14.【1】Number 【2】s15.【1】For Append As #1 【2】"end" 【3】ct,nt 16.【1】For Input 【2】For Output As #2 【3】Not EOF(1) 【4】Line Input #1,str1 【5】Close #1,#2 【6】Kill “C:\old.txt 17.【1】Open "C:StuData.txt" For OutPut As #1 【2】Write 18.【1】"C:\data.txt" 【2】#1, a(i) 【3】k>1 And a(k-1)>b 【4】k = k -1三、综合题1.Option Base 1Dim arr(100) As IntegerPrivate Sub Command1_Click()Dim i As IntegerOpen "c:\in18.txt" For Input As #6For i = 1 To 100Input #6, arr(i)Next iClose #6End SubPrivate Sub Command2_Click()Dim i As IntegerText1.Text = ""Open "c:\out18.txt" For Output As #8For i = 1 To 100If arr(i) Mod 2 <> 0 ThenText1.Text = Text1.Text & Str(arr(i))Write #8, arr(i)End IfNext iClose #8End Sub2.Dim str As StringDim n As LongPrivate Sub Command1_Click()CommonDialog1.Filter = "文本文件(*.txt)|*.txt|All Files(*.*)|*.*"CommonDialog1.FilterIndex = 1CommonDialog1.Action = 1Open CommonDialog1.FileName For Input As #2n = LOF(2)str = Input(n, #2)Text1.Text = strClose #2End SubPrivate Sub Command2_Click()Dim i As IntegerFor i = 1 To Len(str)If Asc(Mid(str, i, 1)) <= Asc("Z") And Asc(Mid(str, i, 1)) >= Asc("A") Then Mid(str, i, 1) = LCase(Mid(str, i, 1))ElseIf Asc(Mid(str, i, 1)) <= Asc("z") And Asc(Mid(str, i, 1)) >= Asc("a") Then Mid(str, i, 1) = UCase(Mid(str, i, 1))End IfNext iText1.Text = strEnd SubPrivate Sub Command3_Click()Open "D:\out.txt" For Output As #3Print #3, strClose #3End Sub第8章程序调试一.选择题二.改错题1.a(i)=int(Rnd*19)+1 →a(i)=int(rnd*20)+1For l=1 to a(i)+1 →for i=2 to a(i)+1If l=a(i) →if l <> a(i)K=k+1 →k=k-1。

VB程序设计学习与实验指导书答案(包含实验答案)

VB程序设计学习与实验指导书答案(包含实验答案)

VB学习与实验指导测试题参考答案第一章第二章第三章第四章第五章第六章第七章第八章第九章综合测试题一综合测试题二下篇实验教程实验2 Visual Basic 变量、函数与表达式的使用实验2-1 算术运算符的使用13178332262-2-22007-10-153 2.857143 2.85714285714286实验2-2 字符运算符的使用20071020071020172007-10-10102007-10-20出错,类型不匹配实验2-3 关系运算符的使用FalseFalseFalseFalseFalseFalseFalseTrueFalseTrue实验2-4 逻辑运算符的使用TrueTrueFalseTrueFalseFalseTrueTrueFalse5-59实验2-5 常用函数的使用3.14 3.147.389056098930651 -1 .999999998205103 4-1 1 0.70554755825 251250 0A a65 9748 688 -98 -89 -9aaa 4.5 aaa4.516 5Visua 6.0sua Basic Basic 6.0abcde EFGBasic Basic BasicVisual Basic 6.0 Basic8 8aaa dd2011-3-152011-3-15 14:35:5215320113001.731.731.7317.32E-010173%实验3 算法基础及程序控制结构实验3-1 三个数的交换A=CC=BB=TBC实验3-2 求周长和面积Const PI=3.14159262*PI*RPI*R*RPicture1.ClsFormat(L, ".00")Format(M, ".00")实验3-3 求分段函数的值x=Val(Text1.Text)y=1+Sin(x)y=Log(x)Text1.Text = ""Text2.Text = ""实验3-4 求一元二次方程的根Val(Text2.Text)Val(Text3.Text)b ^ 2 - 4 * a * c"x1=" & (-b + Sqr(delt)) / (2 * a)"x2=" & (-b - Sqr(delt)) / (2 * a)Text2.Text = ""Text3.Text = ""End实验3-5 判断素数N Mod k = 0k=NN Mod i=0k=2实验3-6 求最大公约数和最小公倍数M<NL=ML=NM Mod k = 0 And N Mod k = 0Text3.Text = kExit ForM>NL=ML=Nk Mod M = 0 And k Mod N = 0Text4.Text = kExit For实验3-7 判断升序数、降序数CStr(N)Len(S) – 1Mid(S, k, 1) >= Mid(S, k + 1, 1)k = Len(S)N = Val(Text1.Text)S = CStr(N)For k = 1 To Len(S) - 1If Mid(S, k, 1) <= Mid(S, k + 1, 1) Then Exit For Next kk = Len(S)实验3-8 判断回文数1Len(s)\2-1Mid(S, k, 1) <> Mid(S, Len(S) - k + 1, 1)k = Len(S) \ 2Mid(S, k, 1) + S1S = S1实验3-9 求级数的值Val(Text1.Text)1x ^ ns * nt / s <= 10 ^ -6Text2.Text = y实验3-10 查找数字串并求和Text1.Text = ""Text2.Text = ""List1.ClearText1.TextFalseWhile Mid(s, i, 1) >= "0" And Mid(s, i, 1) <= "9" Flag = Truek = k * 10 + Mid(s, i, 1)i = i + 1Flag = Truesum + kText2.Text = sum实验3-11 随机产生20个奇数Int((99 - 10 + 1) * Rnd) + 10a Mod 2 = 1n Mod 5 = 0Picture1.Cls实验3-12 加密解密Chr(Asc("a") + (Asc(c) - Asc("a") + 3) Mod 26)c = Chr(Asc("A") + (Asc(c) - Asc("A") + 3) Mod 26) c & s2Text2.TextLen(s2)Mid(s2, i, 1)c = Chr(Asc("a") + (Asc(c) - Asc("a") + 23) Mod 26) c = Chr(Asc("A") + (Asc(c) - Asc("A") + 23) Mod 26) c & s1s1Text1.Text = ""Text2.Text = ""实验3-13 统计单词个数Text1.TextMid(s, i, 1) <> " " And i <= Len(s)i = i + 1Mid(s, start, i - start)nText1.Text = ""List1.Clear实验4 数组的使用实验4-1 一维数组的产生及输出i = 1 To 20Int((9 - 0 + 1) * Rnd + 0)i = 1 To 20Print a(i);Printi = 1 To 20Picture1.Print a(i);If i Mod 5 = 0 Then Picture1.Printi = 1 To 20Text1.Text & Space(2) & a(i)If i Mod 5 = 0 Then Text1.Text = Text1.Text & vbCrLf v In aIf i Mod 5 = 0 Then Picture2.Print实验4-2 选手得分Int((50 - 10 + 1) * Rnd + 10) / 10i = 1 To 6sum = sum + score(i)max = score(1)min = score(1)score(i) > max Then max = score(i)score(i) < min Then min = score(i)(sum - max - min) / 4实验4-3 产生10个互不相同的整数Int((999 - 10 + 1) * Rnd + 10)t Mod 10 = 5Then Exit Forn= n + 1a(i) = tFor i = 1 To 10Print a(i);If i Mod 5 = 0 Then PrintNext i实验4-4 统计数字出现的次数IsNumeric(S) = FalseExit SubLen(S)c >= "0" And c <= "9"A(c) = A(c) + 1A(k) <> 0m = m + 1实验4-5 排序1 To 10Int((99 - 10 + 1) * Rnd + 10)Text1.Text & A(n) & " "1 To 9i + 1 To 10A(i) > A(j)T = A(j)A(j) = A(i)A(i) = T1 To 910 - iA(j) > A(j + 1)T = A(j + 1)A(j + 1) = A(j)A(j) = T1 To 10Text3.Text = Text3.Text & A(i) & " "10T = A(i)ji - 1k + 11 To 10Text4.Text = Text4.Text & A(i) & " "实验4-6 二维数组的产生及输出1 To 41 To 5Int((9 - 0 + 1) * Rnd + 0)1 To 41 To 5Print A(i, j);Print1 To 41 To 5Picture1.Print A(i, j);Picture1.Print1 To 41 To 5Text1.Text = Text1.Text & A(i, j) & " "Text1.Text = Text1.Text & vbCrLf实验4-6 查找最大和最小元素及其位置1 To 31 To 4A(i, j) = Int((99 - 10 + 1) * Rnd + 10)Picture1.Print A(i, j);Picture1.PrintA(1, 1), 1, 1A(1, 1), 1, 1A(i, j)ijA(i, j) < minA(i, j)ij"最大元素" & "A(" & maxi & "," & maxj & ")=" & max "最小元素" & "A(" & mini & "," & minj & ")=" & min实验4-7 矩阵转置Dim A() As Integer"请输入N的值"ReDim A(N, N) As IntegerFor i = 1 To NFor j = 1 To NA(i, j) = Int((99 - 10 + 1) * Rnd + 10)Text1.Text = Text1.Text & A(i, j) & " "Next jText1.Text = Text1.Text & vbCrLfNext it = A(i, j)A(i, j) = A(j, i)A(j, i) = tFor i = 1 To NFor j = 1 To NText2.Text = Text2.Text & A(i, j) & " "Next jText2.Text = Text2.Text & vbCrLfNext i实验5 过程实验5-1 孪生素数和降序素数Dim i As IntegerFor i = 2 To N - 1If N Mod i = 0 Then Exit FunctionNext iPrime = TrueDim a As IntegerDim b As Integera = N \ 10b = N Mod 10If a > b ThenDecNumber = TrueElseDecNumber = FalseEnd If10 To 100Prime(i) = True And Prime(i + 2) = TrueList2.AddItem i & "和" & i + 2DecNumber(i) = True And Prime(i) = TrueList3.AddItem i实验5-2 求多项式和k = 1a = f(x, k)S + ak = k + 1Single As SingleDim i As Integer, a As Singlea = (x + 1) / xFor i = 2 To ka = a * (x + i) / ((2 * i - 1) * x)Next ia实验5-3 进制转换Asc(C) - Asc("A") + 10Left(S, k - 1)Mid(S, k + 1)Change(Mid(S2, i, 1)) * N ^ (0 - i)T1 + T2Tran(8, S)Tran(16, S)Not (C >= "0" And C <= "7" Or C >= "A" And C <= "F" Or C = ".")实验5-4 Armstrong数i As Long, a As Long, b As LongCombo1.Text实验5-5Option Base 1Dim a(5) As IntegerDim b(5) As Integerb(i) = i * i + 1Text2.Text = Text2.Text & b(i) & " "1 To 51 To 5Gcd(a(i), b(j)) = 1Gcd = KExit For实验5-6Create(a)Output(a)Sort(a)Output(a)GetMaxMin(a, MaxData, MinData)MaxDataMinDataAverage(a)Int((99 - 10 + 1) * Rnd + 10)1 To UBound(a)a(i);a(i) > a(j)t = a(j)a(j) = a(i)a(i) = t2 To UBound(a)a(i) > MM = a(i)a(i) < NN = a(i)s = s + a(i)Average = s / UBound(a)实验6 界面设计实验6-1 用户登陆MsgBox "请登录!"PassWordInput=TxtPassW.TextUserIdInput=UserId And PassWordInput=PassWordCmdReset_Click实验6-2 学籍登记OptXb(0).Value = TrueList1.AddItem Space(4) & xh & Space(6) & xm & Space(6) & xb & Space(12) & xy & Space(16) & zy & Space(4)List1.RemoveItem List1.ListIndex实验6-3 字体格式设置Select Case OpFontCheck2.Value = 1OpColor = IndexOpFont = Index实验6-4 兴趣调查List1.AddItem ItemEndSelect Case IndexItem = Text1.Text实验6-5 调色板。

VB程序设计教程实验指导与习题解答上机答案

VB程序设计教程实验指导与习题解答上机答案

自测1程序代码Private Sub Command1_Click() = + 3End SubPrivate Sub Command2_Click() = TrueEnd SubPrivate Sub Command3_Click() = TrueEnd SubPrivate Sub Command4_Click() = 10= False= FalseEnd Sub初始窗口:单机放大之后窗口:单机下划线之后窗口:单击加粗之后窗口:单击还原之后窗口:自测2程序代码Private Sub Command1_Click() Label1 = "visual basic"Text1 = "程序设计题"End SubPrivate Sub Command2_Click() Text1 = "visual basic"Label1 = "程序设计题"End Sub初始窗口:单击显示之后窗口:单击交换之后的窗口:自测题3Private Sub Command1_Click() = *End SubPrivate Sub Command2_Click() = + 30= + 20= 1End Sub初始窗口:单击缩小两倍之后的窗口:单击向右下角移动之后的窗口:自测题4程序代码Private Sub Command1_Click() = "VB学习"Print "简单的VB程序设计" End SubPrivate Sub Command2_Click() ClsEnd SubPrivate Sub Command3_Click() EndEnd SubPrivate Sub Command3_Click() EndEnd Sub初始化窗口:单击显示之后的窗口:单击清除之后的窗口:单击退出之后的窗口:自测题5程序代码Private Sub Command1_Click() = ""= ""End SubPrivate Sub Command2_Click() = Val + ValEnd SubPrivate Sub Command3_Click() EndEnd Sub初始化窗口:单击运算之后窗口:单击清除之后的窗口:单击退出之后的窗口:自测练习1Private Sub Command1_Click() Dim a$a == Left(a, 2)End SubPrivate Sub Command2_Click() a == Right(a, 2)End SubPrivate Sub Command3_Click()= &End Sub初始窗口界面输入字符串截取前两个字符截取后两个字符字符串连接自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f%, g%, h%, i%, j%, k%, l% a = 7349e = a Mod 10 '个位d = (a \ 10) Mod 10 '十位c = (a \ 100) Mod 10 '百位b = a \ 1000 '千位i = (e + 7) Mod 10 '个位h = (d + 7) Mod 10 '十位g = (c + 7) Mod 10 '百位f = (b + 7) Mod 10 '千位j = ii = f '交换后的个位f = j '交换后的千位k = hh = g '交换后的十位g = k '交换后的百位l = f * 1000 + g * 100 + h * 10 + i= FalsePrint "加密前的4位整数是7439"; (Chr(10)); "加密后的结果是"; l End Sub初始界面单击窗体运行后的界面自测练习1Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数", "输入一个数", 8))b = Val(InputBox("输入第一个数", "输入一个数", 7))c = a + bd = a - be = a * bf = a / bf = Fix(f * 100 + / 100MsgBox "x+y=" & c & Chr(10) & "x-y=" & d & Chr(10) & "x*y=" & e & Chr(10) & "x/y=" & f, , "输出的结果"End Sub自测练习2Private Sub Form_Click()Dim a%, b%, c%, d%, e%, f!a = Val(InputBox("输入第一个数的值"))b = Val(InputBox("输入第二个数的值"))c = Val(InputBox("输入第三个数的值"))d = Val(InputBox("输入第四个数的值"))e = a + b + c + df = e / 4Print "和="; e Print "平均值="; f End Sub自测练习3Private Sub Form_Click()Dim h%, f%, x%, y%h = Val(InputBox("输入总头数")) f = Val(InputBox("输入总脚数")) x = (4 * h - f) / 2y = (f - 2 * h) / 2Print "笼中鸡的头数为"; x; "只" Print "笼中兔的头数为"; y; "只"End Sub自测练习1:Private Sub Form_Click() Dim a%, b%, c%a = Val(InputBox("请输入第一条边", "边长"))b = Val(InputBox("请输入第二条边", "边长"))c = Val(InputBox("请输入第三条边", "边长"))If a + b > c And a + c > b And b + c > a ThenIf a * a + b * b = c * c Or a * a + c * c = b * b Or b * b = c * c = a * a Then Print "直角"ElseIf a = b And b = c ThenPrint "等边三角形"ElseIf a = b Or a = c Or b = c ThenPrint "等腰三角形"ElsePrint "一般三角形"End IfElsePrint "不是三角形"End IfEnd Sub不是三角形运行结果:初始窗口:输入三条边:运行结果:直角三角形运行结果:等边三角形运行结果如下:等腰三角形:一般三角形自测练习2:Private Sub Form_Click()Dim a%, b!a = Val(InputBox("请输入"))If a Mod 2 = 0 And a Mod 3 = 0 And a Mod 5 = 0 Then b = Sqr(a)Print a, bElseEndEnd IfEnd Sub初始化窗口:输入一个数:运行结果:自测练习3Private Sub Command1_Click()Dim a%, b%a = ValSelect Case a > 0Case a < 20MsgBox "行李费为元/kg", , "行李费"Case a >= 20 And a < 50MsgBox "行李费为元/kg", , "行李费"Case a >= 50 And a < 100MsgBox "行李费为元/kg", , "行李费"Case Elseb = * 100 + (a - 100) * : MsgBox "行李费为" & b & "元", , "行李费" End SelectEnd Sub初始窗口:输入10:输入25:输入55:输入120:自测练习1Option Base 1Private Sub Form_Click() Dim i%, a(1 To 20) As Integera(1) = 1: a(2) = 1 Print a(1);Print a(2);For i = 3 To 10a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 11 To 16a(i) = a(i - 1) + a(i - 2) Print a(i);Next iPrintFor i = 17 To 20a(i) = a(i - 1) + a(i - 2) Print a(i);Next i初始界面:运行结果界面:自测练习2Option Base 1Private Sub Form_Load() = 10000= 5000End SubPrivate Sub Command1_Click()Dim i%, j%, t%, a(1 To 4, 1 To 4) As Integer For i = 1 To 4For j = 1 To 4a(i, j) = Int(Rnd * 10) + 1a(i, j);Next jNext i"对角线的元素如下"For i = 1 To 4a(i, i);Next iFor i = 1 To 4a(i, 5 - i);Next iFor i = 1 To 4t = a(1, i)a(1, i) = a(4, i)a(4, i) = tNext iFor i = 1 To 4t = a(i, 2)a(i, 2) = a(i, 3)a(i, 3) = tNext iFor i = 1 To 4 For j = 1 To 4a(i, j);Next jNext iEnd Sub运行初始界面:运运行结果界面:自测练习1Sub itostr(a As Integer) Dim b%, c$a = ValWhile a >= 1c = Chr(a Mod 10 + 48) & ca = a \ 10Wend= cEnd SubPrivate Sub Command1_Click() Dim a As Integera = ValCall itostr(a)End Sub运行初始界面:运行结果界面:验证其他数字:自测练习2:Public Function area(n) As Double Dim c#, d#, e#, s#n = Valc = (1 - 0) / nWhile e <= 1d = I * c:e = (I + 1) * cs = s + (Sin(d) + Sin(e)) * c / 2I = I + 1Wendarea = sEnd FunctionPrivate Sub Command1_Click() Dim s As DoubleCall area(n)= area(n)End Sub运行初始界面:运行结果:自测练习3Private Sub Form_click()n = Val(InputBox("请输入n的值"))x = Val(InputBox("请输入x的值"))Print "e的值="; es(n, x)End SubPrivate Function es(n, x)Dim a As Double, b As Double, c As Double, i As Integer a = 1: b = 1: c = 1For i = 1 To na = x * a:b = b * i:c = c + a / bNext ies = cEnd Function运行初始界面:输入数据:运行结果:自测练习4:For i = 1 To 4For j = i + 1 To 5If a(i) > a(j) Thent = a(i): a(i) = a(j): a(j) = tEnd IfNext jNext iFor i = 1 To 5Print a(i); " ";Next iFor i = 1 To 5If Y < a(i) Thent = a(i): a(i) = Y: Y = tEnd IfNext iFor i = 1 To 6a(6) = tNext iEnd FunctionPrivate Sub Command1_Click()Dim i#, a%(1 To 6), Y%Y = Val(InputBox("输入y的值", "输入数据")) Insertfun a%(), YPrintFor i = 1 To 6Print a(i); " "; Next iEnd Sub运行初始界面:插入数据:运行结果:蓝志鹏资源1102 实验8自测练习1:Private Sub Command1_Click()If = 1 Then= "报名参加羽毛球比赛"End IfIf = 1 Then= "报名参加乒乓球比赛"End IfIf = 1 And = 1 Then= "报名参加羽毛球,乒乓球比赛" End IfEnd Sub运行初始界面:运行结果:自测练习4:Private Sub Form_Load() = 2= 24= "黑体"= &HFF&End SubPrivate Sub Option1_Click() = "我是学生"End SubPrivate Sub Option2_Click() = "我是教师"End SubPrivate Sub Option3_Click() = "我是工人"End SubPrivate Sub Option4_Click() = "我是农民"End Sub运行初始界面:运行结果:。

vb第二章 程序设计基础 实验2

vb第二章 程序设计基础 实验2

实验作业程序设计基础2实验目的1、掌握VB的基本数据类型,变量、常量的定义格式。

2、掌握运算符和表达式的表示方法。

实验要求1、选择题,填空题、运行结果题、转换题标明题号,答案写在实验报告上。

2、程序设计题中的第2题要求把事件代码写在实验报告上。

3、程序设计题单建一个文件夹,以学号和姓名命名,压缩后上交到ftp服务器上。

实验内容一、选择题1.表达式25.28 Mod 6.99的值是[ B ]。

(A)1 (B)5 (C)4 (D)出错2.表达式5 ^ 2 Mod 25 \ 2 ^ 2的值是[ D ].(A)1 (B)0 (C)6 (D)43.表达式( 7 \ 3 + 1) * (18 \ 5 - 1)的值是[ A ]。

(A)8.67 (B)7.8 (C)6 (D)6.674.要强制显示声明变量,可在窗体模块或标准模块的声明段中加入语句[ B ]。

(A)Option Base 0 (B)Option Explicit(C)Option Base 1 (D)OptionCompare5.表达式Int(-20.9)+Int(20.9+0.5)-Fix(-17.9)的值为__C____A.-17B.16C.17D.186.如果a,b,c的值分别为3,2,-3,则下列表达式的值是______Abs(b+c)+a*Int(Rnd+3)+Asc(Chr(65+a))A.10B.68C.69D.787.设m=”morning”,下列______表达式的值是”mor”BA.Mid(m,5,3)B.Left(m,3)C.Right(m,4,3)D.Mid(m,3,1)8.如果x是一个正实数,对x的第三位小数四舍五入的表达式是__A____A.0.01*Int(100*x+0.5)B.0.01*Int(10*x+0.5)C.0.01*Int(x+0.5)D.0.01*Int(x+5)9.求一个三位正整数n的十位数的方法是___A___A.Int(n/10)- Int(n/100)*10B. Int(n/10)- Int(n/100)C.n-Int(n/100)*10D. Int(n-Int(n/100)*1010.返回删除字符串前导和尾随空格符后的字符串,用函数 A 。

VB经典例题(附答案)

VB经典例题(附答案)

实验4:编写一个程序。

要求在窗体内设置7个标签(Label)及7个文本框(TextBox),将每个标签的Text属性值设置为如下表达式。

在程序运行时,单击窗体空白区,可在对应的文本框中输出这些表达式的值。

1)8*3*6\22)7/6*3.2/2.15*(5.5+3.5)3)34\4+4.0^5/124)65\3 mod 3.4*fix(3.8)5)“abg”+”34” & “erert”6)true or not (8+3>=23)7)7>2 or 4<9实验目的:学习VB运算符的使用。

Public Class Form1Private Sub Form1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.ClickTextBox1.Text = 8 * 3 * 6 \ 2TextBox2.Text = 7 / 6 * 3.2 / 2.15 * (5.5 + 3.5)TextBox3.Text = 34 \ 4 + 4.0 ^ 5 / 12TextBox4.Text = 65 \ 3 Mod 3.4 * Fix(3.8)TextBox5.Text = """abg""" + """34""" & """erert"""TextBox6.Text = True Or Not (8 + 3)>= 23TextBox7.Text = 7 > 2 Or 4 < 9End SubEnd Class实验5:编写一个程序。

设a=2,b=3,c=4,d=5,S="ABCDEFGHI]K",求下列表达式的值:(1) 5+(a+b)2(2) cos(b)(sin(a)+1) (3) 8e3ln2(4) Left(S,4) (5) Mid(S, 3,4) (6) Instr(S, "EFG")(7) Lcase(Right(S, 5)) (8) Len(Mid((S, 3)实验目的:学习VB中函数的使用。

vb6.0实验七习题与答案

vb6.0实验七习题与答案

实验七专业_____________ 班级________年级________姓名______________学号_______________一、实验目的1、掌握数组(动态、静态数组、可变数组、数组初始化、控件数组)的定义方法。

2、正确理解和使用数组的下标,熟练掌握数组的引用、赋值输入和输出。

3、学会会一些实际问题二、实验内容1、按图设计窗体:要求,编写适当的事件过程实现①单击“生成”按钮,随机生成的20个10~100之间的整数存放数组a中并在Text1中显示②单击“显示偶数”按钮,在Text2中显示其的所有偶数③单击“显示奇数”按钮,在Text3中显示其的所有奇数D i m a(1T o20)A s I n t e g e r'因为在各个按钮过程都要用到数组a(),所以要定义为全局数组P r i v a t e S u b C o m m a n d1_C l i c k()R a n d o m i z eF o r i=1T o20a(i)=I n t(R n d*81+10)T e x t1.T e x t=T e x t1.T e x t&a(i)&""N e x t iE n d S u bP r i v a t e S u b C o m m a n d2_C l i c k()F o r i=1T o20I f i M o d2=0T h e n T e x t2.T e x t=T e x t2.T e x t&a(i)&""N e x t iE n d S u bP r i v a t e S u b C o m m a n d3_C l i c k()F o r i=1T o20I f i M o d2<>0T h e n T e x t3.T e x t=T e x t3.T e x t&a(i)&""N e x t iE n d S u b2、按图设计窗体:要求,编写适当的事件过程实现①单击“生成数组”按钮,随机生成的20个10~99之间的整数存放数组a中并在列表框List1的列表项显示②单击“求极值”按钮,在Text1中显示其中的最大数,在Text2中显示其中最小数D i m a(20)A s I n t e g e rP r i v a t e S u b C o m m a n d1_C l i c k()R a n d o m i z eF o r i=1T o20a(i)=I n t(R n d*90+10)L i s t1.A d d I t e m a(i)N e x t iE n d S u bP r i v a t e S u b C o m m a n d2_C l i c k()D i m m a x%,m i n%m a x=a(1):m i n=a(1)F o r i=1T o20I f m a x<a(i)T h e n m a x=a(i)I f m i n>a(i)T h e n m i n=a(i)N e x t iT e x t1.T e x t=m a x:T e x t2.T e x t=m i nE n d S u b3、要求,编写适当的事件过程实现①单击“生成”按钮,随机生成的30个ASCII值为65~122之间的字符存放数组s中并显示在Text1②单击“分类”按钮,在Text2中显示其中的大写字母,在Text3中显示其中小写字母D i m a(1T o20)A s S t r i n gP r i v a t e S u b C o m m a n d1_C l i c k()T e x t1.T e x t=""R a n d o m i z eF o r i=1T o20a(i)=C h r(I n t(R n d*58+65))T e x t1.T e x t=T e x t1.T e x t&a(i)&""N e x t iE n d S u bP r i v a t e S u b C o m m a n d2_C l i c k()T e x t2.T e x t="":T e x t3.T e x t=""F o r i=1T o20I f A s c(a(i))>=65A n d A s c(a(i))<=90T h e n T e x t2.T e x t=T e x t2.T e x t&a(i)&""I f A s c(a(i))>=97A n d A s c(a(i))<=122T h e n T e x t3.T e x t=T e x t3.T e x t&a(i)&""N e x t iE n d S u b4、按图设计窗体:要求,编写适当的事件过程实现①单击“生成”按钮,随机生成的30个值为10~99之间的整数存放数组a中并显示在Text1②单击“处理”,选出其中能被3整除但不能被6整除的数,显示在Text2中,并统计其个数和平均值(保留小数以下一位)显示在Text3和Text4中③单击“退出”,结束运行D i m a(1T o30)A s I n t e g e rP r i v a t e S u b C o m m a n d1_C l i c k()T e x t1.T e x t=""R a n d o m i z eF o r i=1T o30a(i)=I n t(R n d*90+10)T e x t1.T e x t=T e x t1.T e x t&a(i)&""N e x t iE n d S u bP r i v a t e S u b C o m m a n d2_C l i c k()D i m s u m A s I n t e g e rT e x t2.T e x t=""F o r i=1T o30I f a(i)M o d3=0A n d a(i)M o d6<>0T h e nT e x t2.T e x t=T e x t2.T e x t&a(i)&""k=k+1'统计符合条件的个数ks u m=s u m+a(i)'统计符合条件的数的和E n d I fN e x t iT e x t3.T e x t=kT e x t4.T e x t=s u m/kE n d S u bP r i v a t e S u b C o m m a n d3_C l i c k()E n dE n d S u b5、按图设计窗体:要求,十个文本框为控件数组,编写适当的事件过程实现①单击“评委给歌手打分”,随机生成10个9.0~9.9依次显示在文本框控件数组中②单击“评分”,把十个评委中去掉一个最高和一个最低分后,平均分显示的Text2中D i m a(9)A s S i n g l eP r i v a t e S u b C o m m a n d1_C l i c k()R a n d o m i z eF o r i=0T o9T e x t1(i)=F o r m a t(R n d+9,"0.0")N e x t iE n d S u bP r i v a t e S u b C o m m a n d2_C l i c k()D i m m a x!,m i n!,s u m!m a x=T e x t1(0):m i n=T e x t1(0)F o r i=0T o9s u m=s u m+T e x t1(i)I f m a x<T e x t1(i)T h e n m a x=T e x t1(i)I f m i n>T e x t1(i)T h e n m i n=T e x t1(i)N e x t iT e x t2.T e x t=(s u m-m a x-m i n)/8E n d S u b6、打开工程文件7_6.vbp,其窗体中有一个名为Text1的文本框数组,下标从0开始,程序运行时,单击“产生随机数”就会产生10 个3位的整数并放入到Text1数组中;单击“重排数据”,将把其中的奇数移到前,偶数移到后面。

vb程序设计教程课后答案与实验题答案(第三版)

vb程序设计教程课后答案与实验题答案(第三版)

习题一答案在教材上找。

习题二1.对象指现实世界中的实体。

每个对象有自己的特征(属性)、行为(方法)和发生在该对象上的活动(事件)。

类是对同一种对象的集合与抽象。

类包含所创建对象的属性数据,以及对这些数据进行操作的方法。

封装和隐藏是类的重要特性,将数据结构和对数据的操作封装在一起,类的内部实现细节对用户来说是透明的。

VB工具箱中的可视图标是VB系统设计好的标准控件类。

通过将控件类实例化,得到真正的控件对象。

在窗体上画一个控件,就将类转换为对象,创建了一个控件对象。

VB中常用的对象是控件。

此外,VB还提供了系统对象。

窗体是个特例,它既是类也是对象。

当向一个工程添加一个新窗体时,实质就由窗体类创建了一个窗体对象。

对象的三要素:属性、事件和方法。

属性:存放对象的数据(决定对象外观及功能)对象属性的设置方法:在设计阶段,利用属性窗口直接设置属性值;程序运行时,通过语句[对象名.]属性名=属性值。

若属性只能在设计阶段设置,在程序运行阶段不可改变,称为只读属性。

事件:发生在对象上的事情。

同一事件,对不同的对象,会引发不同的反应。

事件过程:应用程序处理事件的步骤。

应用程序设计的主要工作就是为对象编写事件过程的代码。

方法:面向对象的程序设计语言,为程序设计人员提供了一种特殊的过程和函数。

方法是面向对象的,调用时一定要指明对象。

2.窗体和控件具有的基本属性“(1) Name属性:名称所创建的对象名称,在程序中引用。

(2) Caption属性:标题该属性决定了对象上显示的内容。

(3) Height、Width、Top和Left属性决定对象的大小和位置。

(4) Enabled 属性:对象是否允许操作(5) Visible 属性:对象是否可见(6) Font 属性组:文本的外观FontName字体、FontSize大小FontBold粗体、FontItalic斜体FontStrikethru 删除线FontUnderline 下划线(7) ForeColor属性:前景颜色(8) BackColor属性:背景颜色(9) MousePointer属性:鼠标指针类型, 设置值范围0~15,由系统指定。

VB实验作业参考答案

VB实验作业参考答案
2.编程题:
1)单击窗体后在窗体上显示“您好!×××同学”。
要求:(1)程序中用自己的姓名替代“×××”。(2)文字以红色、仿宋体、加粗、三号字显示,并在字下加横线。(3)建立一个命令按钮,结束时单击此按钮退出。
答案参照课本P27的例1-6。
2)设计一窗体,窗体上有1个文本框(Text1)、1个命令按钮(Command1),运行时文本框中显示“Visual Basic程序设计”,命令按钮标题为“结束”,文本框及命令按钮能随窗体大小的调整而自动调整大小及位置,其中调整文本框使其:Left=0,Top=0,宽度和高度都为窗体的一半;命令按钮:始终位于窗体右下角位置。请编写:
Command1.Top = Form1.ScaleHeight - Command1.Height
End Sub
Private Sub Form_Resize()
Call Form_Load
End Sub
Private Sub Command1_Click()
End
Dim r1 As Single, r2 As Single, Rp As Single, Rs As Single
r1 = InputBox("请输入第一个电阻值:")
r2 = InputBox("请输入第二个电阻值:")
Rp = (r1 * r2) / (r1 + r2)
Rs = r1 + r2
程序代码:
Private Sub Command1_Click()
Dim sg As String, tz As String, bz As String
sg = Text1.Text
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

vb课后习题参考答案第一章习题参考答案一、填空题1、对象事件驱动2、编译运行模式解释运行模式二、简答题1、简述VB的特点。

答:①可视化的程序设计方法②面向对象的程序设计思想③事件驱动的编程机制④结构化的程序设计语言⑤高度的可扩充性⑥强大的数据库访问能力⑦支持动态数据交换⑧支持对象链接与嵌入2、简述VB的安装过程。

答:①向光驱中放入VB安装光盘;②计算机将运行自动安装程序,若没有,执行VB目录下的Setup.exe;③根据提示逐一回答问题,并点击“下一步”;④点击“完成”;2、如何启动VB。

答:三种方法:①单击“开始”按钮,选择“程序”菜单下的“Microsoft Visual Basic 6.0 中文版”菜单项并单击;②双击桌面上的VB6.0快捷图标;③在“开始”菜单的运行对话框中输入命令来启动VB;第二章习题参考答案一、选择题:1-10、DCACD BCBBC11-20、BADCA BBAAB21-29、AACBB CDDC二、填空题1.窗体、控件2.属性值3.属性窗口设置、代码窗口设置4.事件过程5.对象名、下划线、事件名6.Form_Click第三章习题参考答案一、选择题1、B2、C3、A4、B5、A6、C7、C二、填空题1、11字符型、字节型、整型、长整型、单精度型、双精度型、货币型、逻辑型、日期型、对象型、变体型。

String、Byte、Integer、Long、Single、Double、Currency、Boolean、Date、Object、Variant2、双引号(英文)、#3、ASCII、1、24、public、通用、Dim、Static、Private、Static5、&、+、+6、Now()、Time、日期型三、解答题1、(3)2、(1 )3、(1)常量(关键字)(2)字符型常量(3)逻辑型常量(4)变量(5)字符串常量(6)变量(7)字符串常量(8)变量(9)日期型常量(题目应是#11/16/1999#)(10)数值型常量(单精度)4、315、(1)(1+y/x)/(1-y/x) (2)x^3+3*x*y/(2-y)(3)sqr(abs(a*b-c^3)) (4)sqr(s*(s-a)*(s-b)*(s-c))6、7、(1)13 (2)17(3)#1999-11-12# (4)zxy123ABC8、(1)19 (2)8.16666666666667(3)5.25 (4)29、(1)-4 (2)2 . 82842712474619 (3)-3 (4)49 (5)”hello”(6)He7)16 (8)“-459.65”(9)510、(4)11、(2)12、(4)第四章习题参考答案一、选择题:1—7: DAABBCD二、填空题1.500.00%2.将字号扩大为原来的二倍3. B三、编程题(略)1.文本框、标签和命令按钮的名称分别为text1、label1、command1Private Sub Command1_Click()Label1.caption=text1.textCommand1.caption=text1.textEnd Sub2.单价、数量、折扣和应付款对应的文本框的名称分别为:text1、text2、text3和text4,计算和清除命令按钮的名称分别为:command1和command2。

Private Sub Command1_Click()Text4=val(text1)*val(text2)*val(text3)End SubPrivate Sub Command2_Click()Text1=”” : Text2=””: Text3=””:Text4=””End Sub3.文本框、“转大写”命令按钮和“转大写”命令按钮的名称分别为text1、command1和command2Private Sub Command1_Click()Text1=Ucase(text1)End SubPrivate Sub Command2_Click()Text1=Lcase(text1)End Sub第五章习题参考答案一、选择题:1—11: CAABBABBBCC二、填空题1.(3) 2.1 2 3 3.2 2 3 4.160 5.96.78 7.x<=5 (或x<6 或x<7) 8.B9.iSum iSum+j 1250三、编程题1.Private Sub Form_Click()Dim i%,s%S=0For i = 101 To 200 step 2If i mod 3<>0 thenPrint iS=s+iEnd IfNext iPrint sEnd Sub2.Private Sub Form_Click()Dim n%,s&S=1:n=0While s<=400000N=n+1S=s*nWendIf s>400000 Then n=n-1Print nEnd Sub3.Private Sub Form_Click()Dim imin%,imax%,i%,R%Imin=100:imax=20For i=1 to 20R=Int(Rnd*51+50)If R>imax Then imax=RIf R<imin Then imin=RNext iPrint imax,iminEnd Sub第六章习题参考答案一、选择题:1—13: ABAACCBADADBB二、填空题1.Interval Label1.caption=time2.List1.ListCount InterVal Hscroll1.Max 三、编程题第七章习题参考答案一、选择题:1—12: DCADDBBDACCB二、填空题1.42 2.Index 3.414.A(i)=a(11-i) a(11-i)=Tmp5.1 6.X Mod 7=0 7.2108.0 To 8 i+1 To 99.3 K 110.1001 Max Max<arrN(i) (或者arrN(i)>Max) 三、编程题1.Private Sub C1_click()If OP1(0).Value=True Then P1.Print “选择了椭圆形”If OP1(1).Value=True The n P1.Print “选择了圆形”If OP1(2).Value=True Then P1.Print “选择了正方形”End Sub2.统计按钮名为Command1,输入框名为text1,输出框为text2(MultLine 设为True)Private Sub Command1_click()Dim X(1 to 26) AS Integer,I As IntegerDim S As String,L As IntegerL=Len(Text1.text)For i=1 To LS=Mid(Text1,I,1)S=Ucase(S)If S >= "A" And S <= "Z" Thenx(Asc(S) - 65 + 1) = x(Asc(S) - 65 + 1) + 1End IfNext iFor i=1 To LText2=text2 & Chr(64+i) & “=” & x(i) & ““If I Mod 7=0 then Text2=text2 & chr(13)+chr(10)Next iEnd Sub3.设学生成绩的平均分已在Mark(1)至Mark(10)中,学生姓名在name(1)至name(10)中。

Private Sub Command1_click()Dim I%,J%,T%,S$For i=1 To 9For j=i+1 To 10If Mark(i)<Mark(j) ThenT=Mark(i):Mark(i)=Mark(j):Mark(j)=TS=name(i):name(i)=name(j):name(j)=SNext JNext iFor i=1 To 10Print Name(i),Mark(i)Next iEnd Sub第八章习题参考答案一、选择题:1—10: DCBBDDDBCB二、填空题1.a Ubound(b) n=n-12.Key=a(i) Index=-1三、编程题1.子过程Sub P1(x As Single, esp As Single)Dim p!,S!,F!,i%P=1:S=1:i=1:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)<espPrint sEnd Sub子函数Function P2(x As Single, esp As Single) As singleDim p!,S!,F!,i%P=1:S=1:i=1:F=xDoP=P*iS=S+F/pLoop Until Abs(F/p)<espP2=sEnd Function2.Swap2能实现数据交换。

因为地址传递方式形参和实参有共同的地址,那么形参的变化,会引起实参的变化(即形参将值回传给实参),故可实现两数交换。

3.Function LF( ByVal x As Single) As SingleLF=X*X*XEnd FunctionSub Command1_Click()Dim I%For i=1 To 20Print I, LF(i)Next iEnd Sub4.Function WeekDay(ByVal x As Integer) As StringSelect Case xCase 0WeekDay=”星期日”Case 1WeekDay=”星期一”Case 2WeekDay=”星期二”Case 3WeekDay=”星期三”Case 4WeekDay=”星期四”Case 5WeekDay=”星期五”Case 6WeekDay=”星期六”End SelectEnd Function5.Function Rand30(ByVal X As`Integer) As Integer RandomizeRand30=Int(Rnd*X+1)End FunctionSub Command1_Click()For i=1 To 30Print Rand30(100)Next iEnd Sub6.Function F1(ByVal X As`Single, ByVal Y As`Single) As Integer If X>Y ThenF1=XElseF1=YEnd IfEnd Function7.Function GDBH(ByVal x%) As StringDim i%For i = 3 To x - 3 Step 2If isp(i) And isp(x - i) ThenGDBH = i & "+" & (x - i) & "=" & xExit FunctionEnd IfNextEnd FunctionFunction isp(y%) As Booleani = 2: k = Int(y / 2)Do While i <= kIf y Mod i = 0 Then Exit Doi = i + 1LoopIf i > k Thenisp = TrueElseisp = FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i = 6 To 40 Step 2Print GDBH(i)Next iEnd Sub8.Function isQO(ByVal x As Integer) As String If x Mod 2 =0 ThenisQO=”偶数”ElseisQO=”奇数”End IfEnd Function9.Function F1(ByVal x As Integer) As Boolean If x Mod 17 =0 and x Mod 37 =0ThenF1=TrueElseF1=FalseEnd IfEnd FunctionPrivate Sub Form_Click()For i = 1000 To 2000If F1(i) ThenPrint iEnd ifNext iEnd Sub10.Sub F1(a() As Integer, ByVal L1 As Integer, ByVal L2 As Integer)Dim L%, U%, x%, i%L = LBound(a): U = UBound(a)If L1 > U Or L2 > U ThenPrint "位置输入错误!"Exit SubEnd IfIf L1 > L2 Thenx = a(L1)For i = L1 To L2 + 1 Step -1a(i) = a(i - 1)Next ia(L2) = xElseIf L2 > L1 Thenx = a(L1)For i = L1 To L2 - 1a(i) = a(i + 1)Next ia(L2) = xEnd IfEnd Sub第九章习题参考答案一、选择题:1—3: ADD二、填空题1.下拉式弹出式2.PopupMenu3.Visible Enabled 4.-三、编程题(略)第十章习题参考答案一、选择题:1—3: DBD二、填空题1.Load 2.窗体模块标准模块类模块3.DoEvents三、编程题1.(略)2.Function F(ByVal x&,ByVal Y&) As IntegerDim t&,r&If x<y Then t=x:x=y:y=tR= x Mod yDo While R<>0X=yY=rR= x Mod yLoopF=yEnd FunctionPrivate Sub Command1_Click()Text3 = Val(Text1) / F(Val(Text1), Val(Text2))Text4 = Val(Text2) / F(Val(Text1), Val(Text2))End Sub第十一章习题参考答案一、选择题:1—3: DDA二、填空题6 2.AB A三、编程题1.Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)Text1 = x: Text2 = YEnd Sub2.Dim startX, StartY As Integer …在通用中申明Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)If Button = 2 ThenstartX = x: StartY = YEnd IfEnd SubPrivate Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)If Button = 1 ThenPSet (x, Y), RGB(0, 0, 0)End IfEnd SubPrivate Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, Y As Single)If Button = 2 ThenDim r%, c%Dim EndX, EndY As IntegerEndX = x: EndY = Yc = RGB(0, 0, 0)r = Sqr((EndX - startX) ^ 2 + (EndY - StartY) ^ 2)Form1.Circle (startX, StartY), r, cEnd IfEnd Sub第十二章习题参考答案一、选择题:1—8: BBABBCCA9.I H F C L G E B A J K D二、填空题1.Drive 2.Path Print Dir1.Path3.Path 4.Hidden system5.顺序文件随机文件二进制文件6.FreeFile7.Close #18.Open “c:\samples\readme.txt” For Output AS #39.Open “c:\autoexec.bat” For Intput AS #410.Open “c:\samples\xscj.dat” For Random AS #3 Len=54 11.Open “smtext1.txt” For Binary AS #112.For Input Eof(1)13.KeyAscii END(或者End end eNd enD) Text1.text三、编程题1.…在标准模块中定义Type stuID As String * 12name As String * 8z1 As String * 12z2 As String * 12sex As String * 2jc As Booleanscool As String * 20sx As Integeryw As Integeryy As Integerwl As Integerzf As IntegerDim p As stu …在通用中定义Private Sub CmdAppend_Click()p.ID = Text1 = Text2p.z1 = Text3p.z2 = Text4p.scool = Text5If Option1.Value = True Then p.sex = "男"If Option2.Value = True Then p.sex = "女"If Check1.Value = 1 Then p.jc = True Else p.jc = Falsep.sx = Val(Text6) : p.yw = Val(Text7)p.yy = Val(Text8) : p.wl = Val(Text9)p.zf = Val(Text10)Put #1, , pEnd SubPrivate Sub CmdDisplay_Click()Get #1, Val(txtRecord_No), pText1 = p.ID : Text2 = Text3 = p.z1 : Text4 = p.z2Text5 = p.scoolIf p.sex = "男" Then Option1.Value = True Else Option2.Value = True If p.jc = True Then Check1.Value = 1 Else Check1.Value = 0Text6 = p.sx : Text7 = p.ywText8 = p.yy : Text9 = p.wlText10 = p.zfEnd SubPrivate Sub Command3_Click()Text1 = "" : Text2 = ""Text3 = "" : Text4 = ""Text5 = "" : Text6 = ""Text7 = "" : Text8 = ""Text9 = "" : Text10 = ""End SubPrivate Sub Form_Load()Open App.Path & "\stu.dat" For Random As #1 Len = Len(p)End SubPrivate Sub Form_Unload(Cancel As Integer)Close #1Private Sub Text10_GotFocus()Text10 = Val(Text6) + Val(Text7) + Val(Text8) + Val(Text9) End Sub2.3.4.Private Type booksid As String * 10name As String * 8sex As String * 2math As Integereng As Integerele As IntegerEnd Type…成绩输入Private Sub Command1_Click()Dim b As booksOpen "c:\stu.dat" For Append As #1b.id = InputBox("请输入学号") = InputBox("请输入姓名")b.sex = InputBox("请输入性别")b.math = InputBox("请输入数学成绩")b.eng = InputBox("请输入英语成绩")b.ele = InputBox("请输入电子成绩")Write #1, b.id, , b.sex, b.math, b.eng, b.eleClose #1End Sub…不及格人员输出Private Sub Command2_Click()Dim a, b, cOpen "c:\stuo.dat" For Input As #1While Not EOF(1)Input #1, a, b, cIf c = "数学" Then List1.AddItem bIf c = "英语" Then List2.AddItem bIf c = "电子" Then List3.AddItem bWendClose #1End Sub…将不及格人选出存入stuo.datPrivate Sub Command3_Click()Dim a, b, c, d, e, fDim k%Open "c:\stu.dat" For Input As #1 Open "c:\stuo.dat" For Output As #2 While Not EOF(1)Input #1, a, b, c, d, e, fIf d < 60 Then Write #2, a, b, "数学" If e < 60 Then Write #2, a, b, "英语" If f < 60 Then Write #2, a, b, "电子" WendClose #1Close #2End Sub。

相关文档
最新文档