




已阅读5页,还剩21页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
V习题解答习题二Xt2.9编写应用程序,读入一行字符,统计其中有字母、数字、空格和其它字符各有几个。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim str1, str2 As String Dim i, n, sum1, sum2, sum3, sum4 As Integer str1 = InputBox(enter a string) n = Len(str1) For i = 1 To n str2 = Mid(str1, i, 1) Select Case str2 Case a To z, A To Z sum1 = sum1 + 1 Case 0 To 9 sum2 = sum2 + 1 Case sum3 = sum3 + 1 Case Else sum4 = sum4 + 1 End Select Next MsgBox(字母 & sum1 & 数字 & sum2 & Chr(10) & 空格 & sum3 & 其它 & sum4) End SubXt2.10编写应用程序,统计并逐行显示(每行5个数)在区间10000,50000上的回文数。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i, j, k As Integer Dim a, b, c, d, f As Integer Dim str1 As String k = 0 For i = 10000 To 50000 a = i Mod 10 b = (i Mod 100) 10 c = (i Mod 1000) 100 d = (i Mod 10000) 1000f = i 10000 If i = a * 10000 + b * 1000 + c * 100 + d * 10 + f Then str1 = str1 + Str(i) + Space(3) k = k + 1 If k Mod 5 = 0 Then str1 = str1 + Chr(10) + Chr(13) End If End If Next MsgBox(str1 + Chr(10) + Str(k)End SubXt2.15使用While语句,求两个正整数的最小公倍数。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a, b, gbs As Integer a = Val(InputBox(enter a=?) b = Val(InputBox(enter b=?) If a = b Then gbs = b Else gbs = a End If While (gbs Mod a 0) Or (gbs Mod b 0) gbs = gbs + 1 End While MsgBox(正整数 & a & 和 & b & 的最小公倍数为: & gbs) End Sub习题三Xt3.1- 0.6326531 编写求的函数过程,调用此函数求以下表达式的值。y=Function sum(ByVal n As Integer) As Integer Dim i, s As Integer s = 0 For i = 1 To n s = s + i Next sum = s End FunctionPrivate Sub Button1_Click( ) Dim y As Double y = (sum(3) + sum(4) + sum(5) / (sum(6) + sum(7) MsgBox(y= & y)End Subxt3.2 编写一个求3个数中最大值Max和最小值Min的过程。然后用这个过程分别求3个数和5个数、7个数中的最大值和最小值。 求三个数的最大值的过程 Function max(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) Dim m As Integer If a b Then m = a Else m = b End If If m c Then max = m Else max = c End If End Function 求三个数的最小值的过程 Function min(ByVal a As Integer, ByVal b As Integer, ByVal c As Integer) Dim m As Integer If a b Then m = a Else m = b End If If m m Then MsgBox(输入数据错误) End If cmn = jcf(m) / (jcf(n) * jcf(m - n) MsgBox(组合数是: & cmn)End Subxt3.5 斐波纳契(Fibonacci)数列的第一项是0,第二项是1,以后各项都是前两项的和。试用递归算法和非递归法各编写一个程序,求斐波纳契数列第N项的值. function fib1(ByVal n As Integer) 非递归算法-(递推) Dim f1, f2, f3 As Integer Dim i As Integer f1 =0 : f2 = 1 For i = 3 To n F3 = f1 + f2 第i项的值 f1 = f2 f2 = f3 Next Fib1=f3 End Sub Function fib2(ByVal n As Integer) 递归算法 If n = 1 Then fib2 = 0 If n = 2 Then fib2 = 1 If n=3 Then fib2 = fib2(n - 1) + fib2(n - 2) End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim n As Integer n = Val(InputBox(n=?) MsgBox(用递推算法,数列第 & n & 项的值为 & fib1(n)MsgBox(用递归算法,数列第 & n & 项的值为 & fib2(n) End Subxt3.6 编写八进制数与十进制数相互转换的过程:(1)过程ReadOctal,读入八进制数,转换为等值的十进制数。(2)过程WriteOctal,将十进制正整数以等值的八进制形式输出。 oct_num 转int_num (8 转10) Function readoctal(ByVal oct_num As String) As Single Dim n, i, t, s As Integer s = 0 n = Len(oct_num) For i = 1 To n t = Val(Mid(oct_num, i, 1) * 8 (n - i) 各位按权位求值 s = s + t Next readoctal = s End Function int_num 转 oct_num (10 转 8) Function writeoctal(ByVal int_num As String) As Single Dim n, i, t As Integer, s As String Dim octstr(6) As String For i = 6 To 1 Step -1 octstr(i) = int_num - Int(int_num / 8) * 8 连除基数 int_num = Int(int_num / 8) Next For i = 1 To 6 s = s + (octstr(i) 逆序取余 Next writeoctal = Val(s) End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a, b As Integer a = Val(TextBox1.Text) b = writeoctal(a) 调用函数过程 TextBox2.Text = Str(b) End Sub Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim a, b As Integer a = TextBox3.Text b = readoctal(a) 调用函数过程 TextBox4.Text = Str(b) End Subxt3.7 编写一个过程,用来计算并输出 S=1+ 的值。 Function sumd(ByVal n As Integer) As Single Dim i%, s! s = 1 : i = 1 Do i = i + 1 s = s + 1 / i Loop Until i = n sumd = s End Function Function sumf(ByVal n As Integer) As Single Dim i%, s! s = 0 For i = 1 To n s = s + 1 / i Next sumf = s End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(sumd= & sumd(100) MsgBox(sumf= & sumf(100)End Sub-5.187378xt3.8 编写过程,用下面的公式计算的近似值: =1-+-+(-1)n-1在事件过程中调用该过程,并输出当n=100、500、1000、5000时的近似值。Function pi(ByVal n As Integer) As Single Dim i As Integer, s As Single s = 0 For i = 1 To n s = s + (-1) (i - 1) / (2 * i - 1) Next pi = 4 * s End Function Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click MsgBox(n=100,pi= & pi(100) MsgBox(n=500,pi= & pi(500) MsgBox(n=1000,pi= & pi(1000) MsgBox(n=5000,pi= & pi(5000) End Sub xt3.9 参考lt3.6 已知 sinh(x)=从键盘输入x的值,输出sinh(x),其中:ex可用以下级数近似表示: ex1+x+= 精度为 = eps) s = s + t t = t * x / n n = n + 1 Loop jishu1 = s End Function 用通用过程实现求部分级数和 Sub jishu2(ByRef s#, ByVal x!, ByVal eps#) Dim n%, t# n = 1 : s = 0 : t = 1 Do While (Abs(t) = eps) s = s + t t = t * x / n n = n + 1 Loop End Sub 主程序调用函数过程和通用过程 Private Sub Form1_Load(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles MyBase.Load Dim f1#, f2# f1 = jishu1(2.0#, 0.000001) 调用函数过程 Call jishu2(f2, 2.0#, 0.000001) 调用通用过程 或jishu2(f2,2#,0.000001) MsgBox(f1= & Str(f1) & & f2= & Str(f2)End Sub习题四Xt4.4 从键盘输入10个整数,并放入一个一维数组,将其前5个元素与后5个元素对换,即第一个元素与第十个元素互换,第二个元素与第九个元素互换第五个元素与第六个元素互换。分别输出数组原来各元素的值和对换后各元素的值。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(9) As Integer Dim i, t As Integer, str1 As String str1 = 原来数组: + Chr(10) + Chr(13) For i = 0 To 9a(i)=val(inputbox(“请输入a数组各元素的值”) str1 = str1 + Space(4) + Str(a(i) 输出原来数组元素 Next i str1 = str1 + Chr(10) + Chr(13) For i = 0 To 4 t = a(i) a(i) = a(9 - i) 对换数组元素 a(9 - i) = t Next i str1 = str1 + 对换后数组: + Chr(10) + Chr(13) For i = 0 To 9 str1 = str1 + Space(4) + Str(a(i) 输出对换后数组元素 Next i str1 = str1 + Chr(10) + Chr(13) MsgBox(str1) End SubXt4.5一维数组相加设有如下两组数据:A:2,8,7,6,4,28,70,25B:79,27,32,41,57,66,78,80编写一个程序,把上面两组数据分别读入两个数组。然后把两个数组中对应下标的元素相加,即2+79,8+27,25+80,并把相应的结果放入第三个数组,最后输出第三个数组的值。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a() As Integer =2,8,7,6,4,28,70,25Dim b() As Integer =79,27,32,41,57,66,78,80Dim c(7) As Integer Dim i As Integer, str1, str2, str3 As String str1 = a数组为: str2 = b数组为: str3 = c数组为: For i = 0 To 9c(i)=a(i)+b(i)str1 = str1 + Space(4) + Str(a(i) 输出a数组元素str2 = str2 + Space(4) + Str(b(i) 输出b数组元素str3 = str3 + Space(4) + Str(c(i) 输出c数组元素 Next MsgBox(str1+chr(10)+str2+chr(10)+str3) End SubXt4.6矩阵最大元素查找有一个nm的矩阵,编写程序,找出其中最大的元素所在的行和列,并输出其值及行号和列号。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(,) As Integer Dim n,m,I,j as integer,str1 as stringDim max,row,col As Integer n=val(inputbox(“请输入矩阵行数n=”)m=val(inputbox(“请输入矩阵列数m=”) redim a(n-1,m-1) For i = 0 To n-1For j=0 to m-1 a(I,j)=val(inputbox(“请输入”&I&”行”&j&”列元素的值”)str1 = str1+ Str(a(I,j) + Space(4) 输出原来数组元素next jstr1 = str1 + Chr(10) + Chr(13) Next i Msgbox(str1) 显示输入的矩阵 Max=a(0,0) For i = 0 To n-1For j=0 to m-1If a(I,j)max thenMax=a(I,j)Row=ICol=jEnd ifnext j Next i Msgbox(“矩阵最大元素的值为:”&max&chr(10)+”它所在行号为:”&row+chr(10)+”列号为”&col) End Subxt4.7矩阵输入/输出,行/列求和,元素对换编写程序,把下面的数据输入一个二维数组: 25 36 78 13 12 26 88 93 75 18 22 32 56 44 36 58然后执行以下操作:(1)输出矩阵两个对角线上的数。(2)分别输出各行和各列的和。(3)交换第一行和第三行的位置。(4)交换第二列和第四列的位置。(5)输出处理后的数组。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(,) As Integer = 25, 36, 78, 13, 12, 26, 88, 93, 75, 18, 22, 32, 56, 44, 36, 58 Dim rows(3), cols(3) As Integer Dim I, j, t As Integer Dim str1, str2, str3 As String str1 = str2 = 输出主,付对角线元素 For I = 0 To 3 For j = 0 To 3 If I = j Then str1 = str1 + Str(a(I, j) + Space(4) If I + j = 3 Then str2 = str2 + Str(a(I, j) + Space(4) Next j Next I MsgBox(主对角线元素: & str1 & Chr(10) & 付对角线元素: & str2) 输出各行、列的和 str1 = str2 = For I = 0 To 3 For j = 0 To 3 rows(I) = rows(I) + a(I, j) i为行循环变量, 求各行的和 cols(I) = cols(I) + a(j, I) i为列循环变量, 求各列的和 Next j str1 = str1 + Space(4) + Str(rows(I) str2 = str2 + Space(4) + Str(cols(I) Next I MsgBox(各行的和: & str1 & Chr(10) & 各列的和: & str2) 交换第一行和第三行的位置 For I = 0 To 3 i为列循环变量 t = a(0, I) a(0, I) = a(2, I) a(2, I) = t Next 交换第二列和第四列的位置 For I = 0 To 3 i为行循环变量 t = a(I, 1) a(I, 1) = a(I, 3) a(I, 3) = t Next 输出处理后的数组 For I = 0 To 3 For j = 0 To 3 str3 = str3+ Str(a(I, j) + Space(4) Next j str3 = str3 + Chr(10) Next I MsgBox(str3)End Subxt4.8编写程序,建立并输出一个1010的矩阵,该矩阵对角线元素为1,其余元素均为0。矩阵对角线Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(9,9) As Integer Dim i, j as integer, str1 as string For i = 0 To 9For j=0 to 9If i=j or i+j=9 thena(I,j)=1Elsea(i,j)=0End ifstr1 = str1 +space(4)+str(a(i,j)next j str1 = str1 + Chr(10) + Chr(13) Next i Msgbox(str1) End Subxt4.9矩阵转置编写程序,实现矩阵转置,即将一个nm的矩阵的行和列互换。例如,a矩阵为 1 2 3 a = 4 5 6 转置后的矩阵b为 1 4 b = 2 5 3 6Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(,) ,b(,) As Integer Dim n, m, i, j as integer, str1 as stringn=val(inputbox(“请输入矩阵行数n=”)m=val(inputbox(“请输入矩阵列数m=”) redim a(n-1,m-1)redim b(m-1,n-1)str1=”原数组为:”+chr(10) For i = 0 To n-1For j=0 to m-1a(i,j)=val(inputbox(“输入数组元素:”)str1=str1+space(4)+str(a(I,j)next j str1 = str1 + Chr(10) + Chr(13) Next i str1=str1+”转置后的数组为:”+chr(10)For i = 0 To m-1For j=0 to n-1b(i,j)=a(j,I)str1=str1+space(4)+str(b(I,j)next j str1 = str1 + Chr(10) + Chr(13) Next i Msgbox(str1) End SubXt4.10 字符串的查找与排序编写应用程序,要求在TextBox控件的文本框中输入一个英文句子后完成如下操作:(1) 分析并显示TextBox控件的Text值中含有几个字母“a”。(2) 若英文句子用“.”号结束,英文单词间用空格字符和逗号字符作为分隔字符,分析并显示TextBox控件的Text值中含有几个英文单词。(3) 把找到的单词按从小到大的次序排序后显示到另一个TextBox控件的文本框中。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i, n, m, k, p As Integer Dim x, str1, t As String Dim a() As String x = TextBox1.Text 输入英文句子 n = Len(x) 判断英文句子的长度 For i = 1 To n If Mid(x, i, 1) = a Then 查找a字符的个数 m = m + 1 End If Next str1 = str1 + 句子中含有: & m & 个字符a & Chr(10) ReDim a(n) k = 1 For i = 1 To n a(k) = a(k) + Mid(x, i, 1) 字符连成单词 If Mid(x, i, 1) = Or Mid(x, i, 1) = , Then k = k + 1 对单词计数End If If Mid(x, i, 1) = . Then Exit For End If Next p = 1 对单词-冒泡排序 While p = 1 p = 0 For i = 1 To k - 1 If a(i) a(i + 1) Then t = a(i) a(i) = a(i + 1) a(i + 1) = t p = 1 End If Next End While For i = 1 To k 输出排序后的单词 str1 = str1 + Space(1) + a(i) If i Mod 10 = 0 Then str1 = str1 + Chr(10) End If Next MsgBox(str1) End Subxt4.11利用InputBox函数为一维数组赋值,并在输入过程中利用插入排序算法完成数组内容的排序操作。Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim a(),x As Integer Dim i, j, n as integer, str1,str2 as stringn=val(inputbox(“输入数组中数据个数”)Redim a(n-1)i=0str1=str(a(0) For i = 0 To n-1a(i)= val(inputbox(“输入第”+str(I+1)+” 个数据”)str1=str1+str(a(i)x=a(i)For j=i-1 to 0 step-1If xa(j) thena(j+1)=a(j)ElseExit forEnd ifnext j a(j+1)=xNext i For i=0 to n-1Str2=str2+str(a(i)next Msgbox(“原数组为”&chr(10)&str1&”从小到大排序后为”&chr(1
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 普通话教学课件效果
- 2025年公务员面试国际发展合作问题与答案
- 2025年安全员应急响应试题
- 2025年安全生产安全应急处理题库及答案
- 同坡屋面画法教学课件
- 2025年工程管理试题及答案解析
- 2025年应急救援员面试精讲题库
- 2025年瑜伽教练进阶实战模拟题与答案详解
- 2025年电子商务运营管理师考试试题及答案解析
- 机电专业知识培训心得课件
- 《全球哮喘管理和预防策略(GINA 2025)》解读
- 2025四川农商联合银行信息科技部社会招聘笔试历年典型考题及考点剖析附带答案详解
- 行为金融学案例
- 万科集团财务管理制度手册207
- “李可中医药学术流派论治厥阴病”-课件
- 通用技术作品设计报告
- 锚杆支护技术规范正式版本
- 隐形眼镜经营管理制度
- 下一代互联网技术
- 皮肤知识与问题性皮肤分析(入行必看)
- 单位消防安全评估报告(模板)
评论
0/150
提交评论