原创vb教程答案_第1页
原创vb教程答案_第2页
原创vb教程答案_第3页
原创vb教程答案_第4页
原创vb教程答案_第5页
已阅读5页,还剩14页未读 继续免费阅读

下载本文档

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

文档简介

1、原创vb教程答案第七章1(略2(略3(略4(1) BASIC(2) 6 262 62-2 98(3) 2347(4) 10 812 -105 212 105(编写一个摄氏温度与华氏温度转换的通用过程。摄氏温度(C)与华氏温度(F) 转换的公式如下:F二CX9?5, 32Private Sub Command1_C1ick()Dim c As Single, f As Singlec = Textl.TextCall zhh(c, f)Text2. Text = fPrivate Sub zhh(cs As Single, fs As Single)fs = cs * 9 / 5 + 32End

2、 Sub6(编写一个验证一个数是否是素数的通用过程。Private Sub Form_Click()Dim n As Integer, flag As Booleann = InputBoxC输入一个整数"判断素数)Call sushu(n, flag)If flag ThenMsgBox Str(n) + "是素数"ElseMsgBox Str(n) + "不是素数"End IfEnd SubPrivate Sub sushu(m As Integer, f As Boolean)Dim i As IntegerFor i = 2 To In

3、t(m / 2)If m Mod i = 0 Then Exit For Next iIf i = Int(m / 2) + 1 Thenf = TrueElsef = False7(编写程序找出1-100之间的所有李生素数。Option Base 1Option ExplicitPrivate Sub Form_Click()Dim prime(50) As Integer, i As Integer, lprime() As Integer, 1j AsIntegerDim k As Integer, m As Integer, j As Integerprime(1) = 2m = 1F

4、or i = 3 To 99 Step 2For k = 2 To Sqr(i)If i Mod k = 0 Then Exit ForNext kIf k > Sqr (i) Thenm = m + 1prime(m) = iEnd IfNext iCall pd(prime, m, lprime, 1 j)For i = 1 To ljPrint lprime(i):If i Mod 2=0 Then PrintNext iEnd SubPrivate Sub pd(p() As Integer, n As Integer, lp() As Integer, j AsInteger)

5、Dim i As IntegerFor i = 1 To n - 1If p(i + 1) - p(i) = 2 Then ReDim Preserve lp(j + 2)lp(j + 1) = P(i)1P(J + 2) = p(i + 1)j 二 j + 2End IfNext IEnd Sub8(利用随机函数Rnd()生成20个在l、100之间的各不相同的正整数,并在窗体 上显示出来。Private Sub Commandl_Click()Dim a(19) As IntegerCall s(a(), 19)End SubPrivate Sub s (b () As Integer, n

6、 As Integer)Dim i As Integer, j As IntegerFor i = 0 To 19b(i) = Int (Rnd * 100 + 1)For j = 0 To i - 1If b(i) = b(j) Then i 二 i 一 1: Exit ForNext jDim i As Integer, j As IntegerPrint b(i);Next iEnd Sub9(将上面生成的20个首尾相连,找出每相邻四个数之和最大的四个数,并按 下面格式打印出来:nl+n2+n3+n4二sum,其中nl、n2、n3、n4是所求的四个数,sum 是这四个数的和。Privat

7、e Sub Command1_C1ick()Dim a(19) As Integer, i As Integer, j As Integer, suml As Integer, sum As IntegerCall s(a(), 19)suml = a(l) + a (2) + a (3) + a (4)j = 1For i = 0 To 19sum = a(i) + a(i + 1) Mod 20) + a(i + 2) Mod 20) + a(i + 3) Mod20)If sum > suml Then j = iNext iPrint a(j) && a(j +

8、1) Mod 20) && a(j + 2) Mod 20) && a(j + 3) Mod 20) & "二"&sumEnd SubPrivate Sub s(b() As Integer, n As Integer)For i = 0 To nb(i) = Int(Rnd * 100 + 1)For j = 1 To i - 1If b(i) = b(j) Then i = i - 1: Exit ForNext jPrint b(i);Next iPrintEnd Sub10(利用随机函数rnd()生成25个正整数,分别

9、赋给一个5X5数组的每个元 素,然后找出最大元素的位置,并按A(nl, n2)=M形式打印出来。Option Base 1Private Sub Commandl_Click() Dim A(5, 5) As Integer, i As Integer, jAs Integer, nl As Integer, n2 As IntegerFor i = 1 To 5For j = 1 To 5A(i, j) = Int(Rnd * 100 + 1)Print A(i, j);Next jPrintNext iMax = A(l, 1) : nl = 1: n2 = 1For i = 1 To 5

10、For j = 1 To 5If A(i, j) > Max ThenMax = A(i, j)nl = in2 = jEnd IfNext jNext iPrint "A (” & nl & ",” & n2 & ")="; Max End Sub11(Option ExplicitPrivate Function Fab(ByVai N As Integer) As LongIf N = 1 Or N = 2 ThenFab 二 1ElseFab = Fab(N - 1) + Fab(N - 2)End IfEn

11、d FunctionPrivate Sub Form_Click()Dim N As Integer, F As LongN = InputBox C输入一个正整数")F = Fab (N)Print FEnd Sub12(编写程序,验证一个大于2的偶数可以表示为两个素数之和。歌徳巴赫猜想,见实验 9-5 Private Sub Command 1_C1 ick()Dim x As Integer, i As Integer, y As IntegerDox = InputBoxC请输入一个大于2的偶数)Loop Until x > 2 Or x Mod 2=0If x = 4

12、 ThenPrint "4二2+2"Elsei = 3Doy = x - iIf judge (i) And judge(y) ThenPrint Str (x) & "二"& Str(i) &Str(y)Exit DoElsei = i + 2End IfLoop Until i > yEnd IfIf i > y ThenPrint 定理不成立"End IfEnd SubPrivate Function judge(n As Integer)judge = TrueDim i As IntegerFor

13、i = 2 To n - 1If n Mod i 二 0 Thenjudge = FalseExit FunctionEnd IfNext iEnd Function13 (求 Armstrong 数Private Sub Form_Click()Dim n As Integer, flag As Booleann = Input Box C输入一个正整数n:")Call arm(n, flag)If flag ThenPrint Str (n) + "是 arm 数"ElsePrint Str (n) + "不是 arm 数"End IfEn

14、d SubPrivate Sub arm(m As Integer, f As Boolean)Dim i As Integer, j As Integer, s As Long, t As Integer j = Len(CStr(m)For i = 1 To jt 二 Mid(m, i, 1)Next iIf m = s Thenf = TrueElsef = FalseEnd IfEnd Sub14 (编写一个将X进制数转换成十进制数的通用程序。见实验9-7Private Sub Commandl_Click()Dim si As String, n As Integer, s2 As

15、StringIf Textl. Text = "" Or Text2. Text = "" Then MsgBox "请输入数据" n = Textl Textsi = Text2 Texts2 = zh(sl, n)Text3. Text = s2End SubPrivate Function zh(st As String, n As Integer) As LongDim p As Long, i As Integer, ch As String * 1Dim s As Longp 二 n " (Len(st) - 1

16、)For i = 1 To Len(st)ch = UCase (Mid(st, i, 1)Select Case chCase "0 To ©s = s + ch * pCase "A" To "Z"s 二 s + (Asc(ch) - 55) * pEnd SelectP = P nNext izh = sEnd Function13(编写一个裁判评分程序见实验9-8Dim cj (5) As SinglePrivate Sub Command1_C1ick()Dim i As IntegerFor i = 0 To 5cj(i)

17、 = Int(Rnd * 7 + 4) 10) / 10Textl(i) = CStr(cj(i)Next iEnd SubPrivate Sub Command2_C1ick()Dim max As Single, min As Single, s As SingleS = cj(o)max = cj (0)min = cj (0)For i = 1 To 5If max < cj (i) Then max = cj(i)If min > cj (i) Then min = cj(i)s 二 s + cj(i)Next iText2. Text = CStr (s - max -

18、 min) / 4)End Sub16(求数列和Private Sub Form_Click()Dim n As Integer, y As Singlen = Input Box C输入一个 n:")Call jsh(n, y)Print yEnd SubPrivate Sub jsh(m As Integer, x As Single)Dim i As Integer, t As Long, s As SingleFor i = 1 To mt = t + 2 * is = 1 / tX = X + sNext iEnd Sub17(求级数和Private Function F(n As Integer) As LongIf n = 1 Or n = 2 ThenF = 1ElseF = F(n - 1) + F(n - 2)End IfEnd FunctionPrivate Sub Command1_C1ick()Dim n As Integer, t As Single, s As Singlex = Textl Textn = 1Don = n + 1t = x " F(n) / (F(n - 1) * F

温馨提示

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

评论

0/150

提交评论