VB第4-6章课后编程题代码_第1页
VB第4-6章课后编程题代码_第2页
VB第4-6章课后编程题代码_第3页
VB第4-6章课后编程题代码_第4页
VB第4-6章课后编程题代码_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

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

文档简介

1、代码:第四章:Page。108。4(1),分段函数:Private Sub Command1_Click()Dim x As Single, y As Singlex = Val(InputBox(assign a number to x, Number x, 0)If x 1 Theny = xElseIf x = 1 Theny = 3 * x - 2Elsey = 4 * x - 12End IfPicture1.Print when x=; x; then y=; yEnd SubPrivate Sub Command2_Click()EndEnd Sub4(2):Private Su

2、b Command1_Click()RandomizeFor i = 1 To 100n = Int(Rnd * (99 - 10 + 1) + 10)Text1 = Text1 + CStr(n) + Select Case n Case Is 70 m3 = m3 + 1 End SelectNext iPicture1.Print 小于等于40的数有:; m1Picture1.Print 大于40小于等于70的数有:; m2Picture1.Print 大于70的数有:; m3End SubPrivate Sub Command2_Click()Text1 = Picture1.ClsE

3、nd SubPrivate Sub Form_Load()Text1 = Picture1.ClsEnd Sub4(3):3和7的倍数:Private Sub Command1_Click()For i = 1 To 100 If i Mod 3 = 0 Then n1 = n1 + 1 End If If i Mod 7 = 0 Then n2 = n2 + 1 End IfNext iPrint 1-100中是3的倍数的有:; n1Print 1-100中是7的倍数的有:; n2End SubPrivate Sub Command2_Click()EndEnd Sub4(4):分开奇偶数D

4、im i As Integer, j As IntegerDim n(1 To 20) As IntegerDim m As IntegerPrivate Sub Command1_Click()RandomizeFor i = 1 To 20n(i) = Int(Rnd * (999 - 100 + 1) + 100)Picture1.Print n(i); m = m + 1 If m Mod 4 = 0 Then Picture1.PrintNext iEnd SubPrivate Sub Command2_Click()For j = 1 To 20If n(j) Mod 2 = 0

5、ThenPicture2.Print n(j);k = k + 1 If k Mod 4 = 0 Then Picture2.PrintElsePicture3.Print n(j);t = t + 1 If t Mod 4 = 0 Then Picture3.PrintEnd IfNext jEnd SubPrivate Sub Command3_Click()EndEnd Sub课程成绩一览表:Dim i As Integer, n As IntegerDim Cmrk As String, sum As IntegerPrivate Sub Command1_Click()Print 课

6、程名称; Spc(3); 分数n = Val(InputBox(请输入课程总数)For i = 1 To nCmrk = InputBox(输入课程名称及分数, 课目及分数, 数学 86)m = InStr(Cmrk, )k = Len(Cmrk)C = Left(Cmrk, m - 1)mrk = Right(Cmrk, k - m)Print C; Tab(12); mrksum = sum + mrkNext iPrintPrint 总分; sumPrint 平均分 ; Format(sum / n, 0.0)End Sub4(5):输出素数Private Sub Command1_Cl

7、ick()For i = 100 To 300 For j = 2 To i - 1 If i Mod j = 0 Then Exit For Next j If j = i Then Print i; n = n + 1 If n Mod 5 = 0 Then Print End IfNext iEnd SubPrivate Sub Command2_Click()EndEnd Sub4(6):水仙花数Private Sub Command1_Click()PrintPrintFor i = 100 To 999t = Int(i / 100)m = Int(i - t * 100) / 1

8、0)f = Int(i - t * 100 - m * 10) If i = t 3 + m 3 + f 3 Then Print i; End IfNext iEnd SubPrivate Sub Command2_Click()EndEnd Sub4(7):判别回文数Private Sub Command1_Click()m = Text1.Textn = Len(m)For i = 1 To n 2 If Mid(m, i, 1) Mid(m, n - i + 1, 1) Then Exit For End IfNext iIf i = n 2 + 1 Then Text2 = 此数为回

9、文数 Else Text2 = 此数不是回文数 End IfEnd SubPrivate Sub Command2_Click()EndEnd SubPrivate Sub Form_Load()Text1 = Text2 = End Sub4(8):Private Sub Command1_Click()Dim x As String, y As StringDim y1 As Integer, y2 As Integer, y3 As Integer, y4 As Integerx = Text1.TextFor i = 1 To Len(x)y = Asc(Mid(x, i, 1)If

10、(y = 65) Or (y = 65) Theny1 = y1 + 1ElseIf y = 48 And y Max Then Max = A(i, j): pointer(i) = j End If Next j If Max = A(i, 1) Then Print 第; CStr(i); 行的第; 1; 列元素; CStr(Max); 为最大值 Else Print 第; CStr(i); 行的第; CStr(pointer(i); 列元素; CStr(Max); 为最大值 End IfNext iEnd Sub4(9):Dim first As Single, sec As Sing

11、lePrivate Sub Command1_Click()EndEnd SubPrivate Sub Form_Load()For i = 1 To 7Option1(i).Top = Option1(0).Top + i * 600Option1(i).Left = Option1(0).LeftNext iEnd SubPrivate Sub Option1_Click(Index As Integer)first = Text1: sec = Text2Select Case IndexCase 0 Picture1.Cls Picture1.Print first + secCase

12、 1 Picture1.Cls Picture1.Print first - secCase 2 Picture1.Cls Picture1.Print first * secCase 3 Picture1.Cls If sec = 0 Then MsgBox Wrong data Else Picture1.Print first / sec End IfCase 4 Picture1.Cls If sec = 0 Then MsgBox Wrong data Else Picture1.Print first sec End IfCase 5 Picture1.Cls Picture1.P

13、rint first Mod secCase 6 Picture1.Cls Picture1.Print first secCase 7 Picture1.Cls Picture1.Print first & secEnd SelectEnd Sub4 (10 ) Option Base 1Dim A() As Integer, B() As Integer, C() As IntegerDim N As Integer, M As IntegerPrivate Sub Command1_Click()N = InputBox(请输入数组A的元素个数)ReDim A(N)RandomizeFo

14、r i = 1 To NA(i) = Int(Rnd * 20)Next iFor i = 1 To N - 1 For j = i + 1 To N If A(j) A(i) Then temp = A(i): A(i) = A(j): A(j) = temp End If Next jText1 = Text1 & Str(A(i)Next iText1 = Text1 & Str(A(N)End SubPrivate Sub Command2_Click()M = InputBox(请输入数组B的元素个数)RandomizeReDim B(M)For i = 1 To M B(i) =

15、Int(Rnd * 20)Next iFor i = 1 To M - 1 For j = i + 1 To M If B(j) B(i) Then temp = B(i): B(i) = B(j): B(j) = temp End If Next j Text2 = Text2 & Str(B(i)Next iText2 = Text2 & Str(B(M)End SubPrivate Sub Command3_Click()ReDim C(N)For i = 1 To NC(i) = A(i)Next iReDim Preserve C(N + M)For i = N + 1 To M +

16、 NC(i) = B(i - N)Next ip = 1: t = M + NDo While p = t i = p + 1 Do While i = t If C(i) = C(p) Then For j = i To t - 1 C(j) = C(j + 1) Next j k = k + 1 t = t - 1 Else i = i + 1 End If Loopp = p + 1LoopFor i = 1 To M + N - k - 1 For j = i + 1 To M + N - k If C(j) C(i) Then temp = C(i): C(i) = C(j): C(

17、j) = temp End If Next j Text3 = Text3 + Str(C(i)Next iText3 = Text3 + Str(C(M + N - k)End SubPrivate Sub Form_Load()Text1 = Text2 = Text3 = End Sub子过程:Option Base 1Private N As Integer, M As IntegerDim A() As Integer, B() As Integer, C() As IntegerPrivate Sub Command1_Click()N = InputBox(输入A数组的元素个数)

18、Call CRT(N, A()Call sort(N, A()For Each x In AText1 = Text1 + Str(x)Next xEnd SubPrivate Sub CRT(ByVal t As Integer, x() As Integer)ReDim x(t)For i = 1 To tx(i) = Int(Rnd * 90 + 10)Next iEnd SubPrivate Sub Command2_Click()M = InputBox(输入B数组的元素个数)Call CRT(M, B()Call sort(M, B()For Each x In BText2 =

19、Text2 + Str(x)Next xEnd SubPrivate Sub sort(ByVal t As Integer, y() As Integer)For i = 1 To t - 1 For j = i + 1 To t If y(j) y(i) Then temp = y(i): y(i) = y(j): y(j) = temp End If Next jNext iEnd SubPrivate Sub Command3_Click()Dim s As IntegerReDim C(M + N)For i = 1 To N C(i) = A(i)Next iFor i = N +

20、 1 To M + NC(i) = B(i - N)Next iCall sort(M + N, C()For Each x In CText3 = Text3 + Str(x)NextText3 = Text3 + vbCrLf + 去除重复元素后 + vbCrLfCall RmvRpt(C(), s)For i = 1 To M + N - sText3 = Text3 + Str(C(i)Next iEnd SubPrivate Sub RmvRpt(z() As Integer, k As Integer)t = UBound(z)p = 1Do While p = ti = p +

21、1 Do While i = A And ea = 0 And Val(ea) = n Then MsgBox Wrong data entry: m = 0: Exit For m = m + hm * n (k - i) Next iEnd SubPrivate Sub Command2_Click()Text1 = Text2 = Text3 = End Sub4(4)Option Base 1Private Sub Command1_Click() Dim Nu_Sc As String Dim a(12) As String, b(12) As String Print 学号及成绩

22、For i = 1 To 12 Nu_Sc = InputBox(请输入12名学生的学号及成绩, , 23,87) pl = InStr(Nu_Sc, ,) a(i) = Left(Nu_Sc, pl - 1) b(i) = Mid(Nu_Sc, pl + 1) Print ; a(i); :; b(i); k = k + 1 If k Mod 6 = 0 Then Print Next i Print Print 排名, 学号, 分数 Call sort(a, b) Call rate(a, b)End SubPrivate Sub sort(nu() As String, sc() As

23、String) For i = 1 To 11 For j = i + 1 To 12 If Val(sc(j) Val(sc(i) Then temp1 = sc(i): temp2 = nu(i) sc(i) = sc(j): nu(i) = nu(j) sc(j) = temp1: nu(j) = temp2 End If Next j Next iEnd SubPrivate Sub rate(nu() As String, sc() As String) Dim plc(12) As Integer plc(1) = 1: k = 0 For i = 2 To 12 If Val(s

24、c(i) = Val(sc(i - 1) Then plc(i) = plc(i - 1) Else plc(i) = i End If Next i For i = 1 To 12 Print plc(i), nu(i), sc(i) Next iEnd Sub4(5)Private Sub Command1_Click() Dim n As String, flg As Boolean n = Text1 Call jud(n, flg) If flg Then Label2.Caption = Text1.Text & 为降序数 Else Label2.Caption = Text1.T

25、ext & 不是降序数 End IfEnd SubPrivate Sub jud(x As String, judge As Boolean)n = Len(x)For i = 1 To n - 1 If Mid(x, i, 1) = Sqr(x) Then prime = TrueEnd FunctionPrivate Function pls(ByVal y As Integer) As Integer For i = 1 To y If y Mod i = 0 Then pls = pls + i End If Next iEnd Function4(7)Private Sub Comm

26、and1_Click() Dim i As Integer For i = 0 To 5 Text1(i) = Val(f(i + 1) Next iEnd SubPrivate Function f(n As Integer) If n = 1 Or n = 2 Then f = 1 Else f = f(n - 2) + f(n - 1) End IfEnd Function4(8)Private Sub Command1_Click() Dim n As Integer, y As Single, an As Single n = 1 Do an = Mul(n) y = y + an

27、n = n + 1 Loop While an = 0.00001 Picture1.Print Format(y, 0.0000#)End SubPrivate Function Mul(m As Integer) As Single Mul = 1 For i = 1 To m Mul = Mul / (2 * i) Next iEnd Function4(9)Private Sub Command1_Click()Dim n As Integer, x As Single, an As Singlex = InputBox(请输入01之间任一小数, 求级数, 0.2)Dim i As I

28、ntegeri = 2Doan = series(x, i)s = s + ani = i + 1Loop While an 0.00001s = s + xPicture1.Print when x=; Format(x, 0.0#)Picture1.Print S=; Format(s, 0.0000#)End SubPrivate Function series(x As Single, ByVal n As Integer) As Singleseries = x f(n) / (f(n - 1) * f(n)End FunctionPrivate Function f(n As Integer) If n = 1 Then f = 1 ElseIf n = 2 Then f = 2 Else f = f(n - 1) + f(n - 2) End IfEnd Function4(10)Option Base 1Dim a(5) As Integer, b(5) As Integer, c() As Integer, SA As String, SB As StringPrivate Sub Command1_Cl

温馨提示

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

评论

0/150

提交评论