大学VB考试程序调试题及参考答案.doc_第1页
大学VB考试程序调试题及参考答案.doc_第2页
大学VB考试程序调试题及参考答案.doc_第3页
大学VB考试程序调试题及参考答案.doc_第4页
大学VB考试程序调试题及参考答案.doc_第5页
已阅读5页,还剩28页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

程序调试题及参考答案1. 该过程是用于计算一元二次方程的根,并将结果输出来。Option ExplicitPublic Sub getanswer()Dim dalt!, a#, b#, c# a = InputBox(输入系数a) b = InputBox(输入系数b) c = InputBox(输入系数c) dalt = b * b - 4 * a * c If dalt 0 Then -1- dalt = Sqr(dalt) MsgBox Format(-b + dalt) / 2 / a, 0.00) + Chr(13) + Chr(10) + Format(-b - dalt) / 2 / a, 0.00) ElseIf dalt = 0 Then -2- MsgBox Format(-b / 2 / a, 0.00) + Chr(13) + Chr(10) + Format(-b / 2 / a, 0.00) Else dalt = Sqr(-dalt) -3- MsgBox Format(-b / 2 / a, 0.00) + + + Format(dalt) / 2 / a, 0.00) + i + Chr(13) + Chr(10) + Format(-b / 2 / a, 0.00) + - + Format(dalt) / 2 / a, 0.00) + i End IfEnd Sub2 过程prt用以打印一个菱形图案,请在横线上填入必要的内容,使其完整。Option ExplicitPublic Sub prt() 打印数字金字塔 # # # # # # # # # Dim i As Integer, j As Integer Dim start As String 每行起始空格数 Dim Count As Integer 每行#个数 For i = 1 To 9 If i =6 Count = 19 - 2 * i -2- End If Form1.Print start; -3- For j = 1 To Count Form1.Print #; Next j Form1.Print -4- Next iEnd Sub3 过程pyramid用以打印一个数字金字塔,请在横线上填入必要的内容,使其完整。Option ExplicitPublic Sub pyramid() 打印数字金字塔 1 222 33333 4444444 555555555 6666666 77777 888 9 Dim i As Integer Dim j As Integer Dim start As String 每行起始空格数 Dim num As Integer 每行数字个数 For i = 1 To 9 If i = 5 Then start = Space(20 - i) num = 2 * i - 1 Else start = Space(10 + i) -1- num = 19 - 2 * i -2- Form1.Print -4- Next iEnd Sub 4 打印由数字组成的如下所示金字塔图案Option ExplicitPublic Sub prt() 9 888 77777 6666666 555555555 44444444444 3333333333333 222222222222222 11111111111111111 Dim i As Integer, j As Integer * 错误1 * For i = 9 To 1 For i = 9 To 1 Step -1 Form1.Print Space(i); * 错误2 * For j = 1 To 2 * i - 1 For j = 1 To 2 * (9 - i + 1) - 1 * 错误3 * Form1.Print i Form1.Print Trim(Str(i); Next j Form1.Print Next iEnd Sub5 计算s=7+77+777+(n个7组成的数)。Public Sub total() 计算s=7+77+777+(n个7组成的数) Const n = 20 Dim s As Single Dim i As Integer For i = 1 To n s = s + number(i) -1- -2- Next i Form1.Print s=; sEnd SubPublic Function number(n As Integer) As Single -2- Dim i As Integer number = 0 For i = 1 To n number = number * 10 + 7 -3- Next iEnd Function6 eat()过程是用于计算猴子共摘了多少个桃子。(小猴在一天摘了若干个桃子,当天吃掉了一半多一个第二天吃了剩下的一半多一个; 以后每天都吃尚存的一半零一个.到第7天早上要吃时只剩下一个了)Option ExplicitPublic Sub eat()Dim n%, i%, x% x = 1 For i = 6 To 1 Step -1 -1- -2- x = (x + 1) * 2 -3- Next i Form1.Print 共有; x; 个桃子End Sub7Modify.bas模块中的Prime过程是求出100到200之间所有的素数,并打印出来。Public Sub prime() 打印出100到200之间所有的素数,并统计素数的个数 Dim i As Integer Dim j As Integer Dim k As Integer Dim t As Integer 统计素数的个数 Dim b As Boolean For i = 100 To 200 b = True k = 2 - 1 - j = Int(Sqr(i) Do While k = j And b If i Mod k = 0 Then b = False - 2 - End If k = k + 1 - 3 - Loop If b = True Then - 4 - t = t + 1 Form1.Print i End If Next i Form1.Print t=; tEnd Sub8 过程even用以验证一个偶数可以分解为两个素数之和;Isprime用以判断x是否是素数。Public Sub even() 从键盘输入一个大于4的偶数,将它所有的不重复的分解式求出 Dim x As Integer Dim i As Integer Do While x 4 Or x Mod 2 0 -1- 保证x是大于4的偶数 x = Val(InputBox(x=) Loop For i = 3 To x / 2 Step 2 在不大于x的奇数中找素数 If Isprime(i) And Isprime(x - i) Then -2- Form1.Print x; =; i; +; x - i End If Next iEnd SubPublic Function Isprime(x As Integer) As Boolean Dim i As Integer Isprime = True -3- For i = 2 To x - 1If x Mod i = 0 Then -4- Isprime = False Exit For End If Next iEnd Function9 该过程是用于找出被3、5、7除,余数为1的最小的5个整数,并将结果输出来。Option ExplicitPublic Sub find()Dim countN%, n% countN = 0 n = 1 Do n = n + 1 -1- If n Mod 3 = 1 And n Mod 5 = 1 And n Mod 7 = 1 Then -2- Form1.Print n countN = countN + 1 End If Loop While countN 5 -3-End Sub10 rn过程通过调用函数isLeapYear判断某年是否是闰年若是,则打印今年是润年,否则,打印今年不是闰年。Option ExplicitPublic Sub rn() Const year = 2023 If Not (isLeapYear(year) Then -1- Form1.Print 今年是闰年 Else Form1.Print 今年不是闰年 End If End Sub Function isLeapYear(y As Integer) As Boolean If y Mod 400 = 0 Or (y Mod 4 = 0 And y Mod 100 0) Then -2- -3- -4- isLeapYear = False Else isLeapYear = True End IfEnd Function11 在考生目录中,已有模块文件Modify.bas。Modify.bas模块中的CountTo60过程是用于从一堆一分、二分、五分的硬币中取出20枚,使其总值为60分,要求输出取法的数量及每一种取法的一分、二分、五分的个数。 Public Sub CountTo60() Dim one As Integer 一分硬币个数 Dim two As Integer 二分硬币个数 Dim five As Integer 五分硬币个数 Const n = 20 总数20枚 Dim k As Integer 取法数量 k = 0 For one = 1 To n * 错误1 * For two = 1 To n - one For two = one + 1 To n five = n - one - two * 错误2 * If one + two * 2 + five * 5 = 60 Then one + two + five = 60 k = k + 1 Form1.Print one=; one; two=; two; five=; five End If * 错误3 * Next two Next one * 错误4 * Next one Next two Form1.Print k=; kEnd Sub12 过程same用以找出1-100之间所有的同构数。所谓同构数是指一个数出现在它的平方数的右端,如25在25平方62的右端,则25为同构数。利用数字转字符再取出右端字符的方法进行判断。Public Sub same() Dim i As Integer Dim x1 As String, x2 As String For i = 1 To 1000 x1 = Trim(Str(i) -1- 将i转字符型 x2 = Trim(Str(i 2) -2- 将i2转字符型 If x1 = Right(x2, Len(x1) Then -3- Form1.Print i; 是同构数 End If Next iEnd Sub13 Modify.bas模块中的ArmstrongNumber过程是用于求出1-999之间所有的Armstrong数,并打印出来。所谓Armstrong数是指一个数等于它每位上数字的立方和?Public Sub ArmstrongNumber() Dim armstrong As Integer Dim i As Integer Dim hundred As Integer 百位上的数字 Dim ten As Integer 十位上的数字 Dim one As Integer 个位上的数字 For i = 1 To 999 hundred = i 100 -1- ten = Int(i Mod 100) 10) one = i Mod 10 -2- If i = hundred 3 + ten 3 + one 3 Then -3- Form1.Print i; is armstrong number End If Next iEnd Sub14 过程cloze用以求11000中的所有完数。所谓完数是指一个数的所有因子之和等于其自身。Option ExplicitDim a(50) As Integer 存放每个数分解出来的因子Public Sub cloze() 求1-100中的所有完数 Dim i As Integer, j As Integer Dim n As Integer 因子个数 Dim s As Integer 因子和 For i = 1 To 1000 n = 0 因子个数 s = 0 因子和 factor i, n -1- For j = 1 To n s = s + a(j) Next j If i = s Then -2- Form1.Print i End If Next iEnd SubPublic Sub factor(ByVal x As Integer, ByRef k As Integer) Dim i As Integer a(1) = 1 k = 1 For i = 2 To x / 2 -3- If x Mod i = 0 Then k = k + 1 a(k) = i -4- End If Next iEnd Sub15Modify.bas模块中的summary过程是用于计算s=1+2+22+23+直至 s 超过1E+16Public Sub summary() Dim s As Single Dim i As Integer s = 1 -1- i = 1 Do While s 1E+16 -2- s = s + 2 i i = i + 1 -3- Loop Form1.Print S = ; sEnd Sub16 Modify.bas模块中的qiuN过程是用于求出满足不等式1+2x+3x2+4x3+(n+1)xn1000的最大n值。其中x是大于等于1的实数,其值由键盘输入。Option ExplicitPublic Sub qiuN()该过程是用于求出满足不等式 1+2x+3x2+4x3+(n+1)xn1000的最大n值, 其中x是大于等于1的实数,其值由键盘输入。 Dim x As Single, s As Single, n As Integer, s1 As Single, p As Single x = Val(InputBox(x=) s = 1: n = 1 p = x While s 1000 -1- s1 = s s = s + (n + 1) * p p = p * x -2- n = n + 1 -3- Wend n = n - 2 -4- Form1.Print The Maxism of n; n, s=; s1End Sub17 sum过程用于计算f=1-1/(2*3)+1/(3*4)-1/(4*5)+1/(19*20)Option ExplicitPublic Sub sum() Dim f As Single Dim i As Integer Dim sign As Integer sign = -1 -1- f = 1 For i = 2 To 19 -2- f = f + sign / (i * (i + 1) sign = -sign -3- Next i Form1.Print f=; fEnd Sub18 Modify.bas模块中的JiSuan过程是用于计算1-(1/2)+(1/3)-+(1/99)-(1/100)的值并打印出来。Public Sub JiSuan() 计算1-(1/2)+(1/3)-.+(1/99)-(1/100)的值并打印出来 Dim i As Integer * 错误1 * Dim k As Integer Dim k As Single Dim s As Single s = 0 * 错误2 * For i = 1 To 100 For i = 1 To 100 Step 2 k = 1 / i s = s + k * 错误3 * k = 1 / i + 1 k = 1 / (i + 1) s = s - k Next i Form1.Print s=; sEnd Sub19 Modify.bas模块中的summary过程是用于计算1!+2!+20!,并打印出计算结果.Modify.bas模块中的nFaction函数过程用于计算n!。Public Function nFactor(ByVal n As Integer) As Double Dim i As Integer Dim temp As Double temp = 1 -1- For i = 1 To n temp = temp * i Next i nFactor = temp -2-End FunctionPublic Sub summary() Dim sum As Double Dim i As Integer Dim n As Integer n = 20 For i = 1 To n sum = sum + nFactor(i) -3- Next iForm1.Print sum= & sum -4-End Sub20 find过程是用于从1到10000中找出这样的数,该数各个位的数字的阶乘相加之和等于该数,并将结果输出。Option ExplicitPublic Sub find() Dim k, a, n, i Dim p As Integer For k = 1 To 10000 a = LTrim(Str(k) n = 0 For i = 1 To Len(a) -1- p = Val(Mid(a, i, 1) n = n + fact(p) -2- Next i If n = k Then Form1.Print k Next kEnd Sub Function fact(x As Integer) As Long 该函数用于计算阶乘 Dim y As Long Dim i% y = 1 For i% = 1 To x y = y * i% Next i% fact = y -3-End Function21 Modify.bas模块中的Combination过程是用于计算m个数据中取出n个数据的排列组合值,计算公式为Cmn=m!/(n!*(m-n)!)。Modify.bas模块中的n过程是产生数据,数据的取值范围为-1010之间的随机数。Public Sub Combination() Dim m As Integer Dim n As Integer Dim Cmn As Long Do m = Val(InputBox(请输入一个整数m) n = Val(InputBox(请输入一个整数n(n= n 必须保证输入的两个数m=n Loop While m n * 错误2 * Cmn = nFactor(m) / (nFactor(n) * nFactor(m - n) Form1.Print 排列组合数为; CmnEnd SubPublic Function nFactor(ByVal n As Integer) As Double Dim i As Integer Dim temp As Double temp = 1 For i = 1 To n temp = temp * i Next i *错误 3 * nFactor(n) = temp nFactor = temp * 错误4 *End Function22 过程Uppersen实现从键盘上任意输入一条英文句子,将句子中的每个单词的首字母都变成大写例如:输入I am a good student. 要求输出 I Am A Good Student.Public Sub Uppersen() Dim oldsen As String, newsen As String Dim char As String, lastchar As String Dim l As Integer, i As Integer oldsen = InputBox(请输入英文句子:) l = Len(oldsen) -1- 以空格作为单词的界定,空格后的字母转换为大写字母 lastchar = -2-上一次取的字符 For i = 1 To l char = Mid(oldsen, i, 1) -3-这一次取的字符 If lastchar = Then char = UCase(char) -4- End If newsen = newsen & char lastchar = char Next i Form1.Print input:; oldsen Form1.Print output:; newsenEnd Sub23 餐厅发工资程序.Modify.bas模块中的Money过程是用于统计一个有7个工作人员的餐厅发工资所需的100元、50元、10元、5元和1元的票面数.Modify.bas模块中的SalaryData过程是给出7个员工的工资?Dim salary(7) As IntegerPublic Sub Money() Dim hundred As Integer, totalhundred As Integer 100元票面数量、总数量 Dim fifty As Integer, totalfifty As Integer 50元票面数量、总数量 Dim ten As Integer, totalten As Integer 10元票面数量、总数量 Dim five As Integer, totalfive As Integer 5元票面数量、总数量 Dim one As Integer, totalone As Integer 1元票面数量、总数量 Dim totalsalary As Integer 工资总计 Dim i As Integer, temp As Integer totalhundred = 0 totalfifty = 0 totalten = 0 totalfive = 0 totalone = 0 totalsalary = 0 For i = 1 To 7 temp = salary(i) -1- hundred = Int(temp / 100) temp = temp - hundred * 100 -2- fifty = Int(temp / 50) temp = temp - fifty * 50 ten = Int(temp / 10) temp = temp - ten * 10 five = Int(temp / 5) temp = temp - five * 5 one = temp -3- totalhundred = totalhundred + hundred totalfifty = totalfifty + fifty totalten = totalten + ten totalfive = totalfive + five totalone = totalone + one totalsalary = totalsalary + salary(i) -4- Next i Form1.Print 共需100元 & Str(totalhundred) & 张 Form1.Print 共需50元 & Str(totalfifty) & 张 Form1.Print 共需10元 & Str(totalten) & 张 Form1.Print 共需5元 & Str(totalfive) & 张 Form1.Print 共需1元 & Str(totalone) & 张 Form1.Print 合计 & Str(totalsalary) & 元End SubPublic Sub SalaryData() salary(1) = 1398 salary(2) = 1765 salary(3) = 689 salary(4) = 1500 salary(5) = 832 salary(6) = 590 salary(7) = 1200End Sub24 Modify.bas模块中的wrap过程用于判断一个字符串是否回文。所谓回文是指字符串顺读与倒读都是一样的,如潮起潮落,落潮起潮。Public Sub Wrap()Dim length As Integer Dim str1 As String Dim strleft As String Dim strright As String Dim k As Integer str1 = InputBox(请输入任意的字符串) 输入任意字符串 *错误1* length = Len(str1) length = Val(str1) k = 1 Do *错误2* strleft = Mid(str1, k, 1) 从左边起逐个取出一个字符 strleft = Left(str1, k) *错误3* strright = Mid(str1, length - k + 1, 1) 从右边起逐个取出一个字符 strright = Right(str1, k) *错误4* If strleft strright Then If strleft = strright Then Exit Do End If k = k + 1 Loop While k length / 2 Then Form1.Print str1 & 是回文 Else Form1.Print str1 & 不是回文 End IfEnd Sub25 Modify.bas模块中的Guess过程是猜数游戏,由计算机产生一个1,100的任意整数,输入猜数后计算机给出提示,如果5次后还没有猜中就结束游戏并公布正确答案,请改正其中的错误。说明:只要修改标出出错位置的下面那条语句。Public Sub Guess() Dim R As Integer Dim X As Integer Dim times As Integer Randomize -1- R = Rnd * 100 R = Int(Rnd * 100) + 1 产生一个1-100的任意整数 times = 1 Form1.Print R Do X = Val(InputBox(输入猜数X) Select Case X Case R Form1.Print 猜中了 Exit Do -2- Case X R Case Is R Form1.Print 太大了,继续猜! Case Else Form1.Print 太小了,继续猜! End Select times = times + 1 -3- Loop While times 5 Loop While times 5 Then Form1.Print 猜数失败,游戏结束! -4- Form1.Print 正确答案为 & Str(R) End IfEnd Sub26 Modify.bas模块中的Findat过程是用于在一个字符串变量中查找at,并用消息框给出查找结果的报告:没有找到或找到的个数。Public Sub Findat() 在字符串str1中查找at Dim str1 As String Dim length As Integer 字符串长度 Dim sum As Integer 查到的个数 Dim i As Integer str1 = InputBox(请输入一个字符串) length = Len(str1) -1- i = 1 sum = 0 Do While i 0 Then 找到了指定的字符串 num = num + 1 次数加1 i = i + pos - 1 -4- 继续向前找 Else Exit For 没找到,退出 End If Next i matchCount = numEnd Function28 ComMulti过程是求任意两个正整数的最小公倍数,求最小公倍数的一

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论