版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、二級(jí) vb 程序設(shè)計(jì)練習(xí)程序填空1下面程序按每行10 個(gè)數(shù)打印出 30 個(gè)介于 -300 到 300 間無重復(fù)的隨機(jī)整數(shù),并顯示其中的最小值。數(shù)組b(30)存放所產(chǎn)生的無重復(fù)的隨機(jī)整數(shù),運(yùn)行結(jié)果如圖所示。private sub command1_click() dim b(30), j as integer, k as integer dim y as integer, x as single, min as integer for j = 1 to 30 lb: x = rnd if x 0.5 then y = 1 else y = -1 b(j) = y * int(rnd*301) k
2、 = 1 do while kj if b(j) = b(k) then goto lb k = k + 1 loop print b(j); if j mod 10=0 then print next j min = b(1) for j = 1 to 30 if b(j) min then min=b(j) next j print min end sub 2下面程序用于對(duì)9 位的準(zhǔn)考證號(hào)進(jìn)行校驗(yàn)。參數(shù)mno 存放準(zhǔn)考證號(hào), mjy 存放產(chǎn)生的校驗(yàn)位, tag 判斷校驗(yàn)正確否,若校驗(yàn)正確 tag 置 1,否則為 0。產(chǎn)生校驗(yàn)位的算法為: 取準(zhǔn)考證號(hào)右邊的 8 位之和關(guān)于 9 的模作為校驗(yàn)值
3、, 準(zhǔn)考證號(hào)最左邊的一位為校驗(yàn)位。public sub foe(byval mno as string, mjy as integer, tag as integer) dim msum as integer, m as string msum = 0 for j=2 to 9 m = mid(mno, j, 1) msum = msum + val(m) next j mjy = msum mod 9 if mjy =val(left(mno,1) then tag = 1 else tag = 0 end if end sub command1_click 事件用于調(diào)用 foe 子過程,對(duì)
4、輸入到文本框 text1 的準(zhǔn)考證號(hào)進(jìn)行校驗(yàn),如果校驗(yàn)正確,顯示“正確” ,否則顯示“錯(cuò)誤”,同時(shí)顯示正確的校驗(yàn)碼。private sub command1_click() dim nn as integer, mtag as integer if len(text1) 9 or not isnumeric(text1) then msgbox 非法數(shù)據(jù),請(qǐng)重新輸入! text1 = text1.setfocus exit sub end if call foe(text1, nn, mtag) if mtag then print 正確 else print “錯(cuò)誤” ;nn end if
5、end sub 3下面的程序按從小到大的順序隨機(jī)產(chǎn)生不超過20個(gè)介于100 到 400(包括 100、400)的數(shù),每次產(chǎn)生的一個(gè)數(shù)總大于它前面已產(chǎn)生的數(shù), 然后求產(chǎn)生的數(shù)的平均值。 數(shù)組 b(20)用于存放所產(chǎn)生的隨機(jī)數(shù),運(yùn)行結(jié)果如圖所示。private sub command1_click() dim b(20), i as integer, j as integer, sum as single j = 1 do re: b(j) = int (rnd*301+100) if b(j) = 400 or j = 20 then exit do elseif b(j) = b(j - 1)
6、 then goto re end if j=j+1 loop sum = 0 for i = 1 to j print b(i); sum=sum+b(i) next i print aver=;sum/j end sub 4新的身份證號(hào)由18 位組成,最低位(右邊)為校驗(yàn)位,其算法是: 10-(前 17 位之和關(guān)于 10 的模) 。函數(shù) foe 用于對(duì) 18 位的身份證進(jìn)行校驗(yàn)。參數(shù)mstr 存放身份證號(hào), md為產(chǎn)生的校驗(yàn)位,若校驗(yàn)正確,函數(shù)返回“true” ,否則返回“false” 。public function foe(byval mstr$, md%) as boolean di
7、m mp as string, msum as integer msum = 0 for i=1 to 17 mp = mid(mstr, i, 1) msum = msum + val(mp) next i md = 10 msum mod 10 if md = val(right(mstr,1) then foe = true else foe = false end if end function command1_click 事件用于調(diào)用 foe 子過程,對(duì)輸入到文本框text1 的身份證號(hào)進(jìn)行校驗(yàn),如果校驗(yàn)正確,顯示“true” ,否則顯示“ false”和的校驗(yàn)碼值。private
8、 sub command1_click() dim mm as integer if len(text1) 18 or not isnumeric(text1) then msgbox 非法數(shù)據(jù),請(qǐng)重新輸入! text1 = text1.setfocus exit sub end if if foe(text1, mm) = true then print true else print “false ”;mmend if end sub 5end sub本程序隨機(jī)產(chǎn)生n(15)個(gè) a 到 j 的大寫字母,按字母降序排序后,將連續(xù)出現(xiàn)的字用壓縮形式顯示。例如,連續(xù) 5 個(gè) h 字母顯示為 5*
9、h,參見圖例。數(shù)組a()用于存放隨機(jī)產(chǎn)生的字母。private sub command1_click() const n = 15 dim a(1 to n) as string * 1, c dim count%, i%, j%, k% for i = 1 to n a(i) = chr(int(rnd*10+65 ) print a(i); next i print for i = 1 to n - 1 k = i for j = i+1 to n if a(j) a(k) then k=j next j c = a(i): a(i) = a(k): a(k) = c next i for
10、 i = 1 to n print a(i); ; next i print i = 1 do while i = n count = 1 if i n then j = i + 1 do while a(i) = a(j) count = count+1 if j n then j = j + 1 else exit do loop if count = 1 then print a(i); ; else print count; *; a(i); ; i =i+count loop end sub 6. 本程序的功能是在窗體隨機(jī)產(chǎn)生10 個(gè)長度為120 之間的大寫字母字符串,并同時(shí)顯示最長
11、字符串的字符數(shù)及內(nèi)容,如圖所示。private sub command1_click() cls dim st(1 to 10) as string, c as string * 1 dim i%, n% for i = 1 to 10 _ _ for j = 1 to n c = chr(int(rnd * 26 + 65) st(i) = _ _ _next j print st(i) next i maxlen = 0 maxstr = for i = 1 to 10 if _ _ then maxlen = len(st(i) maxstr = _ end if next i _ _e
12、nd sub 7. 兩素?cái)?shù)的差為2,稱此對(duì)素?cái)?shù)為素?cái)?shù)對(duì),下列程序是成對(duì)顯示100 以內(nèi)的素?cái)?shù)對(duì)。其中,函數(shù)isp的功能是判斷參數(shù)m 是否為素?cái)?shù),若是返回true ,否則返回false 。public function isp(m) as boolean dim i% for i = 2 to int(sqr(m) if then isp = false next i end function private sub command1_click() dim i% p1 = isp(3) 第一個(gè)可能的素?cái)?shù)for i = p2 = isp(i) if then print i 2, i 相鄰的兩個(gè)
13、是素?cái)?shù),則打印p1 = 處理下一個(gè)素?cái)?shù)next i end sub 8. 本程序的功能是求100 以內(nèi)的素?cái)?shù),結(jié)果分三列輸出,如樣例所示。算法思想:從2開始,對(duì)每一個(gè)數(shù)判斷是否是素?cái)?shù),若是就輸出,否則就繼續(xù),直至到100。private sub command1_click() dim n as integer, m as integer, i as integer me.cls for n = 2 to 100 for m = 2 to n 1 if = 0 then end if next m if then print n, i = i + 1 if i mod 3 = 0 then e
14、nd if next n end sub 9. 子過程 ish 的功能是判斷正整數(shù)n 是否是回文數(shù),若為回文數(shù),則參數(shù) tag返回 true。單擊命令按鈕,輸出隨機(jī)產(chǎn)生的10 個(gè)10 ,100 之間的回文數(shù)(數(shù)字左右對(duì)稱的正整數(shù)稱為回文數(shù))。private sub command1_click() dim nn%, t as boolean, i% for i = 1 to 10 nn = int(rnd * 991 + 10) call if t then print nn; 是回文數(shù) next i end sub sub ish(byval n as integer, ) dim leng
15、th%, i%, s1$ s1 = trim(str(n) tag = length = for i = 1 to length 2 if mid(s1, i, 1) then tag = false next i end sub 10. 本程序的功能是判斷一個(gè)5 位數(shù)的最后二位構(gòu)成的數(shù)是否為素?cái)?shù)。在窗體的文本框text1 中輸入一個(gè) 5 位數(shù),判斷該數(shù)的第4、5 二位構(gòu)成的數(shù)是否為素?cái)?shù),若是顯示“是素?cái)?shù)”否則為“非素?cái)?shù)”。例如,32517最后二 17構(gòu)成的數(shù)是素?cái)?shù)。private sub command1_click() dim tag as boolean m = m = val(m) f
16、or i = 2 to if = 0 then tag = false next i if then print m; 是素?cái)?shù) else print m; 非素?cái)?shù) end if end sub 11. 隨機(jī)產(chǎn)生 100 個(gè)在閉區(qū)間 30 ,80 之間的整數(shù),存放于數(shù)組a 中;并將其中大于50的偶數(shù)再存于數(shù)組b 中;然后,對(duì)數(shù)組 b 用冒泡法從大到小排序后輸出。子過程output的功能是按每行十個(gè)元素輸出數(shù)組內(nèi)容。private sub form_click() dim a(1 to 100) as integer, b(100) as integer, m as integer, n as i
17、nteger randomize n = 0 for i = 1 to 100 a(i) = int(rnd * 51 + 30) if a(i) mod 2 = 0 then n = n + 1 b(n) = end if next i call output(a(), 100) for i = 1 to for j = 1 to n - i if then t = b(j): b(j) = b(j + 1): b(j + 1) = t end if next j next i call end sub private sub output(x() as integer, t as inte
18、ger) print 數(shù)組輸出: for i = 1 to t print x(i); if i mod 10 = 0 then print next i print end sub 12. 本程序的功能是用于判斷輸入的正整數(shù)是否為升序數(shù)。使用輸入對(duì)話框輸入數(shù)據(jù), 判斷結(jié)果顯示在標(biāo)簽框 lable1 中。設(shè)正整數(shù) n=d1d2dm,如果 di then flag = false end if next i if then label1.caption = n & “是升序數(shù)” else label1.caption = n & “不是升序數(shù)” end if end sub 13
19、. 下列程序?qū)⒔o定范圍內(nèi)的偶數(shù)(大于等于4),表示為兩個(gè)素?cái)?shù)之和。圖例所示為輸入10和 30 后運(yùn)行結(jié)果。函數(shù)子過程fun() 的功能是判斷數(shù)值n 是否是為素?cái)?shù),若是返回1,否則返回 0。private sub command1_click() dim a%, b%, i%, c%, d% a = val(text1.text): b = val(text2.text) if then t = a: a = b: b = t end if for i = a to b step 2 do c = c + 2 d = i - c loop until = 1 picture1.print i;
20、=; c; +; d next i end sub function fun(n as integer) dim x!, k% for k = 2 to n - 1 if n mod k = 0 then fun = 0 end if next k end function 14. 下面程序的功能是統(tǒng)計(jì)文本框中每個(gè)數(shù)字字符( “0”-“9”)出現(xiàn)的次數(shù)。用 num(0)來存放字符“ 0”的個(gè)數(shù),用 num(1)來存放字符“ 1”的個(gè)數(shù),用num(9)來存放字符“ 9”的個(gè)數(shù)。字符串由用戶在文本框中輸入,結(jié)果在圖形框中輸出。private sub command1_click() dim num
21、(9) as integer dim i%, m%, j%, c as string * 1, s$ m = for i = 1 to m c = mid(text1.text, i, 1) if then j = val(c) end if next i for i = 0 to 9 if then picture1.print 數(shù)字 ; ; 出現(xiàn)次數(shù)為 ; num(i) end if next i end sub 15. 下列程序?qū)⑶蟪?10000 之內(nèi)的同構(gòu)數(shù)。 并且在窗體標(biāo)題欄上顯示找到同構(gòu)數(shù)的個(gè)數(shù),如圖所示。所謂同構(gòu)數(shù)是指,該整數(shù)會(huì)出現(xiàn)在其平方數(shù)的最右端。例如:6 出現(xiàn)在 36 的最
22、右端,76 出現(xiàn)在 5776的最右端。子函數(shù)judge() 的功能是判定同構(gòu)數(shù),若是judge 返回 true,否則返回 false 。判定同構(gòu)數(shù)算法思想:將數(shù)n 逐位與 n 的平方數(shù)相同位置上的值相比較。private sub command1_click() dim i as long m = 0 for i = 1 to 10000 if then m = m + 1 list1.additem str(i) list2.additem str(i * i) end if next i = 10000以內(nèi)的同構(gòu)數(shù): & m & 個(gè) end sub private func
23、tion judge(byval n as long) as boolean dim n2 as long n2 = n * n judge = true do while if (n mod 10) = then n = n 10 n2 = n2 10 else exit do end if loop end function 16.下列程序中的子過程fenge用于實(shí)現(xiàn)函數(shù) split()的功能(字符分離到數(shù)組) 。通過在文本框中輸入一個(gè)以逗號(hào)分隔的數(shù)字序列,按回車后調(diào)用過程fenge ,將數(shù)字序列分離后,轉(zhuǎn)換為數(shù)值型數(shù)據(jù)存放于數(shù)組shuzhi()中,并在 picturebox中輸出該數(shù)組的
24、所有值(假設(shè)不存在輸入錯(cuò)誤,輸入的數(shù)字元素個(gè)數(shù)在100個(gè)以內(nèi)) 。private sub text1_keypress(keyascii as integer) if keyascii = 13 then call fenge() end if end sub private sub fenge(str1) dim i as integer, j as integer, shuzhi(100) as single i = 0 j = instr(str1, ,) 檢測第一個(gè)逗號(hào)的位置do while j 0 shuzhi(i) = val() 分離數(shù)字序列str1 = mid() i = i
25、+ 1 j = instr(str1, ,) loop 最后一個(gè)數(shù)字for j = 0 to i next j end sub 17. 下列程序完成如下功能:在文本框中輸入一個(gè)20 以內(nèi)的正整數(shù)n,單擊“開始”按鈕后,判斷輸入的有效性,如果文本框中輸入數(shù)越界,給出如圖所示的相關(guān)提示,并要求重新輸入,否則隨機(jī)產(chǎn)生一個(gè)大寫英文字母,然后以此英文字母為首字母,連續(xù)循環(huán)變化,輸出一個(gè) n*n 的字符矩陣,并顯示“首字母x 的 n 階字符矩陣”的說明,圖示為首字母k 的 8階字符矩陣。private sub command1_click() dim i%, k%, n% n = val(text1) i
26、f n 20 then text1 = text1.setfocus exit sub end if s = int(rnd * 26) + 65 隨機(jī)產(chǎn)生一個(gè)大寫字母的ascii 碼label1 = 首字母 & 的 & n & 階字符矩陣 cls print: print k = 以下輸出 n*n 的字符矩陣, k 為字符控制變量for i = 1 to n print tab(5); for j = print chr(65 + k); space(1); k = k + 1 if 對(duì)字符控制變量進(jìn)行處理next j print next i end sub 18. 將 100至 150 之間的偶數(shù),拆分成兩個(gè)素?cái)?shù)之和(只要一對(duì)就可以了),最后輸出格式如圖所示。函數(shù)子過程prime 用于判斷一個(gè)數(shù)是否為素?cái)?shù)。private function prime(byval x as integer) as boolean dim i as integer prime = for i = 2 to sqr(x) if then prime = false exit function end if next i end f
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 深圳股權(quán)轉(zhuǎn)讓合同(2025年版)4篇
- 商場LED顯示屏租賃合同(二零二五年)
- 二零二五年度國際法學(xué)與留學(xué)項(xiàng)目合同3篇
- 2025年度個(gè)人一手房買賣合同環(huán)保標(biāo)準(zhǔn)范本4篇
- 2025版戶外休閑場所草皮采購與租賃合同3篇
- 2025年智能家居系統(tǒng)產(chǎn)品銷售激勵(lì)協(xié)議書2篇
- 2025版團(tuán)購樓房指標(biāo)轉(zhuǎn)讓與房地產(chǎn)咨詢代理合同3篇
- 2025版智能防蚊紗窗研發(fā)與銷售合作協(xié)議3篇
- 2025年度個(gè)人投資分紅收據(jù)模板制作服務(wù)協(xié)議4篇
- 2025年度互聯(lián)網(wǎng)金融服務(wù)提供商合作協(xié)議范本4篇
- 骨髓穿刺課件
- 鄉(xiāng)村治理中正式制度與非正式制度的關(guān)系解析
- 2024版義務(wù)教育小學(xué)數(shù)學(xué)課程標(biāo)準(zhǔn)
- 智能護(hù)理:人工智能助力的醫(yī)療創(chuàng)新
- 國家中小學(xué)智慧教育平臺(tái)培訓(xùn)專題講座
- 5G+教育5G技術(shù)在智慧校園教育專網(wǎng)系統(tǒng)的應(yīng)用
- VI設(shè)計(jì)輔助圖形設(shè)計(jì)
- 淺談小學(xué)勞動(dòng)教育的開展與探究 論文
- 2023年全國4月高等教育自學(xué)考試管理學(xué)原理00054試題及答案新編
- 河北省大學(xué)生調(diào)研河北社會(huì)調(diào)查活動(dòng)項(xiàng)目申請(qǐng)書
- JJG 921-2021環(huán)境振動(dòng)分析儀
評(píng)論
0/150
提交評(píng)論