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

合集下载

vb程序设计实验教程习题答案(精品).doc

vb程序设计实验教程习题答案(精品).doc

实验教材实验-1:单项选择题:AACCC DCBBC2:填空题:(1).bas(2).vbp, ,frm(3)视图工具箱(4)对象(5)属性对象事件方法(6)Windows(7)事件行为(8)窗体模块实验二1:单项选择题:ADCCA BAB DC BBCBC BAB DC 2:基本运算题:(1)27(2) 3.33333333333333(3) 3(4)0(5)-3(6) 2(7)ab33(8)类型不匹配(9) 3(10)AB3实验三1:单项选择题:BCBDC2:填空题:(1)300(2)321456(3)Shanghai(4) 6 7 6(5)True(6)2+3=5(7)”area=” 12.56(8)VISUAL实验四1:单项选择题:CAACA2:填空题:(1)7(2) 3 5(3)输入的数小于0(4) 3(5)False(6)2010 is not leap year.试验五1:单项选择题:ACCAB ACCCB2:填空题:(1)9(2)@n ② 1(3)®Len(a)②n\2 ③Mid(a,(n-i+l),l)④Mid(a,(n-i+l),l)(4)@n Mod 3=1 And n Mod 5=1 And n Mod 7=1 ©While CountN<5(5)Mid(cl,l,i)实验六1:单项选择题:DB DCD ACADD2:填空题:(1)30(2) 3(3)①a(2)②a(3)③a(l)(4)①1 ②2 ③a(i)=a(6-i)(5)①i+j ②0 ③a(i,j)(6)©Print ②k ③a(k,j)=a(j,k)④a(k,j);实验七1:单项选择题:ABBACB2:填空题:(1)15(2)①Preserve a(n+l) ②a(i+l)=a(i) ③Exit For ④a(i+l)=m⑤0 to n+1 (或LBound(a) to UBound(a))(3)d,c,b,a实验八1:单项选择题:CADBD CACB2:填空题:(1)a=9 b=4(2)362516941(2)①a (2)UBound(b)③实验九1:单项选择题:DDDBB CAB CAD2:填空题:(1)①p=p*n ②Fac(n) ③ 10人-6(2)①Max=x ②Max=y ③p ④Max(x,a(n))(3)①number(i) ②n as integer (3)number=number+7* 10A(i-1)实验十1:单项选择题:DC BAB C实验十一1:单项选择题:DADBBB BDD实验十二1:单项选择题:CBCBC CBDAD DBC2:填空题:(1)①Label 1. FondName=Combo 1 .TextCombo2.Text ③ Label 1 .FontBold=True ④ Label 1 .FondItalic=True⑤ Label 1 .FondSize=Combo3.Text(2)①Listl.Text ©List 1 .RemoveitemListl.ListindexListl .Additem List2.Text ④ List2.RemoveItem实验十三实验十四1:单项选择题:DACDAABC2:填空题:(1)ShowFont(2)打开另存为颜色字体打印机帮助(3)工程部件Microsoft Common Dialog Control 6.0(4 ) ShowColor Action=3实验十五1:单项选择题:CB BCB B BAD2:填空题:(1)内建菜单弹出菜单(2)Click(3)工具(4)代码窗口Click(5)相同不同实验十六1:单项选择题:B CAAC ACDDB DCC2:填空题:(1)①Open "c:\studl.txt” For Output As #1Textl.Text=”End”White #l,Textl.Text(2)①For Input ②For Output As #2 ③Not Eof(l)Input #l,strl ⑤ Close #1,#2 ⑥Kill(“c:\old.txt”(3)①For Output ②For Input As #2。

VB第1章习题与答案

VB第1章习题与答案

第1章习题与答案一、选择题1. 在VB集成环境创建VB应用程序时,除了工具箱窗口、窗体窗口、属性窗口外,必不可少的窗口是:(A)窗体布局窗口(B)立即窗口(C)代码窗口(D)监视窗口2. 保存新建的工程时,默认的路径是:(A)My Documents (B)VB98 (C)\ (D)Windows3. 将调试通过的工程经“文件”菜单的“生成.exe文件”编译成.exe后,将该可执行文件转到其他机器上不能运行的主要原因是:(A)运行的机器上无VB系统所需的动态连接库(B)缺少.frm窗体文件(C)该可执行文件有病毒(D)以上原因都不对4.当需要上下文帮助时,选择要帮助的“难题”,希望出现MSDN窗口及显示所需“难题”的帮助信息。

应按的键的(A)Help (B)F10 (C)Esc (D)F15.Visual Basic中提供的On Error Resume Next错误陷阱语句的功能是:(A)当发生错误时,使程序跳转到语句标号为0的程序块(B)当发生错误时,不使用错误处理程序块(C)当发生错误时,忽略错误行,继续执行下一语句(D)当发生错误时,终止本过程程序执行,继续执行下一过程6.在VB环境中,工程文件的扩展名是:(A).frm (B).bas (C).vbp (D).frx7.将一个VB程序保存在磁盘上,至少会产生的文件是:(A).doc、.txt (B)aaa、.exe (C).vbp、.frm (D).bat、.sys8.下面关于对象的描述中,错误的是:(A)对象就是自定义结构变量(B)对象代表正在创建的系统中的一个实体(C)对象是一个状态和操作(或方法)的封装体(D)对象之间的信息传递是通过消息进行的9.下面4项中不属于面向对象系统三要素的是(A)变量(B)事件(C)属性(D)方法10.下面各种高级语言中,不是面向对象程序设计语言的是(A)Visual Basic (B)C++ (C)Pascal (D)Java二、填空题1.当进入VB集成环境后,发现没有显示“工具箱”窗口,应选择【1】菜单的【2】选项,使“工具箱”窗口显示。

vb教科书答案(中国农业出版社)

vb教科书答案(中国农业出版社)

二、教科书中的练习题参考答案习题三,参考答案一、VB定义了哪几种数据类型?各自有什么特点?变量有哪几种类型?常量有哪几种类型?答:在Visual Basic中,数据类型分为三大类:基本数据类型、用户自定义数据类型、枚举类型。

基本数据类型是系统定义的一些类型,用户自定义数据类型和枚举类型时用户根据实际情况可以自己定义的类型,并且类型中可以混合基本数据类型。

变量有整型、长整型、单精度浮点型、双精度浮点型、货币型、字节型、字符串型、布尔型、日期型、对象型、变体型数据,还可以使用户自定义类型和枚举类型。

常量有整型、长整型、单精度浮点型、双精度浮点型、货币型、字符串型、布尔型、日期型。

二、说明下列哪些是Visual Basic合法的直接常量,并分别指出它们的类型。

100.0 %100 123D3 0100 “ASDF””1234.5” #2006/6/1# 100# &H123 True答:合法的直接常量有:100.0、123D3、0100、“ASDF”、”1234.5”、 #2006/6/1#、100#、&H123、True、其中整型数据有:0100、&H123单精度数据有: 100.0双精度数据有:123D3、100#字符型数据有:“ASDF”、”1234.5”日期型数据有:#2006/6/1#布尔型数据有:True三、下列数据哪些是变量,哪些是常量?是什么类型的常量?Name ”name” False ff ”11/16/99”答:变量有:Name、ff、常量有:”name”、 False、”11/16/99”其中”name”、”11/16/99”是字符串型,False是布尔型四、下列符号中,哪些可以是Visual Basic的合法变量名?Sum33 x45_0 450_x Single XYZ False 答:上面合法的变量名有:Sum33、x45_0、XYZ。

五、把下列数学表达式写成Visual Basic表达式:(1)5||zyx++(2)6)1(xy+(3)xy yx310+答:(1)Abs(x+y)+z^5 (2) (1+x*y)^6 (3)(10*x+Sqr(3*y))/(x*y)六、计算下列函数的值:(1) Int(-3.14159) (2) Sqr(Sqr(81))(3) Int(Abs(99-100)/2) (4) Sgn(7*3+2-10)答:(1) -4 (2) 3 (3) 0 (4) 1七、写出下列表达式的值:(1) (2^4+8*3 Mod 5)/2 (2) 4^3\7+8-(6>3 or 5<34 and 8<>7)答:(1) 10 (2) 18八、设X=8,Y=4,Z=5,求下列表达式的值(实数保留三位小数):(1) X+4*Z (2) X^3/7答:(1) 28 (2) 73.143习题四,参考答案4.1 略。

VB教材课后习题答案

VB教材课后习题答案

<大学程序设计基础-Visual Basic>教材课后习题答案第1章一、选择题1 2 3 4 5 6 7B D BC B B D第2章一、选择题1 2 3 4 5 6 7 8 9 10B C B C B C A A A A11 12 13 14A B A A二、填空题1.rem或’ _ :2.“” # #3.5x2-3x-2sinA/34.3214565.300三、操作题1、程序段:Private Sub Command1_Click()Dim x As Integer, y As IntegerDim s As Long, c As Longx = Text1.Texty = Text2.Texts = x * yc = (x + y) * 2Label3.Caption = "长方形的面积为" + Str(s)Label4.Caption = "长方形的周长为" + Str(c)End Sub3、程序段:Private Sub Command1_Click()Text1.Text = "第一"End SubPrivate Sub Command2_Click()Text1.Text = "第二"End Sub4、程序代码:Private Sub Text1_Change()Text1.MaxLength = 10End Sub5、程序代码:Private Sub Text1_Click()Text1.SelStart = 0Text1.SelLength = Len(Text1.Text)End Sub第3章一、选择题1 2 3 4 5 6 7D A D B A D B二、填空题1. False2.B^2-4*a*c else三、操作题1.从键盘输入三个值,判断它们能否构成三角形的三个边。

如果能构成一个三角形,则计算三角形的面积。

VB实验报告参考答案(本部)

VB实验报告参考答案(本部)

实验报告参考答案(本部)实验一VB集成开发环境实验内容(3):四、设计流程①界面设计:在窗体上画一个标签。

②属性设置:标签的width设为2000,height设为300。

有边框是BordStyle设为1,背景透明是BackStyle设为0。

显示“等级考试”是将caption设为“等级考试”,居中是将Alignment 设为2。

③代码设计:无五、关键代码(注意:解题方法有多种,其它方法只要能完成实验任务也是正确的)无六、调试和测试结果至少要写测试结果正确,如果写了调试中出现的典型问题及解决方法,分数给高些实验二简单的VB程序设计实验内容(3):四、设计流程①界面设计:在窗体上画一个标签、一个命令按钮。

②属性设置:固定边框是BordStyle设为1,背景透明是BackStyle设为0。

③代码设计:分别在3个事件过程form_load(),form_click(),Cmd1_click()中编程。

其中图片从网上下载,与工程文件、窗体文件保存在同一目录,代码中使用相对路径app.path。

五、关键代码(注意:解题方法有多种,其它方法只要能完成实验任务也是正确的)Private Sub Form_Load()Form1.Picture = LoadPicture(App.Path + "\tu.jpg")Form1.Caption = "我的变化"End SubPrivate Sub Form_Click()LBL1.Top = 0LBL1.Left = 0End SubPrivate Sub Cmd1_Click()Cmd1.Height = 1500Cmd1.Width = 2000End Sub六、调试和测试结果至少要写测试结果正确,如果写了调试中出现的典型问题及解决方法,分数给高些实验三数据类型及其运算四、设计流程①界面设计:在窗体上画1个文本框、1个标签、6个命令按钮。

VB实验教材模拟练习(有答案)报告

VB实验教材模拟练习(有答案)报告

模拟练习一第一部分计算机信息技术基础知识选择题(共20分,每题2分)1.IP地址通常分为固定IP地址和动态IP地址,目前国内大多数家庭上Internet网的用户的IP地址都是B的。

A.相同B.动态C.可以相同D.固定2.下列四个不同进位制的数中,数值最大的是B。

A.十进制数73.5 B.二进制数1001101.01C.八进制数115.1 D.十六进制数4C.43.下列关于台式PC机芯片组的叙述中,错误的是D。

A.芯片组是主板上最为重要的部件之一,存储器控制、I/O控制等功能主要由芯片组实现B.芯片组与CPU同步发展,有什么样功能和速度的CPU,就需要什么样的芯片组C.芯片组决定了主板上能安装的内存最大容量及可使用的内存条类型D.同CPU一样,用户可以很方便,很简单地更换主板上的芯片组4.路由器(Router)用于异构网络的互连,它跨接在几个不同的网络之间,所以它需要使用的IP地址个数为_D_。

A.1 B.2 C.3 D.所连接的物理网络的数目5.为了既能与国际标准UCS(Unicode)接轨,又能保护现有的中文信息资源,我国政府发布了_C汉字编码国家标准,它与以前的汉字编码标准保持向下兼容,并扩充了UCS/Unicode中的其他字符。

A.ASCII B.GB2312C.GB18030 D.GBK6.在下列有关通信技术的叙述中,错误的是C。

A.目前无线电广播主要还是采用模拟通信技术B.数字传输技术最早是被长途电话系统采用的C.数字通信系统的信道带宽就是指数据的实际传输速率(简称“数据速率”)D.局域网中广泛使用的双绞线既可以传输数字信号,也可以传输模拟信号7.在下列有关商品软件、共享软件、自由软件及其版权的叙述中,错误的是C。

A.通常用户需要付费才能得到商品软件的合法使用权B.共享软件是一种“买前免费试用”的具有版权的软件C.自由软件允许用户随意拷贝,但不允许修改其源代码和自由传播D.软件许可证确定了用户对软件的使用方式,扩大了版权法给予用户的权利8.下列有关Internet的叙述错误的是_A。

VB课本各个实验课后题答案

VB课本各个实验课后题答案

实验33-2elPrivate 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")End SubPrivate Sub Form_Load()End Sub3-3Private 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 SubPrivate Sub Form_Load()End Sub3-4Private 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 Sub3-5Private Sub Command1_Click()Label1.Caption = DateEnd SubPrivate Sub Command2_Click()Label2.Caption = TimeEnd SubPrivate Sub Command3_Click()Label3.Caption = WeekdayName(Weekday(Date))End Sub实验44-1Private 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 Sub4-2Private Sub Command1_Click()x = InputBox("请输入弧度数值", "弧度转换", 0)x = 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 Sub4-3Private Sub Command1_Click()Dim x%, y%, z%x = InputBox("输入第一个数")y = InputBox("输入第二个数")z = InputBox("输入第三个数")If 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-4Private 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.5Case Is >= 100fei = x * 1End SelectMsgBox "上网费用为:" & fei & "元" End IfEnd SubPrivate Sub Command2_Click()EndEnd Sub4-4带循环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 >= 100fei = x * 1End SelectElseMsgBox zongfei, , "上网费用为"Exit DoEnd Ifzongfei = zongfei & Space(3) & feiLoopEnd SubPrivate Sub Command2_Click()EndEnd Sub实验55-1Private Sub Command1_Click()For n = 1 To 100s = s + nNext nPrint sEnd Sub5-2Private 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 NextNextEnd Sub5-3Private 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 Sub5-4Private Sub Command1_Click()Dim x%, y%, z%, i%, j%For x = 0 To 9For y = 0 To 9For z = 0 To 9i = x * 100 + x * 10 + zj = y * 100 + z * 10 + zIf i + j = 532 Then Print x; y; zNextNextNextEnd SubPrivate Sub Command2_Click()For i = 0 To 9For j = 0 To 9For k = 0 To 9If Val(i & i & k) + Val(j & k & k) = 532 Then Print i, j, kNextNextNextEnd Sub5-5Private Sub Command1_Click()x = Val(Text1)n = 0Do While x <= Val(Text1) * 4x = x * (1 + 0.1)n = n + 1LoopPrint x, nEnd Sub5-6Private Sub Command1_Click()n = Val(InputBox("输入计算到第几项:", , 0)) For i = 1 To ns = s + (-1) ^ (i + 1) * iNextPrint n, sEnd Sub5-7Private Sub Command1_Click()Dim n%, s%n = Val(InputBox("输入计算到第几项:", , 0)) s = 0: t = 0For i = 1 To nt = t + is = s + tNextPrint 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 Sub5-8Private Sub Command1_Click()Dim s As Double, pi As Doublen = 1: s = 0Do While 1 / (2 * n - 1) >= 0.0001s = s + (-1) ^ (n + 1) * 1 / (2 * n - 1)n = n + 1Looppi = 4 * sPrint piEnd Sub5-9Private Sub Command1_Click()For n = 100 To 999k = Trim(Str(n))If n = Left(k, 1) ^ 3 + Mid(k, 2, 1) ^ 3 + Right(k, 1) ^ 3 Then Print n NextEnd Sub5-10Private Sub Command1_Click()For n = 999 To 100 Step -1For i = 2 To n - 1If n Mod i = 0 Then Exit ForNext iIf i = n ThenPrint nk = k + 1End IfIf k = 3 Then Exit ForNext nEnd SubPrivate Sub Form_Load()End Sub5-11Private Sub Command1_Click()s = 0: Max = 0: Min = 1000For i = 1 To 20Randomizex = Int(Rnd * 50 + 51)Print x;If x > Max Then Max = xIf x < Min Then Min = xs = s + xNext iaver = s / 20PrintPrint "max="; Max, "min="; Min, "aver="; aver End Sub5-12Private Sub Command1_Click()For i = 1 To 4Print Tab(20 - i); String(2 * i - 1, "*")NextEnd SubPrivate Sub Command2_Click()For i = 1 To 4Print Tab(30 - i);For j = 1 To 2 * i - 1Print "*";NextPrintNextEnd Sub弹球Private Sub Command1_Click()Dim h!, s!h = 100s = 0For i = 1 To 10s = s + hh = h / 2Print hs = s + hNext iPrint "共经过了"; s - h, "最后一次弹高"; h End Sub完数Private Sub Command1_Click()Dim s%For i = 1 To 1000s = 0For j = 1 To i - 1If i Mod j = 0 Then s = s + jNext jIf i = s Then Print i; "是完数"Next iEnd Sub实验6 6-1Private Sub Command1_Click()Dim i%, a%(1 To 10), max%, min%, avg!max = 0: min = 100: avg = 0For i = 1 To 10a(i) = Int(Rnd * 51 + 50)Print a(i);If a(i) > max Then max = a(i)If a(i) < min Then min = a(i)avg = avg + a(i)Next iPrintPrint "--------------------"Print max, min, avg / 10End Sub6-2Private Sub Command1_Click()Dim a%(1 To 20), i%, y%, l%, z%, j%, b% For i = 1 To 20a(i) = Int(Rnd * 101)Print a(i);If i Mod 10 = 0 Then PrintSelect Case a(i)Case 90 To 100y = y + 1Case 80 To 89l = l + 1Case Is >= 70z = z + 1Case Is >= 60j = j + 1Case Elseb = b + 1End SelectNext iPrint "优秀:"; yPrint "良好:"; lPrint "中:"; zPrint "及格:"; jPrint "不及格:"; bEnd Sub6-3Private Sub Command1_Click()Dim a(1 To 10) As Integer, i%, j%For i = 1 To 10a(i) = Int(Rnd * 50 + 1)Print a(i);Next iPrintn = 10For i = 1 To n - 1For j = n To i + 1 Step -1If a(j) > a(j - 1) Thent = a(j): a(j) = a(j - 1): a(j - 1) = tEnd IfNext jNext iPrint "-----------排序之后--------"For i = 1 To 10Print a(i);Next iPrintEnd Sub6-4Private Sub Command1_Click()Dim a(1 To 20) As Integer, i%For i = 1 To 20a(i) = Int(Rnd * 101 + 1)'Print a(i);'If i Mod 5 = 0 Then PrintIf i Mod 5 = 0 Then Print a(i) Else Print a(i);Next iPrintEnd SubPrivate Sub Command2_Click()Dim a(1 To 20) As Integer, i%For i = 1 To 20a(i) = Int(Rnd * 101 + 1)Print a(i);If i Mod 5 = 0 Then PrintNext iEnd SubPrivate Sub Form_Load()End Sub6-5Private Sub Command1_Click()Dim a%(1 To 9), b(1 To 3, 1 To 3) As Integer, i%, j% For i = 1 To 9a(i) = Int(Rnd * 100)Print a(i);Next iPrintPrintFor i = 1 To 3For j = 1 To 3b(i, j) = a((i - 1) * 3 + j)Print b(i, j);Next jPrintNext iEnd Sub6-6Private Sub Command1_Click()Dim a() As Integer, i%n = InputBox("n")ReDim a(1 To n, 1 To n)For i = 1 To nFor j = 1 To na(i, j) = Int(Rnd * 100)Print a(i, j);Next jPrintNext iEnd Sub6-7Private Sub Command1_Click()Dim a() As Integer, i%n = InputBox("n")ReDim a(1 To n, 1 To n)For i = 1 To nFor j = 1 To nIf (i = j) Or (i + j = n + 1) Then a(i, j) = 1 Else a(i, j) = 0Print a(i, j);Next jPrintNext iEnd Sub6-8Private Sub Command1_Click()Dim a(), i%, j%, m%, n%m = InputBox("M")n = InputBox("n")ReDim a(1 To m, 1 To n)For i = 1 To mFor j = 1 To na(i, j) = Int(Rnd * 100)Print a(i, j);Next jPrintNext iDim max%, imax%, jmax%max = a(1, 1): imax = 1: jmax = 1For i = 1 To mFor j = 1 To nIf a(i, j) > max Thenmax = a(i, j)imax = ijmax = jEnd IfNext jNext iPrint "--------------"Print max, imax, jmaxEnd Sub6-9Private Sub Command1_Click()Dim a(), i%, j%, m%, n%m = InputBox("M")n = InputBox("n")ReDim a(1 To m, 1 To n)For i = 1 To mFor j = 1 To na(i, j) = Int(Rnd * 100)Print a(i, j);Next jPrintNext iReDim Preserve a(1 To m, 1 To n + 1) '计算i行的和,放入第n+1列For i = 1 To ma(i, n + 1) = 0For j = 1 To na(i, n + 1) = a(i, n + 1) + a(i, j)Next jNext iFor i = 1 To mFor j = 1 To n + 1Print a(i, j);Next jPrintNext iEnd Sub6-10Private Sub Command1_Click()Dim n%, a%(), i%n = InputBox("n")ReDim a(1 To n)Print "-----初值-----------------"For i = 1 To n '赋值a(i) = Int(Rnd * 21 - 10)Print a(i);If i Mod 5 = 0 Then PrintNext iPrintFor i = 1 To n - 1 '将相同的值的数组元素赋值为-100 If a(i) <> -100 ThenFor j = i + 1 To nIf a(j) = a(i) Then a(j) = -100Next jEnd IfNext iPrint "-----重复的值赋值为-100----------"For i = 1 To n '赋值Print a(i);If i Mod 5 = 0 Then PrintNext iPrintPrint "-----删除掉重复的值------------"'下面的代码将值为-11的数组元素过滤掉k = 0For i = 1 To nIf a(i) <> -100 Thenk = k + 1a(k) = a(i)End IfNext i'打印前k个ReDim Preserve a(1 To k)For i = 1 To kPrint a(i);If i Mod 5 = 0 Then PrintNext iEnd Sub。

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位数,其各位数字的立方和等于该数本身。

VB1-3章_习题答案(课本习题)

VB1-3章_习题答案(课本习题)

VB习题答案(1-3章)习题一一、选择题1.C 2.C 3.A 4.C 5.C 6.C 7.B二、填空题1.学习版、专业版、企业版2.窗体界面3.Alt+F4三、简答题1.简述Visual Basic语言的特点。

Visual Basic是Windows操作平台下的可视化编程语言,它继承和发展了BASIC语言的基本功能,具有简单易学、功能强大等特点。

Visual Basic提供了可视化设计工具,以图形用户界面(GUI)为PC机用户提供了一个直观的工作环境。

支持面向对象的程序设计(OOP),采用事件驱动方式,提供对象的链接与嵌入(OLE)和访问数据库等功能。

2. Visual Basic 6.0的集成开发环境由哪些部分组成,各部分的主要功能是什么?1. 标题栏:用来显示打开的工程名称和系统的工作状态。

2. 菜单栏:菜单栏中包括“文件”、“编辑”、“视图”、“工程”、“格式”、“调试”、“运行”、“查询”、“图表”、“工具”、“外接程序”、“窗口”和“帮助”共有13个菜单项。

这是一组下拉式菜单,提供了设计、编辑、编译和调试应用程序所需要的绝大部分功能和命令。

3. 工具栏: Visual Basic 6.0提供了“编辑”、“标准”、“窗体编辑器”和“调试”4种工具栏。

工具栏上形象直观的排列着一些最常用的命令按钮,对应特定的常用操作命令。

4. 控件箱:控件箱提供了一组控件。

设计界面时,用户可以用鼠标双击控件箱中的控件,在窗体中画出该控件,或用鼠标单击控件箱中的控件,然后在窗体中拖动鼠标画出所需的控件。

5. 窗体设计器:窗体设计器是应用程序最终面向用户的窗口。

各种控件、图形、数据以及程序的运行结果都在窗体设计器窗体中显示出来。

6. 工程资源管理器:在工程资源管理器窗口中(图1-2),列出了应用程序的工程以及工程中的窗体和模块。

7. 属性窗口:属性窗口主要用来设置Visual Basic窗体及各控件的属性特征,属性窗口的主体部分分为左右两列,左边是控件的属性名列表,右边是控件的属性值列表。

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实验参考答案VB(Visual Basic)是一种常用的编程语言,广泛应用于软件开发领域。

在学习VB的过程中,进行实验是非常重要的一部分。

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

实验一:简单的计算器程序要求:编写一个简单的计算器程序,能够实现基本的加减乘除运算。

参考答案:```vbPrivate Sub btnCalculate_Click()Dim num1 As DoubleDim num2 As DoubleDim result As Doublenum1 = CDbl(txtNum1.Text)num2 = CDbl(txtNum2.Text)If rbAdd.Checked Thenresult = num1 + num2ElseIf rbSubtract.Checked Thenresult = num1 - num2ElseIf rbMultiply.Checked Thenresult = num1 * num2ElseIf rbDivide.Checked ThenIf num2 <> 0 Thenresult = num1 / num2ElseMsgBox("Error: Division by zero!")End IfEnd IftxtResult.Text = resultEnd Sub```实验二:学生成绩管理系统要求:编写一个学生成绩管理系统,能够实现学生信息的录入、查询和统计功能。

参考答案:```vbPrivate Sub btnAdd_Click()Dim name As StringDim score As Integername = txtName.Textscore = CInt(txtScore.Text)lstStudents.Items.Add(name & " - " & score)End SubPrivate Sub btnSearch_Click()Dim searchName As StringDim index As IntegersearchName = txtSearch.Textindex = lstStudents.FindString(searchName)If index <> -1 ThenlstStudents.SelectedIndex = indexElseMsgBox("Student not found!")End IfEnd SubPrivate Sub btnStatistics_Click()Dim totalScore As IntegerDim averageScore As DoubleDim count As IntegerFor Each item As String In lstStudents.ItemsDim score As Integer = CInt(item.Split("-")(1).Trim()) totalScore += scorecount += 1NextaverageScore = totalScore / counttxtTotalScore.Text = totalScoretxtAverageScore.Text = averageScore```实验三:简单的图书管理系统要求:编写一个简单的图书管理系统,能够实现图书信息的录入、查询和删除功能。

VB实验教材的实验与习题答案

VB实验教材的实验与习题答案
?
实验4-11
Len(st); Ch=Lcase(ch) ; Case Else ; Mid(Char,I,1)&St
?
实验4-12
i\100 ; (i Mod 100)\10
?
习题
一、 选择题
1.C 2. C 3.A 4. D 5.A 6. B 7. B 8. D 9. B
Const g = 9.8
Dim V0 As Single, t As Single
Dim S As SingFra biblioteke V0 = InputBox("请输入V0的值:")
t = InputBox("请输入时间t的值:")
S = (1 / 2) * g * t ^ 2 + V0 * t
Randomize
a = Int(Rnd * (999 - 100 + 1) + 100)
h = Mid(CStr(a), 1, 1)
t = Mid(CStr(a), 2, 1)
n = Right(CStr(a), 1)
Label5.Caption = a & "-->" & Format(n * 100 + t * 10 + h, "000")
List1.AddItem Log(a) / Log(b)
End Sub
‘关系运算
Private Sub Command2_Click()
List1.Clear
List1.AddItem Mid("Visual Basic", 1, 12) = Right("Language Visual Basic", 12)

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程序设计教程实验指导与习题解答上机答案

自测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程序设计课后习题答案(科学出版社)(1)

VB程序设计课后习题答案(科学出版社)(1)

同步练习1二、选择题01——05 CADAB06——10 ACDAB11——15 CBDBB同步练习2二、选择题01——05 ABDCA06——10 CACBC11——15 DADAD16——20 BDBBB三、填空题1.可视2.LEFT、TOP、WIDTH、HEIGHT3.按字母顺序4.查看代码5.工具、编辑器6.FORM窗体、FONT7.MULTILINE8.在运行时设计是无效的9.工程、工程属性、通用、FORM1.SHOW 10.TABINDEX、0同步练习3二、选择题01——05 BCADB06——10 ADBBC11——15 DBCBA16——20 BAABB三、填空题1.整型、长整型、单精度型、双精度型2.SIN(30*3.14/180)+SQR(X+EXP(3))/ABS(X-Y)-LOG(3*X) 3.164、今天是:3-194.FALSE5.-4、3、-3、3、-4、46.CDEF7.(X MOD 10)*10+X\108.(35\20)*20=20( 35 \ 20 )* 20 = 209.X MOD 3=0 OR X MOD 5=010.27.6、8.2、8、1、100、397、TRUE、FALSE同步练习4一、选择题01——05 DBCAD06——10 CBBAB11——15 D25BAC16——20 CBACB21——25 DAABC二、填空题1.正确性、有穷性、可行性、有0个或多个输入、有1个或多个输出2.1 2 33.X>=74.X<AMIN5.10246.117.128.6、I、”*”;9.0、100、I+110.2*I、6、”*”;三、编程题1.编程求n!,n从键盘输入。

Private Sub Form_Click()Dim n%, s%, i%n = Val(InputBox("请输入n的值:"))s = 1For i = 1 To ns = s * iNext iPrint n; "!="; sEnd Sub2.编程求100以内的全部质数。

VB教材习题_参考答案1-6章

VB教材习题_参考答案1-6章

Visual Basic程序设计教程(李杰主编清华大学出版社 2011.2)习题答案习题2参考答案2.2 单选题1、B2、C3、A4、D5、A6、B7、D8、C9、B 10、D2.3 填空题1、事件驱动2、Cancel3、Name4、Private Sub Form1_Click()5、运行程序6、代码窗口7、Text1.Text = "我是一个学生"8、LostFocus()事件9、Label1.Caption = "请输入用户名"Label1.ForeColor = vbGreen10、(Text1.Text)*22.4 事件练习题1、①该事件的对象是标签Label1,单击标签触发该事件。

②缺省Label1对象的代码是针对窗体,其余有Label1对象的代码是针对标签的。

③Label1.AutoSize = True语句使标签的大小自动随文字的多少而改变。

2、程序运行后,单击命令按钮,在Text1文本框显示"知音海外版",在Text2文本框显示"计算机世界"。

3、单击“显示文本框”命令按钮触发的事件过程Private Sub Command1_Click()Text1.Visible = TrueText1.ForeColor = vbRedText1.Text = "VB程序设计"End Sub单击“隐藏文本框”命令按钮触发的事件过程Private Sub Command2_Click()Text1.Visible = FalseEnd Sub习题3参考答案3.2 单选题1 C2 C3 D4 B5 B6 B7 B8 C9 D3.3填空题1.REM(或‘) _ :2 双引号(“”)#3 $ %4 UCASE()5.(log(1+d*d)-exp(2))^(5/2) [ 还有一种理解方式:(log(1+d*d)-e*e)^(5/2)]6. &O113 &H4B习题4参考答案4.2 单选题BDCBD4.3 填空题1.MsgBox("是否删除? ", vbOkCancel + vbQuestion, "删除")2.The Length of 12345=53. 变量或表达式4. 15. loop4.4 程序设计题1.Private Sub Command1_Click()If Text1.Text = "asdf" ThenMsgBox "密码正确!"ElseMsgBox "密码错误", vbRetryCancel, "错误"End IfEnd Sub2. < 题目中的“连续产生n*n个字母”应该改为“连续产生n个字母”> Private Sub Command1_Click()Dim n%, y%, i%n = Val(Text1.Text)If n > 10 Or n<=0 ThenMsgBox "数据超界!"Text1.Text=””Text1.SetFocusElsey = Int((90 - 65 + 1) * Rnd) + 65For i = 1 To nPrint Chr(y + i)Next iEnd IfEnd Sub3.Private Sub Form_Click()Dim a!, b!, c!, x1!, x21, d!a = Val(Text1.Text)b = Val(Text2.Text)c = Val(Text3.Text)d = b * b - 4 * a * cIf d >= 0 Thenx1 = (-b + Sqr(d)) / (2 * a)X2 = (-b - Sqr(d)) / (2 * a)Print x1; X2ElsePrint "没有实根"End IfEnd Sub4.Private Sub Form_Click()Dim i%, j%, s%, n%,q%n = InputBox("enter n:")q = 0For i = 1 To ns = 0For j = 1 To is = s + jNext jq = q + sNext iPrint qEnd Sub5.Private Sub Form_Click()Dim i%, j%, n%, k%n = 0For i = 1 To 3For j = 1 To 3For k = 1 To 3Print i & j & k,n = n + 1If n Mod 5 = 0 Then PrintNext kNext jNext iPrintPrint "n="; nEnd Sub6.Private Sub Form_Click()Dim i%, j%, n%, k%For i = 1 To 9For j = 0 To 9For k = 0 To 9n = i * 100 + j * 10 + kIf n = i ^ 3 + j ^ 3 + k ^ 3 Then Print n,Next kNext jNext iEnd Sub习题5参考答案5.2 单选题1 A2 B3 C4 C5 D6 D7 A8 C9 D 10 A 11 A 12 B 5.3 填空题1.False2.Caption3.0 ListCount-14.Print List1.List(3)5.Combo1.ReMoveItem 36.LargeChange7.Timer8.5009.Shape10.Borderstyle5.4 程序设计题1.Dim op1, op2, result As SingleDim operator As IntegerPrivate Sub Command1_Click(Index As Integer)If Index >= 0 And Index <= 10 ThenIf txt1.Text = "" Thentxt1.Text = CStr(Index + 1)Elsetxt1.Text = txt1.Text + CStr(Index + 1) End IfEnd IfIf Index >= 12 And Index <= 15 Thenop1 = CDbl(txt1.Text)operator = Indextxt1.Text = ""End IfIf Index = 11 Thenop2 = CDbl(txt1.Text)Select Case operatorCase 12result = op1 + op2Case 13result = op1 - op2Case 14result = op1 * op2Case 15result = op1 / op2End Selecttxt1.Text = CStr(result)End IfEnd SubPrivate Sub Command2_Click()op1 = 0op2 = 0result = 0txt1.Text = ""End SubPrivate Sub Command3_Click()EndEnd Sub2.Private Sub chkBold_Click()If chkBold.Value = vbChecked ThenTxt1.FontBold = TrueElseTxt1.FontBold = FalseEnd IfEnd SubPrivate Sub chkUnderline_Click()If chkUnderline.Value = vbChecked Then Txt1.FontUnderline = TrueElseTxt1.FontUnderline = FalseEnd IfEnd SubPrivate Sub Opt18_Click()Txt1.FontSize = 18End SubPrivate Sub Opt22_Click()Txt1.FontSize = 22End Sub3.Private Sub Form_Load()List1.AddItem "上海"List1.AddItem "北京"List1.AddItem "天津"List1.AddItem "广东"List1.AddItem "辽宁"List1.AddItem "陕西"List1.AddItem "河南"List1.AddItem "深圳"List1.AddItem "四川"List1.AddItem "重庆"End SubPrivate Sub List1_DblClick()For i = 0 To 9If List1.ListIndex = i ThenList2.AddItem List1.List(i)List1.RemoveItem iEnd IfNext iEnd Sub4.Private Sub Command1_Click()Timer1.Enabled=TrueTimer1.Tag=Text1.Text*60Frame1.Caption=“现在开始倒计时”End SubPrivate Sub Timer1_ Timer()Timer1.Tag=Timer1.Tag-1If m<0 ThenTimer1.Enabler=FalseMsgBox“预定的时间到了!”,0,“倒计时”Frame1.Caption=“请输入计时的分钟数:”Text1.Text=0Exit SubEnd Ifn1=Format(m Mod 60,“00”)n2=Format(m \ 60)Mod 60,“00:”)n3=Format(m \ 3600),“00:”)Text1.Text=n3&n2&n1End Sub习题6参考答案6.1单选题1.C2.C3.B4.B5.B6.D7.B8.B9.A 10.A6.2 填空题1.Option Base 12.变体类型3.preserve (注意:教材上面写成perserve, 教材错误)4.Variant变体类型5.96.相同,索引号7.1).通过复制粘贴的方式创建控件数组2).在设计时候添加多个同类型的类型控件,然后通过“属性”窗口将这些控件的名称改为相同,并把Index属性设为不同的下标值。

!VB1-4章习题参考答案.docx

!VB1-4章习题参考答案.docx

《Visual Basic程序设计基础》教材习题参考答亲第1章VisuaI Basic 6.0程序设计概述判断题1.J2.J3. X4. V5. X6.V7.X8. J9・J10. X11.J 12.J13. X14.15. X16.J17.V18. J19.V20. V选择题l.C 2. B 3.D 4.C 5.B 6. A7.D 8. A 9. B 10. A 11. C 12. D三、填空题1.对象、事水牛2.属性、方法、事件3.控件、屏幕(Screen)4.对象5. Left、Top6.属性、v对象名>.<属性名>=v表达式〉7.网格8. Forml9.Load或Initialize或Resize或Activate (自动执行的先后顺序也按此排列)10.Activate、Deactivate四、程序设计题程序1.界面设计参看图10-1> 10・2以及表10-1 o表10-1实例2各控件的主要属性设置控件属性(属性值)属性(属性值)标签控件1Name(Labell)Caption("我的第一个VB程序”)标签控件2Name(Label2)Caption(”请输入你的姓名”)FontUnderLine(True)Fontltalic(True)命令按钮1Namc(Command 1)CaptionC'确定(&O)”)命令按钮2Name(Command2)Caption(”结束(&X)”)过程设计如下:Private sub Form_Click()Command2.Enabled = FalseEen SubPrivate Sub Commandl_Click()Label2.Visible = FalseLmb ell. Caption = Text 1. Text + n :欢迎使用” + Lab ell. Caption Text1.Visible = FalseCommandl.Enabled = False: Command2.Enabled = TrueEnd SubPrivate Sub Command2_Click()End End Sub程序3.界面设计略,过程设计如下(窗体Height 值的计量包括标题、边框,而ScaleHeight 值仅与窗体有效绘图区域的高度有关):Private Sub Form_Load()Textl.Text = "Visual Basic 程序设计” End SubPrivate Sub Form_Resize() Textl.Top = 0: Textl-Left = 0Textl.Width = Forml. ScaleWidth/2 '用 Forml .ScaleWidth 效果优于用Forml ・ Width Textl ・ Height = Forml ・ ScaleHeight/2 1 用 Forml. ScaleHeight 优于用 Forml ・ Height Commandl•Top = Forml•ScaleHeight 一 Commandl•HeightCommandl.Left = Forml.Scalewidth 一 Commandl.Width End Sub程序2.界面设计略,过程设计如卞: PrivateSub Commandl_Click()Forml.FontSize = Forml.FontSize Forml .Cis: Print ”欢迎使用 VB” End SubPrivate Sub Command2_Click() Forml.FontSize = Forml.FontSize Forml .Cis : Print ”欢迎使用 VB” End SubPrivate Sub Command3_Click() Forml.FontBold = TrueForml .Cis: Print ”欢迎使用 VB“ End SubPrivate Sub Command4_Click() Forml.FontBold = FalseForml .Cis: Print ”欢迎彳吏用 VB” End Sub 运行时,不得连续、 适时错误。

vb课后习题参考答案

vb课后习题参考答案

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、25、&、+、+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。

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)。

第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 = 3j = 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。

相关文档
最新文档