二、编程题:
1 判断一个数是否是素数。
Private sub form_click()
Dim x as integer,I as integer
X=text1
For i=2 to sqr(x) ‘
If x mod i=0 then exit for
Next i
If i>sqr(x) then
Text2=”x是素数”
Else
Text2=”x不是素数”
Endif
Endsub
2、 求两个任意自然数的最大公约数。
Private sub form_click()
Dim m as integer,n as integer,I as integer,r as integer
m=text1
n=text2
R=m mod n
Do while r<>0
M=n
N=r
R=m mod n
Loop
Text2=str(n)
End sub
3、 随机生成10个两位正整数,将其中的奇数和偶数分别显示在list1和list2中 。
Private sub form_click()
Dim a(1 to 10) as integer,I as integer
For i=1 to 10
A(i)=int(rnd*90+10)
Next i
For i=1 to 10
If a(i) mod 2=1 then
List1.additem str(a(i))
Else
List2.additem str(a(i))
Endif
Next i
End sub
4、判断一个数是否是回文数。
Private sub form_click()
Dim I as integer,s as string
S=text1
For i=1 to len(s)\\2
If mid(s,I,1)<>mid(s,len(s)-i+1,1) then exit for
Next i
If i>len(s)\\2 then
Print s & ”是回文数”
Else
Print s & ”不是回文数”
Endif
End sub
5求两个自然数的最小公倍数。
Private sub form_click()
Dim m as integer ,x as integer,y as integer,flg as Boolean
X=text1:y=text2
Flg=false
Do until flg
m=m+x
If m mod y=0 then
Flg=true
Ensif
Loop
Text2=str( m)
End sub
注:还可用先求最大公约数,再求最小公倍数的方法。
6 从键盘接收一个字符,判断其是否是大写字母、小写字母、数字或其他字符。
Private sub form_click()
Dim x as string
x = Text1
Select Case x
Case "A" To "Z"
Text2 = "x是大写字母"
Case "a" To "z"
Text2 = " x是小写字母"
Case "0" To "9"
Text2 = " x是数字"
Case Else
Text2 = "x是其他字符"
End select
End sub
7判断一个三位正整数是否是水仙花数。
Private sub form_click()
Dim a as integer,b as integer,c as integer,x as integer
X=val(text1)
A=x\\100
B=(x-a*100)\\10
C=x-a*100-b*10
If x=a^3+b^3+c^3 then
Text2=str(x)+”是水仙花数”
Else
Text2=str(x)+”不是水仙花数”
Endif
End sub
8任意生成10个两位正整数,将这10个数的和显示在text1中,平均值显示在text2中。
Private sub form_click()
Dim I as integer,x as integer ,s as integer
S=0
For i=1 to 10
X=int(rnd*90+10)
S=s+x
Next i
Text1=str(s)
Text2=str(s/10)
End sub
9求出斐波那契数列的前1,并按顺序将他们显示在窗体上。斐波那契数列的递推公式如下:
Private sub form_click()
Dim fb(1 to 18) as integer,I as integer
Fb(1)=1:fb(2)=1
For i=3 to 18
Fb(i)=fb(i-1)+fb(i-2)
Next i
For i=1 to 18
Print fb(i);
Next i
End sub
10求100以内的偶数和,将结果显示在窗体上。
Private sub form_click()
Dim I as integer,s as integer
S=0
For i=2 to 100 step 2
S=s+i
Next i
Print s
End sub
11任意生成10个两位正整数,使用选择法对其进行升序排序。
Private sub form_click()
Dim a(10) as integer,I as integer,j as integer
For i=1 to 10
A(i)=int(rnd*90+10)
Next i
For i=1 to 9
For j=i+1 to 10
If a(i)>a(j) then
T=a(i):a(i)=a(j):a(j)=t
Endif
Next j
Next i
For i=1 to 10
Print a(i);
Next i
End sub
12求6!,将阶乘结果显示在窗体上。
Private sub form_click()
Dim I as integer,t as long
T=1
For i=1 to 6
T=t*i
Next i
Print t
End sub
13求100以内的奇数和,并将结果显示在窗体上。
Private sub form_click()
Dim s as integer,I as integer
S=0
For i=1 to 100 step 2
S=s+i
Next i
Print s
End sub
14任意生成10个两位正整数,使用冒泡法对其进行排序。
Private Sub form_click()
Dim a(1 to 10) As Integer, I as integer , J as integer, t As Integer
For I = 1 To 10
a(I) = Int(Rnd * 90 + 10)
Next I
For I = 1 To 9
For J = 1 To 10-i
If a(J) > a(J + 1) Then
t = a(J): a(J) = a(J + 1): a(J + 1) = t
End If
Next J
Next I
For I = 1 To 10
Print a(I);
Next I
End Sub
15从键盘接收一个数字,判断其是否小于0,等于0或大于0。
Private sub form_click()
Dim x as integer
X=inputbox(“输入一个数字”)
If x<0 then
Print “输入的数字小于0”
Elseif x=0 then
Print “输入的数字等于0”
Else
Print “输入的数字大于0”
Endif
End sub
16 将20以内的素数求出来,显示在text1文本框中。
Private sub form_click()
Dim I as integer,j as integer
For i=1 to 20
For j=2 to sqr(i) ‘也可用i-1或i\\2代替sqr(i)
If I mod j=0 then exit for
Next j
If j>sqr(i) then
Text1=text1 & str(i)
’显示在列表框用list1.additem I , 显示在图片框用picture1.print i
Endif
Next i
End sub
17 判断一个三位的正整数是否是升序数。
Private sub form_click()
Dim x as integer ,a as integer ,b as integer ,c as integer
X=inputbox(“输入一个三位的正整数”)
A=x\\100
B=(x-a*100)\\10
C=x-a*100-b*10
If a Print “x是升序数”
Else
Print “x不是升序数”
Endif
End sub
18编写程序,求下面函数的值,将结果显示在窗体上。函数如下:
Private sub form_click()
Dim x as integer ,y as single
X=val(text1)
If x<=0 then
Y=2-x
Elseif x>0 and x<=2 then
Y=x+2
Elseif x>2 and x<=5 then
Y=x^2
Else
Y=25-x
Endif
Print y
End sub
19 将任意输入的三个数按照从大到小的顺序输出。
Private sub form_click()
Dim I as integer ,j as integer ,t as single
Dim x(1 to 3) as single
A=text1:b=text2:c=text3
X(1)=a:x(2)=b:x(3)=c
For i=1 to 2
For j=i+1 to 3
If x(i) Endif Next j Next i For i=1 to 3 Print x(i) Next i End sub 20将100以内的素数求出来,显示在picture1中。 Private sub form_click() Dim I as integer ,j as integer Picture1.print 2 For i=3 to 100 step 2 For j=2 to sqr(i) If I mod j=0 then exit for Next j If j>sqr(i) then Picture1.print i Endif Next i End sub 21随机生成10两位正整数,将这10个数中的最小值求出来,并显示在窗体上。 Private sub form_click() Dim I as integer ,min as integer ,x as integer Min=1000 For i=1 to 10 X=int(rnd*90+10) If min>x then min=x Next i Print min End sub 22在text1中输入任意一字符串,将其逆序显示在text2中。例如:在text1中输入字符串”abcde”,将其逆序后,结果为”edcba”。 Private sub form_click() Dim I as integer,x as string X=text1 For i=1 to len(x) Text2=mid(x,I,1) & text2 Next i End sub 23随机生成10个三位正整数,统计其中奇数和偶数的个数,将统计结果显示在窗体上。 Private sub form_click() Dim I as integer ,j as integer ,k as integer ,x as integer J=0:k=0 For i=1 to 10 X=int(rnd*900+100) If x mod 2=1 then J=j+1 Else K=k+1 Endif Next i Print j,k End sub 24求1+2+3+…+100的和,结果显示在窗体上。 Private sub form_click() Dim s as v integer ,I as integer For i=1 to 100 S=s+i Next i Print s End sub 25将任意四位正整数的每一位上数字取出来。要求:利用text1接收一个四位数,每位数字分别显示在text2、text3、text4和text5中。 Private sub form_click() Dim x as integer ,a as integer ,b as integer ,c as integer ,d as integer X=val(text1) A=x\\1000 B=(x-a*1000)\\100 C=(x-a*1000-b*100)\\10 D=x-a*1000-b*100-c*10 ‘可用d=x mod 10代替 Text2=str(a) Text3=str(b) Text4=str(c) Text5=str(d) End sub