VB_编程练习(答案).doc_第1页
VB_编程练习(答案).doc_第2页
VB_编程练习(答案).doc_第3页
VB_编程练习(答案).doc_第4页
VB_编程练习(答案).doc_第5页
已阅读5页,还剩11页未读 继续免费阅读

下载本文档

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

文档简介

一.选择结构-题目:编写函数fun,函数的功能是:根据一个百分制 成绩mark(整数),显示对应五级制的评定。条件如下: mark大于等于90显示优秀 mark小于90且大于等于80显示良好 mark小于80且大于等于70显示中等 mark小于70且大于等于60显示及格 mark小于60显示不及格 要求使用IF语句来实现。-Private Function fun(mark As Integer) As String*Program*If mark = 90 Then fun = 优秀ElseIf mark = 80 Then fun = 良好ElseIf mark = 70 Then fun = 中等ElseIf mark = 60 Then fun = 及格Else fun = 不及格End If* End *End FunctionPrivate Sub Form_Load() Show Print fun(90)End Sub-题目:编写函数fun,函数的功能是:判断一个字符是: 字母字符、数字字符还是其他字符,并做相应的显示。 字母字符显示字符串字母 数字字符显示字符串数字 其他字符显示字符串其他 要求使用select语句来实现-Private Function fun(n As String) As String*Program*Select Case nCase A To Z, a To z fun = 字母Case 0 To 9 fun = 数字Case Else fun = 其他End Select* End *End FunctionPrivate Sub Form_Load() Show Print fun(A) Print fun(9) Print fun(&)End Sub二.一般循环-题目:(事件)双击窗体。 (响应)求出100200之间,能被5整除,但不能被3整除的数。输出这些数并求所有数之和,存入变量中SUM中。 输出sum 应为 2100-Private Sub Form_dblClick()Dim sum As Integersum = 0* Program *Dim i As IntegerFor i = 100 To 200 If (i Mod 5) = 0) And (i Mod 3) 0) Then Print (CStr(i) sum = sum + i End IfNext* End *Print sumEnd Sub-题目:(事件)单击窗体。 输出L 应为 46189 (响应)求1020之间所有素数的乘积并输出在窗体上。将结果存入变量L中。-Private Sub Form_Click()Dim l As Long* Program * Dim i As Integer Dim j As Integer Dim b As Boolean l = 1 b = False For i = 10 To 20 For j = 2 To i - 2 If i Mod j = 0 Then b = True End If Next If b = False Then l = l * i End If b = False Next Print Str(l)* End *End Sub-题目:(事件)单击窗体。 (响应)编写程序求101112+111213+151617的结果, 并将结果输出到窗体上。将结果存入变量M中 输出M 应为 15390-Private Sub Form_Click()Dim m As Long* Program *Dim i, j As IntegerDim s As LongFor i = 10 To 15 s = 1 For j = i To i + 2 s = s * j Next m = m + sNextPrint (CStr(m)* End *End Sub-题目:(事件)单击窗体。 (响应)在窗体上打印数列2/1,3/2,5/3,8/5,13/8 的前10项,并求其和, 将和保存在变量s中 输出:-Private Sub Form_Click()Dim s As Single* Program *Dim i, j, k, m As Integers = 0i = 2j = 1For k = 1 To 10 s = s + i / j m = j Print (CStr(i) & / & CStr(j) j = i i = i + mNext* End *End Sub-题目:编写函数fun其功能是:根据整型形参m,计算如下公式的值: y=11/31/51/7.1/(2m+1) 例如:若m=9,则应输出:2.133255-Private Function fun(m As Integer) As Single Dim y As Double* Program * Dim y As Double Dim i As Integer y = 1 For i = 1 To m y = y + 1 / (2 * i + 1) Next fun = y* End *End FunctionPrivate Sub Form_Load() Show Print fun(9)End Sub-题目:函数fun其功能是:根据整型形参m,计算如下公式的值:y=1/21/41/6.1/2m 例如:若m=9,则应输出:1.41448412698413-Private Function fun(m As Integer) As Double* Program * Dim y As Double Dim i As Integer For i = 1 To m y = y + 1 / (2 * i) Next fun = y* End *End FunctionPrivate Sub Form_Load() Show Print fun(9)End Sub-题目: (事件)单击窗体。 (响应)已知sum=1-1/4+1/9-1/16+1/25.-1/100, 输出 sum 应为 0.8179622 请将sum结果值输出到窗体上。 使用for.next语句完成程序-Private Sub Form_Click() Dim sum As Single* Program *Dim i As Integerfor i = 1 to 10sum = sum + (-1) (i - 1) /(i*i)nextPrint sum * End *End Sub三.级数-题目: (事件)单击窗体。 (响应)已知sum=1/2!-1/4!+1/6!-1/8!+1/10!,请将sum结果值输出到窗体上。 使用do while.loop语句完成程序 输出sum 应为 0.4596977-Private Sub Form_Click() Dim p As Long p变量存放阶乘的值 Dim sum As Single* Program *Dim i As IntegerDim n As Integeri = 1p = 1Do While i = 10 p = p * iIf i Mod 2 = 0 Then n = n + 1 sum = sum + (-1) (n - 1) / pEnd If i = i + 1LoopPrint sum* End *End Sub-题目:(事件)单击窗体。 (响应)已知S=1+1/4+1/9+1/16+1/25+1/100,结果输出在窗体上。输出s 应为 1.291286-Private Sub Form_Click()Dim s As Single* Program *Dim i As Integers = 0For i = 1 To 10 s = s + 1 / (i i)NextPrint (s)* End *End Sub-题目: (事件)单击窗体。 (响应)求sum=1+1+2+3+5+8+13+.+144的和,请将sum结果值输出到窗体上。使用 do.loop语句完成程序 输出sum 应为 376-Private Sub Form_Click() Dim sum As Long* Program *Dim i As IntegerDim s1 As IntegerDim s2 As Integeri = 1s1 = 1: s2 = 1Sum = Sum + s1 + s2Do If s2 = 144 Then Exit Do End If s1 = s1 + s2 s2 = s1 + s2 Sum = Sum + s1 + s2 LoopPrint Sum* End *End Sub-题目:编写函数fun其功能是:根据整型形参m,计算如下公式的值: y=1/5+1/6+1/7+1/8+1/9+1/10.+1/(m+5) 例如:若m=9,则应输出:1.168229要求:函数中用到的中间变量必须声明为Single类型。-Private Function Fun(m As Integer) As Single* Program * Dim y As Double Dim i As Integer For i = 0 To m y = y + 1 / (i + 5) Next Fun = y* End *End FunctionPrivate Sub Form_Load() Show Print Fun(9)End Sub四.数字关系-题目:编写函数fun其功能是: 判断一个整数w的各位数字平方之和能否被5整除 , 可以被5整除则返回1 , 否则返回0-Private Function fun(w As Integer) As Boolean* Program * Dim k As Integer, s As Integer Do s = s + (w Mod 10) * (w Mod 10) w = Int(w / 10) Loop While w 0 If s Mod 5 = 0 Then k = 1 Else k = 0 End If fun = k* End *End FunctionPrivate Sub Form_Load() Show Print fun(50) 输出True Print fun(51) 输出FalseEnd Sub-题目:(事件)双击窗体。 (响应)写程序解决百钱买百鸡问题:公鸡3元1只,母鸡5元1只,小鸡一元3只,怎样用100元买100只鸡。把结果输出到窗体上。将答案数存放在变量N中。 输出- Private Sub Form_dblClick()Dim n As Integern = 0* Program *Dim i, j, k As IntegerFor i = 1 To 20 母鸡 For j = 1 To 33 公鸡 For k = 3 To 100 小鸡 If (k Mod 3) = 0) And (i * 5 + j * 3 + k / 3) = 100) And (i + j + k) = 100) Then Print (CStr(i) Print (CStr(j) Print (CStr(k) n = n + 1 End If Next NextNext* End *End Sub-题目: (事件)单击窗体。 (响应)输出所有的三位数,它满足该数中有某两位为相同的数, 且该数是一个完全平方数。 求出所有这些数的和,并将结果存入变量SUM中。 输出-Private Sub Form_Click() Dim sum As Integer* Program *dim a as integer,b as integerdim b3 as integer,b2 as integer,b1 as integerfor a=10 to 31b=a*ab3=b100b2=(b10) mod 10b1=b mod 10if b1=b2 or b1=b3 or b2=b3 then? a,bsum=sum+bend ifnext aPrint sum* End *End Sub-题目:(事件)单击窗体。 (响应)求出1000-9999之间具有如下特点的四位数字, 它的平方根恰好就是它中间的两位数字,例如,2500开平方为50,恰为2500的中间两位, 输出 找出所有这样的四位数。并求出所有这样的数的和存入SUM中-Private Sub Form_Click()Dim sum As Integersum = 0* Program *Dim i As Integer Dim j As Integer For i = 1000 To 9999 j = Val(Mid(Trim(Str(i), 2, 2) If j 2 = i Then Print i, sum = sum + i End If Next* End *End Sub-题目: (事件)单击窗体。 (响应)求一个数,它除3余2,除5余3,除7余2, 请将满足上面条件的最小数保存到sum变量中。 输出 sum 应为23 使用for.next语句完成程序-Private Sub Form_Click() Dim sum As Integer* Program *Dim i As IntegerFor i = 2 To 1000 If i Mod 3 = 2 And i Mod 5 = 3 And i Mod 7 = 2 Then Print i Exit For End IfNext isum = i* End *End Sub五.递推-题目:编写函数fun,函数的功能是:当x的初值为10,每年增长率为百分之一,计算多少年以后x的值能达到y,并显示所需年数的值(变量名必须为n)。 要求使用Do . Loop Until语句来实现。 当y为15时, n应该为41-Private Function fun(y As Single) As LongDim x As Single, n As Long*Program*x = 10n = 0Do While x y x = x * 1.008 n = n + 1Loopfun = n* End *End FunctionPrivate Sub Form_Load() Show Print fun(15)End Sub-题目:已知,猴子吃一堆桃子,每天吃桃子总数的一半多一个。到第n天时,桃子只剩一个。编写函数fun,函数的功能是: 求出开始桃子的数量并显示。如: n为7时,则显示190要求使用For语句来实现。 要求使用Do Until . Loop语句来实现-Private Function fun(n As Long) As String*Program*x = 1i = 1Do Until i = n x = (x + 1) * 2 i = i + 1Loopfun = x* End *End FunctionPrivate Sub Form_Load() Show Print fun(7)End Sub-题目:用迭代法求某个数的平方根。 已知求平方根的迭代公式如图:算法思想:对于一个问题的求解x,可由给定的一个初值x0,根据某一 迭代公式得到一个新的值x1,这个新值x1比初值x0更接近要求的值x; 再以新值作为初值,即:x1x0,重新按原来的方法求x1,重复这一过和 直到|x1-x0| 0.00001 Fsqrt = x1* End *End FunctionPrivate Sub Form_Load() Show Print Fsqrt(45) 当a为45 时应输出6.708204End Sub六.数组-题目:求二维数组中最大元素及其所在的行和列并将最大值存入变量Max中,将最大值的行位置存入row中,列位置存入变量column中. 输出:-Private Sub Form_Load() Show Dim a(2, 3) As Integer Dim max As Integer, row As Integer, column As Integer a(1, 1) = 34: a(1, 2) = 34: a(1, 3) = 43: a(2, 1) = 34: a(2, 2) = 78: a(2, 3) = 12* Program * Dim i As Integer, j As Integer max = a(1, 1): row = 1: column = 1 For i = 1 To 2 For j = 1 To 3 If a(i, j) a(row, column) Then max = a(i, j) : row = I : column = j End If Next j Next i Print 最大元素是; max Print 在第 & row & 行,; 第 & column & 列* End *End Sub-题目:应用选择法对数组A按升序排列基本思想: 1)对有n个数的序列(存放在数组a(n)中),从中选出最小的数,与第1个数交换位置; 2)除第1 个数外,其余n-1个数中选最小的数,与第2个数交换位置; 3)依次类推,选择了n-1次后,这个数列已按升序排列。 输出排序前后数组数据,验证结果-Private Sub Sort(ByRef a() As Integer, n As Integer)* Program * Dim i, j, imin, temp As Integer For i = 1 To n - 1 imin = i For j = i + 1 To n If a(imin) a(j) Then imin = j Next j temp = a(i) a(i) = a(imin) a(imin) = temp Next i* End *End SubPrivate Sub Form_Load() Show Dim i As Integer Dim arr(10) As Integer Randomize Print 排序前:For i = 1 To 10 arr(i) = Int(10 * Rnd + 1) : Print arr(i); Next Print Sort arr, 10 Print 排序后: For i = 1 To 10 Print arr(i); NextEnd Sub-题目:(事件)单击窗体。 (响应)生成一个一维数组(10个数组元素依此为:15、23、72、43、96、23、3、65、88、17), 写程序求出这个数组中的最大值、最小值和平均值,并输出在窗体上。 将最大值,最小值,平均值分别存入变量Max,Min,Aver中 输出Max为96,Min为3,Aver为44-Private Sub Form_Click()Dim max, min, aver As Integer* Program *Dim a()Dim i, s As Integera = Array(15, 23, 72, 43, 96, 23, 3, 65, 88, 17)max = a(0)min = a(0)s = 0For i = 0 To 9 If a(i) max Then max = a(i) End If If a(i) min Then min = a(i) End Ifs = s + a(i)Nextaver = Int(s / 10)Print max, min, aver* End *End Sub七.杂项-题目:对字符串进行加密处理。 加密过程: 将每个字母C减一序数K5,即 c=chr(Asc(c)- k),这时 ZU, zu,YT 当加序数后的字母小于A或a则 c=Chr(Asc(c)-k +26) 输出:-Private Function EnyStr(strI As String) As String* Program * Dim i As Integer Dim strp As String, strT As String, iA As String Dim nl As String i = 1: strp = nl = Len(RTrim(strI) Do While (i = A And strT Asc(Z) Then iA = iA - 26 strp = strp + Chr$(iA) ElseIf (strT = a And strT Asc(z) Then iA = iA - 26 strp = strp + Chr$(iA) Else strp = strp + strT End If i = i + 1 Loop EnyStr = strp* End *End FunctionPrivate Sub Form_Load() Show Print EnyStr(abc def xyz)End Sub-题目:编写函数fun,函数的功能是:计算两个正整数的最大公约数,并显示。如:两个数为15、20,结果显示5 要求使用Do . Loop语句来实现。-Private Function fun(m As Integer, n As Integer) As IntegerDim r%If m n Then r = m: m = n: n = r*Program*Do r = m Mod n If r = 0 Then Exit Do m = n n = rLoopfun = n* End *End FunctionPrivate Sub Form_Load() Show Print fun(15, 10)End Sub-题目:(事件)单击窗体。 (响应)一球从100米高度自由落下,每次落地返回原高度的一半,求第10次落地时,共经过多少米?并将结果在窗体上输出。 输出s应为299.6094-Private Sub Form_Click()Dim s As Single* Program *Dim j, i As Integers = 0j = 100For i = 2 To 10 j = j / 2 s = s + 2 * jNexts = 100 + sPrint s* End *End Sub-题目:编写函数fun,函数的功能是:计算并输出给定整数n的所有因子之和(不包括1与自身)。规定n的值不大于1000? 例如:n的值为855时,应输出704。-Private

温馨提示

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

评论

0/150

提交评论