




版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
1、中國石油大學華東VB練習題(大一下)1、 時鐘2、 滾動條3、 列表框4、 復選框5、 單選框6、 組合框7、 多分枝選擇8、 定義函數(shù)九、TXT十、數(shù)組一、時鐘程序運行后,每隔2秒命令按鈕Command1向右移動100個單位。Private Sub Form_Load()Timer1.Interval = 2End SubPrivate Sub timer1_timer()Call mymoveEnd SubSub mymove() Command1.Left = Command1.Left + 200End Sub二、滾動條水平滾動條HScroll1的數(shù)值范圍04000,LargeChan
2、ge設置為200,SmallChange設置為50,水平滾動條HScroll1的值(Value)代表命令按鈕Command1的寬度,當水平滾動條的滑塊變化時,命令按鈕Command1的寬度隨之改變Private Sub Form_Load()HScroll1.Min = 0HScroll1.Max = 4000HScroll1.SmallChange = 50HScroll1.LargeChange = 200Command1.Caption = "駐倚靠危樓風細細,無言誰會憑欄意。衣帶漸寬終不悔,為伊銷得人憔悴"End SubPrivate Sub HScroll1_Ch
3、ange()Command1.Width = Val(HScroll1.Value)End Sub三、列表框 點擊“->”按鈕,將左邊列表中的選中項移動到右邊列表的末尾;點擊“<-”按鈕,將右邊列表中的選中項移動到左邊列表的末尾;點擊“=>”按鈕,將左邊列表所有內(nèi)容移動到右邊列表的末尾;點擊“<=”按鈕,將右邊列表所有內(nèi)容移動到左邊列表的末尾。Private Sub Form_Load()List1.AddItem "A"List1.AddItem "B"List1.AddItem "C"List2.AddIt
4、em "d"List2.AddItem "e"End SubPrivate Sub Command1_Click()i = List1.ListIndexIf i >= 0 And i <= List1.ListCount - 1 Then List2.AddItem List1.Text List1.RemoveItem iEnd IfEnd SubPrivate Sub Command2_Click()i = List2.ListIndexIf i >= 0 And i <= List2.ListCount - 1 Then
5、List1.AddItem List2.Text List2.RemoveItem iEnd IfEnd SubPrivate Sub Command3_Click()Dim i%For i = 0 To List1.ListCount - 1List2.AddItem List1.List(i)Next iList1.ClearEnd SubPrivate Sub Command4_Click()Dim i%For i = 0 To List2.ListCount - 1List1.AddItem List2.List(i)Next iList2.ClearEnd Sub 實現(xiàn)追加、插入、修
6、改、刪除、清空等5個按鈕的功能Sub ClrList(AList As ListBox) For i = AList.ListCount - 1 To 0 Step -1 AList.RemoveItem i Next iEnd SubPrivate Sub Command1_Click() List1.AddItem Text1.TextEnd SubPrivate Sub Command2_Click() i = List1.ListIndex With List1 If i >= 0 And i <= .ListCount - 1 Then .AddItem Text1.Te
7、xt, i End WithEnd SubPrivate Sub Command3_Click() i = List1.ListIndex If i >= 0 And i <= List1.ListCount - 1 Then List1.List(i) = Text1.Text End IfEnd SubPrivate Sub Command4_Click() With List1 If .ListIndex > -1 Then _ .RemoveItem .ListIndex End With End SubPrivate Sub Command5_Click() Clr
8、List List1 ' List1.ClearEnd SubPrivate Sub Form_Load() List1.Clear: List1.AddItem "語文": List1.AddItem "數(shù)學": List1.AddItem "英語"End Sub實現(xiàn)第一條、前一條、后一條、最后一條等4按鈕的功能 Private Sub form_load()List1.AddItem "財務"List1.AddItem "會計"List1.AddItem "國貿(mào)"
9、List1.AddItem "營銷"List1.AddItem "工管"Private Sub Command1_Click()List1.Text = List1.List(0)End SubPrivate Sub Command2_Click()List1.Text = List1.List(List1.ListIndex - 1)End SubPrivate Sub Command3_Click()List1.Text = List1.List(List1.ListIndex + 1)End SubPrivate Sub Command4_Clic
10、k()List1.Text = List1.List(List1.ListCount - 1)End Sub4、 復選框 窗體中4個復選框分別代表4各班級,復選框中的標題文字代表班級人數(shù),用戶選擇復選框后,點擊命令按鈕Command1,程序計算出所選班級的總?cè)藬?shù),結(jié)果放入文本框Text1中。Private Sub Command1_Click()Dim s%If Check1.Value = 1 Then s = s + Val(Check1.Caption)End IfIf Check2.Value = 1 Then s = s + Val(Check2.Caption)End IfIf C
11、heck3.Value = 1 Then s = s + Val(Check3.Caption)End IfIf Check4.Value = 1 Then s = s + Val(Check4.Caption)End IfText1.Text = Str(s)End SubPrivate Sub Form_Load()Check1.Caption = 30Check2.Caption = 28Check3.Caption = 31Check4.Caption = 29Label1.Caption = "人數(shù)"Text1.Text = ""End Sub
12、用戶選擇復選框后,點擊命令按鈕Command1,程序根據(jù)復選框?qū)ξ谋究騎ext1中的字體進行相應的設置。Private Sub Command1_Click()If Check1.Value = 1 Then Text1.FontBold = TrueIf Check2.Value = 1 Then Text1.FontItalic = TrueIf Check3.Value = 1 Then Text1.FontUnderline = TrueEnd Sub五、 單選框用戶在數(shù)和算法兩個框架中各選一個單選按鈕,點擊命令按鈕Command1,程序根據(jù)選中單選按鈕進行相應的計算,結(jié)果放入文本框T
13、ext1中。Private Sub Command1_Click()Dim a%, b%, c%, d%k = 1: t = 0a = Val(Option1.Caption)b = Val(Option2.Caption)If Option1.Value And Option3.Value Then For i = 1 To a k = k * i Next iText1.Text = kElseIf Option1.Value And Option4.Value Then For i = 1 To a t = t + i Next iText1.Text = tElseIf Option2
14、.Value And Option3.Value Then For i = 1 To b k = k * i Next iText1.Text = kElseIf Option2.Value And Option4.Value Then For i = 1 To b t = t + i Next iText1.Text = tEnd IfEnd Sub5、 組合框字體組合框包括宋體、黑體、幼圓、隸書4個列表項;字號組合框列表項的值從1248,以4遞增。選擇字體組合框或者字號組合框中的某一項,點擊按鈕Command1,文本框中的字體和字號進行相應的改變。Private Sub Command1_
15、Click()Text1.FontName = Combo1.TextText1.FontSize = Combo2.TextEnd SubPrivate Sub Form_Load()Combo1.AddItem "宋體"Combo1.AddItem "黑體"Combo1.AddItem "幼圓"Combo1.AddItem "隸書"For i = 12 To 48 Step 4Combo2.AddItem iNext iText1.Text = "Visual Basic"End Sub七、
16、多分枝加密:點擊加密按鈕Command1,輸入1個小寫英文字母,程序?qū)⒃撚⑽淖帜缸優(yōu)樽帜副碇衅浜竺娴囊粋€字母輸出,如a改為b,m改為n,z改為a,以此類推。點擊解密按鈕Command2,輸入1個小寫英文字母,程序?qū)⑵浣饷芎笸ㄟ^輸出。使用InputBox和MsgBox完成輸入輸出。Private Sub Command1_Click()Dim a As String, b As Stringa = InputBox("")If Asc(a) = 122 Then b = Chr(97)Else: b = Chr(Asc(a) + 1)End IfMsgBox bEnd Sub
17、Private Sub Command2_Click()Dim c As String, d As Stringc = InputBox("")If Asc(c) = 97 Then d = Chr(122)Else: d = Chr(Asc(c) - 1)End IfMsgBox dEnd Sub鐵路收費:鐵路行李托運費計算規(guī)則為:行李重量不超過50kg時,每千克0.25元;超過50kg不超過100kg時,其超過部分每千克0.35元;超過100kg時,其超過部分每千克0.45元。點擊按鈕Command1,程序輸入行李重量,計算并輸出托運費用。使用InputBox和MsgB
18、ox完成輸入輸出。Private Sub Command1_Click()Dim a As Double, b As Doublea = Val(InputBox("")If a >= 0 And a <= 50 Then b = a * 0.25 ElseIf a <= 100 Then b = 12.5 + (a - 50) * 0.35 ElseIf a >= 100 Then b = 30 + (a - 100) * 0.45End IfMsgBox bEnd Sub (也可用select case 語句)成績評定; 成績評價方法為:90及以
19、上優(yōu)秀,80及以上良好,70及以上中等,60及以上及格,60以下不及格。點擊按鈕Command1,程序輸入成績,輸出成績評價。使用InputBox和MsgBox完成輸入輸出。Private Sub Command1_Click() a = Val(InputBox("輸入成績") If a >= 90 Then b = "優(yōu)秀" ElseIf a >= 80 Then b = "良好" ElseIf a >= 70 Then b = "中等" ElseIf a >= 60 Then b =
20、"及格" Else b = "不及格" End If MsgBox bEnd Sub (也可用select case 語句或者直接限定范圍進行選擇)8、 定義函數(shù): 【一個月有幾天】一個月的天數(shù):在窗體Form1中實現(xiàn)函數(shù)Days。參數(shù)y和m分別表示年份和月份,函數(shù)返回該年該月的天數(shù)。 提示:閏年2月份有29天,平年有28天。 閏年的條件:1)年份能被4整除,但不能被100整除;2)或年份能被400整除 Public Function Days(y As Long, m As Long) As Long End FunctionFunction leap
21、(y As Long) As Boolean b1 = y Mod 4 = 0 b2 = y Mod 100 <> 0 b3 = y Mod 400 = 0 leap = b1 And b2 Or b3End FunctionPublic Function days(y As Long, m As Long) As Long Select Case m Case 2 If leap(y) Then days = 29 Else days = 28 End If Case 4, 6, 9, 11 days = 30 Case 1, 3, 5, 7, 8, 10, 12 days =
22、31 End SelectEnd FunctionPrivate Sub Command1_Click() Dim a&, b&, c& a = Val(InputBox("") b = Val(InputBox("") c = days(a, b) MsgBox cEnd Sub【三角形】:在窗體Form1中實現(xiàn)函數(shù)Triangle。a,b,c三個參數(shù)為三角形三條邊,若三邊能構成三角形,通過參數(shù)p將周長返回,函數(shù)返回值返回三角形面積;否則周長和面積均返回0。 提示: 海倫公式為: , Public Function Triang
23、le(a As Long, b As Long, c As Long, p As Long) As Double End FunctionPublic Function Triangle(a As Long, b As Long, c As Long, ByRef p As Long) As Double Dim l If a + b <= c Or a + c <= b Or b + c <= a Then Triangle = 0: p = 0 Else p = a + b + c l = p / 2 Triangle = Sqr(l * (l - a) * (l - b
24、) * (l - c) End IfEnd FunctionPrivate Sub Command1_Click() Dim a&, b&, c&, p&, s# a = Val(Text1.Text) b = Val(Text2.Text) c = Val(Text3.Text) s = Triangle(a, b, c, p) Text4.Text = str(p) Text5.Text = str(s)End Sub【計算總和】: 在窗體Form1中實現(xiàn)函數(shù)Sum。參數(shù)a()為輸入的數(shù)組,函數(shù)返回數(shù)組中所有元素之和。 Public Function Sum
25、(a() As Long) As Long End FunctionFunction sum(a() As Long) As Long sum = 0 For i = LBound(a) To UBound(a) sum = sum + a(i) Next iEnd FunctionPrivate Sub Command1_Click() Dim a(3) As Long TextToArray Text1, a MsgBox sum(a)End Sub【排序】: 在窗體Form1中實現(xiàn)子程序Sort。參數(shù)a()為數(shù)組,子程序Sort對該數(shù)組按正序(從小到大)排序。 Public Sub So
26、rt(a() As Long) End SubPublic Sub sort(a() As Long)Dim i%, j%, k%, p%For i = LBound(a) To UBound(a) - 1p = iFor j = i + 1 To UBound(a) If a(p) > a(j) Then p = j Next j k = a(i) a(i) = a(p) a(p) = kNext iEnd Sub(王老師的那種算法可能會評測錯誤)【判斷是否三角形等腰】: 在窗體Form1中實現(xiàn)函數(shù)TriangleClass。a,b,c三個參數(shù)為三角形三條邊,函數(shù)返回三角形的形狀??赡?/p>
27、的三角形形狀為以下4種:等邊三角形,等腰三角形,一般三角形,不構成三角形。 Public Function TriangleClass(a As Long, b As Long, c As Long) As String End Function Private Sub Command1_Click()Dim a As Long, b As Long, c As Longa = Val(InputBox("")b = Val(InputBox("")c = Val(InputBox("")MsgBox triangleclass(a,
28、 b, c)End SubPublic Function triangleclass(a As Long, b As Long, c As Long) As StringDim s$If a + b <= c Or b + c <= a Or a + c <= b Thens = "不構成三角形"ElseIf a = b And a = c And b = c Thens = "等邊三角形 "ElseIf a = b Or b = c Or a = c Thens = "等腰三角形"Else:s = "一般
29、三角形"End Iftriangleclass = sEnd Function 【最大值】: 在窗體Form1中實現(xiàn)函數(shù)Max。參數(shù)a()為輸入的數(shù)組,函數(shù)返回數(shù)組中的最大值。 Public Function Max(a() As Long) As Long End Function Option Base 1Private Sub Form_Load()Dim n %, a%() ,i%n = Val(InputBox("")ReDim a(n)For i = 1 To na(i) = Val(InputBox("")Next iMsgBox
30、 max(a()End SubPublic Function max(a() As Long) As LongDim i As Integermax = a(LBound(a)For i = LBound(a) To UBound(a)If a(i) > max Then max = a(i)Next iEnd Function【排序】:在窗體Form1中實現(xiàn)子程序Sort。參數(shù)a()為數(shù)組,子程序Sort對該數(shù)組按逆序(從大到?。┡判颉?Public Sub Sort(a() As Long) End Sub Public Sub Sort(a() As Long)n = UBound
31、(a)For i = 0 To n - 1For j = i + 1 To nIf a(j) > a(i) Thent = a(i)a(i) = a(j)a(j) = tEnd IfNext jNext iEnd Sub【完數(shù)】:在窗體Form1中實現(xiàn)函數(shù)Perfect。如果參數(shù)x為完全數(shù),該函數(shù)返回True;否則返回False。(完全數(shù):真因子(除自身以外的因子)之和為其本身) Public Function Perfect(x As Long) As Boolean End FunctionPrivate Sub Command1_Click()Dim x As Longx = Va
32、l(InputBox("")MsgBox perfect(x)End SubPublic Function perfect(x As Long) As BooleanFor i = 0 To x - 1If x Mod i = 0 Thens = s + iEnd IfNext iIf s = x Thenperfect = TrueElseperfect = FalseEnd IfEnd Function【符號函數(shù)】 在窗體Form1中實現(xiàn)函數(shù)MySgn。當參數(shù)x>0時,函數(shù)MySgn返回1;當參數(shù)x<0時,函數(shù)MySgn返回-1;當參數(shù)x=0時,函數(shù)MySg
33、n返回0。 Public Function MySgn(x As Long) As Long End FunctionPublic Function MySgn(ByVal x As Long) As LongIf x > 0 Then MySgn = 1ElseIf x = 0 Then MySgn = 0Else MySgn = -1End IfEnd Function九、TXT:復制文件:復制文件。點擊Command1按鈕,程序?qū)⑽募.txt中的內(nèi)容復制到文件b.txt中Private Sub Command1_Click()Dim a$Open "a.txt"
34、; For Input As #1 Open "b.txt" For Output As #2 Do While Not EOF(1) Line Input #1, aPrint #2, aLoopClose #2逆序: 文件a.txt中每行包括2列內(nèi)容,分別為姓名,成績。單擊Command1按鈕,程序?qū).txt文件中的各行,逆序?qū)懭隻.txt文件中。 a.txt文件格式示例 - "小紅",90 "小明",95 "小剛",85 b.txt文件格式示例 - "小剛",85 "小明&qu
35、ot;,95 "小紅",90Private Sub Command1_Click()Dim a$(1 To 10), b%(1 To 10), i%, j%Open "a.txt" For Input As #1Open "b.txt" For Output As #2i = 1 Do While Not EOF(1) Input #1, a(i), b(i) i = i + 1 LoopFor j = UBound(b) To LBound(b) Step -1 If b(j) > 0 Then Write #2, a(j),
36、 b(j) End IfNext jClose #2Close #1End Sub找最大: 文件a.txt中每行包括2列內(nèi)容,分別為姓名,成績。單擊Command1按鈕,程序找出最高分所在行,將姓名和成績寫入b.txt文件中。 a.txt文件格式示例 - "小紅",90 "小明",95 "小剛",85 b.txt文件格式示例 - "小明",95Private Sub Command1_Click()Dim a$, b%, c$, d%Open "a.txt" For Input As #1 Op
37、en "b.txt" For Output As #2 Do While Not EOF(1) Input #1, a, b If b > d Then d = b: c = a Loop Write #2, c, dClose #1Close #2End Sub 求和: 文件a.txt中每行包括3列內(nèi)容,分別為姓名,語文成績,數(shù)學成績。單擊Command1按鈕,程序計算出每名同學的總成績,寫入b.txt文件中。 a.txt文件格式示例 - "小紅",80,90 "小明",70,80 b.txt文件格式示例 - "小紅
38、",170 "小明",150Private Sub Command1_Click()Dim n$, c%, d%, e%Open "a.txt" For Input As #1Open "b.txt" For Output As #2Do While Not EOF(1) Input #1, n, c, d e = c + dWrite #2, n, eLoop Close #1 Close #2End Sub 選擇 : 文件a.txt中每行包括2列內(nèi)容,分別為姓名,成績。單擊Command1按鈕,程序?qū)⒎謹?shù)大于等于90分對
39、應的行,寫入b.txt文件中。 a.txt文件格式示例 - "小紅",90 "小明",95 "小剛",85 b.txt文件格式示例 - "小紅",90 "小明",95Private Sub Command1_Click()Dim a$, b%Open "a.txt" For Input As #1Open "b.txt" For Output As #2Do While Not EOF(1) Input #1, a, b If b >= 90 The
40、n Write #2, a, b End IfLoopClose #1Close #2End Sub排序: 文件a.txt中每行包含1個數(shù)。單擊Command1按鈕,程序?qū)⑺袛?shù)排序后寫入b.txt文件中。 a.txt文件格式示例 - 13 8 17 12 b.txt文件格式示例 - 8 12 13 17Private Sub Command1_Click()Dim a%(1 To 12), i%, j%, k%, t%,h = UBound(a): g = LBound(a): i = LBound(a)sp = App.Path & ""Open sp &
41、; "a.txt" For Input As #1Open sp & "b.txt" For Output As #2For i = 1 To 12 Input #1, a(i)Next iFor j = 1 To 11 For k = j To 12 If a(j) > a(k) Then t = a(j): a(j) = a(k): a(k) = t Print a(j) Next kNext jFor j = 1 To 12If a(j) > 0 Then Write #2, a(j)Next j Close #1 Close
42、#2End Sub素數(shù); 文件a.txt中每行包含1個數(shù)。單擊Command1按鈕,程序?qū)⑺械乃財?shù)寫入b.txt文件中。 a.txt文件格式示例 - 13 8 17 12 b.txt文件格式示例 - 13 17Private Sub Command1_Click()Dim a%sp = App.Path & ""Open sp & "a.txt" For Input As #1Open sp & "b.txt" For Output As #2Do While Not EOF(1) Input #1, a F
43、or i = 2 To a - 1 If a Mod i = 0 Then Exit For Next i If i > a - 1 Then Write #2, aLoopClose #1Close #2End Sub 十、數(shù)組 數(shù)組刪除: 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,在文本框Text3中放入待查找的數(shù),點擊按鈕Command1,程序在數(shù)組中從左到右進行查找,將第1個等于該數(shù)的元素刪除后(若找不到則不刪除任何元素),將數(shù)組放入Text4中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。Option B
44、ase 1Private Sub Command1_Click()Dim n%, a%(), b%(), i%, m%, idx%n = Val(Text1.Text) :m = Val(Text3.Text)ReDim a(n)TextToArray Text2, aidx = -1For i = 1 To nIf m = a(i) Then idx = i: Exit ForEnd IfNext iIf idx > 0 Then For i = idx + 1 To n a(i - 1) = a(i) Next i n = n - 1 ReDim Preserve a(1 To n)
45、End IfArrayToText a, Text4End Sub插入: 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,在文本框Text3中放入待插入的數(shù)據(jù),在文本框Text4中放入在數(shù)組中插入的位置,點擊按鈕Command1,程序?qū)?shù)據(jù)插入數(shù)組中,然后將數(shù)組放入Text5中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。要求數(shù)組元素下標從0開始。Private Sub Command1_Click()Dim a%(), n%, k%, t%,j%n = Val(Text1.Text)ReDim a(n - 1)TextToA
46、rray Text2, ak= Val(Text3.Text)t = Val(Text4.Text)For i = 0 To nIf i < t Then Exit ForNext iFor j = n To t + 1 Step -1a(j) = a(j - 1)Next ja(t) = kReDim Preserve a(n)ArrayToText a, Text5End Sub 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素(數(shù)據(jù)已按從小到大排好順序),在文本框Text3中放入待插入的數(shù)據(jù),點擊按鈕Command1,程序?qū)?shù)據(jù)插入數(shù)組中,且插入數(shù)據(jù)
47、后數(shù)組仍然保持有序,然后將數(shù)組放入Text4中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。Private Sub Command1_Click()Dim a%() ,n%,i%,k%n = Val(Text1.Text) - 1ReDim a(n)TextToArray Text2, ak= Val(Text3.Text)For i = 0 To nIf a(i) >= k Then P = i: Exit ForNext iFor i = n To p + 1 Step -1 a(i + 1) = a(i)Next ia(p) = kReDim Pr
48、eserve a(n + 1)ArrayToText a, Text4End Sub 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,在文本框Text3中放入待插入的數(shù)據(jù),在文本框Text4中放入在數(shù)組中插入的位置,點擊按鈕Command1,程序?qū)?shù)據(jù)插入數(shù)組中,然后將數(shù)組放入Text5中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。要求數(shù)組元素下標從0開始。Private Sub Command1_Click()Dim a%(),n%,i%,j%,k%,t%n = Text1.Text - 1k =val(text3.tex
49、t) t = Text4.TextReDim a(n)TextToArray Text2, aFor i = n To t + 1 Step -1a(i + 1) = a(i)Next ia(t) = kReDim Preserve a(n + 1)ArrayToText a, Text5End Sub 最大最小值: 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,點擊按鈕Command1,找出這n個數(shù)中第二小的數(shù)放入文本框Text3中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。Private Sub Command1_Cl
50、ick()Dim a() As Double, t As Doublen = Val(Text1.Text)ReDim a(n)TextToArray Text2, aFor i = 0 To n - 2 For j = 0 To n - i - 1 If a(j) > a(j + 1) Then t = a(j + 1): a(j + 1) = a(j): a(j) = t End If Next j Next i Text3.Text = a(2)End Sub 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,點擊按鈕Command1,找出這n個數(shù)中
51、最小的數(shù)放入文本框Text3中,將該數(shù)在數(shù)組中的下標放入文本框Text4中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。要求數(shù)組元素下標從0開始。Private Sub Command1_Click()Dim n%, b%, c%, i%, a%() n = Val(Text1.Text)ReDim a( n - 1) TextToArray Text2.Text, ab = a(0): c = 0For i= LBound(a) To UBound(a) If a(i) < b Then b = a(i): c = iNext iText3.Text
52、= str(b)Text4.Text = str(c)End Sub數(shù)組查找: 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,在文本框Text3中放入待查找的數(shù),點擊按鈕Command1,程序在數(shù)組中從左到右進行查找,將第1個等于該數(shù)的元素下標放入文本框Text4中,如果查找不到則將-1放入Text4中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。要求數(shù)組元素下標從0開始。Private Sub Command1_Click()Dim a%() ,i%,k%,t%,n%,idx%n = val(Text1.Text) k =
53、 val(Text3.Text)ReDim a(n- 1)TextToArray Text2, aidx = - 1For i= 0 To n - 1If k = a(i) Thenidx = iEnd IfNext iText4.text = idxEnd Sub 排序: 在文本框Text1中放入一維數(shù)組元素個數(shù)n,在文本框Text2中放入n個數(shù)組元素,點擊按鈕Command1,將n個元素按正序(從小到大)排序后放入文本框Text3中。使用ArrayToText和TextToArray完成數(shù)組與文本框之間的轉(zhuǎn)換。方法一:Private Sub Command1_Click()Dim n%,
54、a%(), i%, j%, k%n = Val(Text1.Text)ReDim a(1 To n)TextToArray Text2, aFor i = LBound(a) To UBound(a) -1 For j = i + 1 To UBound(a) If a(i) > a(j) Then k = a(i) a(i) = a(j) a(j) = k End If Next jNext iArrayToText a, Text3方法二:Private Sub Command1_Click()Dim n%, a%(), i%, j%, k%n = Val(Text1.Text)Re
55、Dim a(1 To n)TextToArray Text2, aFor i = LBound(a) To UBound(a) - 1 t= iFor j = i + 1 To UBound(a)If a(t) < a(j) Then t = j Next j k = a(i) a(i) = a(j) a(j) = kNext iArrayToText a, Text3數(shù)組的輸入輸出: 逆序輸出 點擊按鈕Command1,程序分別輸入個數(shù)n以及n個整數(shù),然后將這n個整數(shù)分別逆序輸出(比如n=3,輸入3個數(shù)分別3 9 7,輸出 7 9 3)。使用InputBox和MsgBox完成輸入輸出。(1次輸入或輸出1個數(shù)據(jù))Private Sub Command1_Click()Dim a%() , b%(),n%,i%,j%n = Val(InputBox("輸入n個數(shù)")ReDim a(1 To n) b(1 To n)For i = 1 To na(i) = Val(InputBox("輸入數(shù)字")Next iFor i = 1 To
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年人行道養(yǎng)護車項目合作計劃書
- 2025年航天器壓力控制系統(tǒng)項目合作計劃書
- 2025年智能用電系統(tǒng)產(chǎn)品合作協(xié)議書
- 2025年元器件測試儀器項目建議書
- 學習方法與學習心理的關聯(lián)性分析
- 探索教育變革之路共創(chuàng)未來終身學習平臺啟動論壇
- 北京通州區(qū)2025屆物理高一第二學期期末經(jīng)典模擬試題含解析
- 教育創(chuàng)新與翻轉(zhuǎn)課堂高等教育的未來路徑
- 中職數(shù)學教學課件
- 安全隱患排查與整改流程
- 學校中層干部選拔聘用實施方案中層干部選聘實施方案2
- 生物必修1教師用書
- 園藝植物育種學知到課后答案智慧樹章節(jié)測試答案2025年春浙江大學
- 《電力機車制動系統(tǒng)檢修與維護》課件 項目二任務四檢修中繼閥
- GB/T 15683-2025糧油檢驗大米直鏈淀粉含量的測定
- 2025吉林省安全員C證考試(專職安全員)題庫及答案
- 電鉆清洗消毒流程
- 裝修貸款申請書
- 造林安全文明施工方案
- 員工作風培訓
- 施工現(xiàn)場防揚塵、防噪音、防光污染措施
評論
0/150
提交評論