模(一 二)
1.从键盘上输入3个数分别放入x y z中,编程使x中的数最大,z中的数最小。
Private Sub Form_Click()
x = Val(InputBox("请输入x"))
y = Val(InputBox("请输入y"))
z = Val(InputBox("请输入z"))
If x < y Then t = x: x = y: y = t
If y < z Then t = y: y = z: z = t
If x < y Then t = x: x = y: y = t
Print x; y; z
End Sub
2.用近似公式求自然对数e的值,精确到1/n!<F,F由用户输入。e=1+1/1!+1/2!+……1/n!
Private Sub Form_Click()
Dim F#, e#, n%, t&
e = 1: n = 1: t = 1
F = Val(InputBox("输入F"))
Do
e = e + 1 / t
n = n + 1
t = t * n
Loop Until 1 / t <= F
Print e
End Sub
3.d:\a.txt复制为d:\b.txt,要求不能直接使用Filecopy语句。
Private Sub Command1_Click()
Dim sline As String
Open "d:\a.txt" For Input As 1
Open "d:\b.txt" For Output As 2
While Not EOF(1)
Line Input #1, sline
Print #2, sline
Wend
Close
Print "Copy finished"
End Sub
4.统计200~300中素数的个数,显示在窗体上,素数的判断用自定义函数judge来实现。
Function judge(ByVal i As Long) As Boolean
For j = 2 To m
If i Mod j = 0 Then Exit For
Next j
If j > m Then judge = True Else judge = False
End Function
Private Sub Command1_Click()
Dim c As Integer
For i = 200 To 300
If judge(i) Then c = c + 1
Next i
Print "200300之间共有"; c; "个素数"
End Sub
5.模拟自动选号在文本框内显示30以内的7个数,且互不相同。
Private Sub Form_Click()
Dim a(1 To 7) As Integer
Randomize
a(1) = Int(Rnd() * 30) + 1
For i = 2 To 7
n = Int(Rnd() * 30) + 1
For j = 1 To i - 1
If a(j) = n Then Exit For
Next j
If j = i Then a(i) = n Else i = i – 1   
Next I   
Text1.Text = ""
For i = 1 To 7   
Text1.Text = Text1.Text & a(i) & " " 
Next i
End Sub
6.利用循环,打印输出下图。
*
***
*****
*******
*********
*******
*****
***
*
Private Sub Command1_Click()
For i = 1 To 5
Print Tab(20 - i); String(2 * i - 1, "*")
Next i
For i = 4 To 1 Step -1
Print Tab(20 - i); String(2 * i - 1, "*")
Next i
End Sub
7.3—100之间的所有孪生素数。
Private Function isprime(i) As Boolean
For j = 2 To i - 1
If i Mod j = 0 Then Exit For
Next j
If j = i Then
isprime = True
End If
End Function
Private Sub Form_Click()
printform
For i = 3 To 100
If isprime(i) And isprime(i + 2) Then
Print i; i + 2
End If
Next i
End Sub
8.随机产生10个两位整数放在数组a中,然后求出其中的最大值 最小值和平均值。
Private Sub Form_Click()
Dim a(1 To 10) As Integer
Max = 10
Min = 99
For i = 1 To 10
a(i) = Int(Rnd() * 90) + 10
Print a(i);
If a(i) > Max Then Max = a(i)
If a(i) < Min Then Min = a(i)
s = s + a(i)
Next i
Print
Print "max="; Max; "min="; Min; "average="; s / 10
End Sub
9.编程:单击“写入数据”按钮,程序将取10[10100]的随机数,并存入“abc.dat”,单击“读取数据”按钮,程序将从文件“abc.dat”中读出写入的10个数显示在窗体上,每行显示5个数。
Private Sub Command1_Click()
Dim i, a As Integer
Open "abc.dat" For Output As #1
Randomize
For i = 1 To 10
a = Int(Rnd() * 90) + 10
Write #1, a
Next i
Close #1
End Sub
Private Sub Command2_Click()
Dim i, a As Integer
Open "abc.dat" For Input As #1
Do While Not EOF(1)
Input #1, a
Print a;
i = i + 1
If i Mod 5 = 0 Then Print
Loop
Close #1
End Sub
10.小红12岁,父亲大她30岁,编程计算她父亲在几年后比她年龄大一倍,那时父女年龄各为多少?
Private Sub Form_Click()
Dim a As Integer
a = 12
Do While a * 2 <> a + 30
a = a + 1
Loop
Print "经过"; a - 12; ""
Print "父女的年龄分别是"; a + 30; a
End Sub
模拟卷3
1.根据上网时间计算上网费用,每月收费最多不超过100
Private Sub Command1_Click()
Dim a As Integer
a = Val(InputBox("请输入请输入上网时间"))
If a > 0 Then
Select Case a
  Case Is < 10
  Page = 30
  Case Is < 60
  Page = a * 2
  Case Else
  Page = a * 1.5
  End Select
Else
MsgBox "没有上网,无需交费"
End If
If Page > 100 Then Page = 100
Print "您本月共上网"; a; "小时,需交费"; Page; ""
End Sub
2.有一个台阶,每步跨2个台阶最后剩1个,32……..65个,7步恰好走完,问有多少台阶
Private Sub Command1_Click()
Dim a As Integer, i As Integer
Do While True
i = i + 1
If i Mod 2 = 1 And i Mod 3 = 2 And i Mod 4 = 3 And i Mod 5 = 4 And i Mod 6 = 5 And i Mod 7 = 0 Then
Print "该阶梯至少有"; i; ""
Exit Do
End If
Loop
End Sub
3.某大奖赛,7个评委打分,满分100分,对一名参赛者,输入7个评委的打分分数,去掉一个最高分,一个最低分,求出平均分为该参赛者的得分。编写程序输入评委的打分,输出参赛者的得分。
Private Sub Command1_Click()
Dim max As Integer, min As Integer
Dim i As Integer, s As Integer, f As Integer
max = 0
min = 100
For i = 1 To 7
f = InputBox("请输入评委的打分")
If f > max Then max = f
If f < min Then min = f
s = s + f
Next i
aver = (s - max - min) / 5
Print "参赛者最后得分为"; aver, max, min
End Sub
Private Sub Command1_Click()
Dim str1 As String, str2 As String
Dim i As Integer, k As String
str1 = InputBox("")
str2 = ""
k = Len(str1)
For i = k To 1 Step -1
str2 = str2 + Mid(str1, i, 1)
Next i
Print str2
End Sub
4输入任意长度的字符串,要求将字符顺序倒置,例如,将输入的“ABCDEFG”变成“GFEDCBA
Private Sub Command1_Click()
  Dim str1 As String, str2 As String
  Dim i As Integer, k As Integer
  str1 = InputBox("")
  str2 = ""

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。