字符串操作问题:
1. 统计出字符串中字符“a”出现的次数
Private Sub Form_Click()
Dim s$, i%, n%
s = InputBox("请输入一串字符串")
n = 0
For i = 1 To Len(s)
If Mid(s, i, 1) = "a" Then
n = n + 1
End If
Next i
Print n
End Sub
2. 统计出所有小写字母出现的次数
Private Sub Form_Click()
Dim s$, i%, n%
s = InputBox("请输入一串字符串")
n = 0
For i = 1 To Len(s)
If Mid(s, i, 1) >= "a" And Mid(s, i, 1) <= "z" Then
n = n + 1
End If
Next i
Print n
End Sub
3. 统计出所有大写字母出现的次数
Private Sub Form_Click()
Dim s$, i%, n%
s = InputBox("请输入一串字符串")
n = 0
For i = 1 To Len(s)
If Mid(s, i, 1) >= "A" And Mid(s, i, 1) <= "Z" Then
n = n + 1
End If
Next i
Print n
End Sub
4. 将字符串的字符顺序倒置
Private Sub Form_Click()
Dim s$, i%, c$
s = InputBox("请输入一串字符串")
c = ""
For i = Len(s) To 1 Step -1
c = c & Mid(s, i, 1)
Next i
Print c
End Sub
猴子摘桃问题:
1. 小猴摘了若干个桃,第一天吃掉一半多一个,第二天接着吃了剩余的一半多一个,以后每天都吃尚存的桃的一半多一个,到第七天要吃时只剩下1个了。问小猴最初摘了多少桃。
Private Sub Form_Click()
Dim X%, i%
X = 1
For i = 6 To 1 Step -1
X = (X + 1) * 2
Next i
Print X
End Sub
2. 某人第一天卖出一半货物,以后每天卖出剩下的一半,直到第7天还剩下100件,求原来共有多少货物。
Private Sub Form_Click()
Dim X%, i%
X = 100
For i = 6 To 1 Step -1
X = X * 2
Next i
Print X
End Sub
DO循环求和问题:
1. 编写程序计算S的值,直到最后一项小于10-4为止(要求将存放结果的变量类型定义成单精度型)
S=+++…
Private Sub Form_Click()
Dim s!, i%, t!
s = 0
i = 1
Do
t = 1 / (i * (i + 1))
s = s + t
i = i + 2
Loop Until t < 0.0001
Print s
End Sub
2. 编写程序计算S的近似值,直到最后一项的绝对值小于10-5次方为止,并用PRINT方法输出计算结果。其中
S=1- + - + … +(-1)K+1
Private Sub Form_Click()
Dim s!, i&, t!
s = 0
i = 1
Do
t = (-1) ^ (i + 1) / i
s = s + t
i = i + 1
Loop Until Abs(t) < 0.00001
Print s
End Sub
3. 利用公式
可以求出自然对数的底e的近似值,要求累加到最后一项的值小于0.000001为止。
Private Sub Form_Click()
Dim e!, i%, t!
e = 1: i = 1: t = 1
Do
t = t * i
e = e + 1 / t
i = i + 1
Loop Until 1 / t < 0.000001
Print e
End Sub
4. 编写程序计算S的近似值,直到最后一项的绝对值小于10-6为止(要求将存放结果的变量类型定义成单精度型)
S=+++…
答案1:
Private Sub Form_Click()
Dim s!, i%, t!
s = 0: i = 2: t = 1
Do
t = t * (i - 1) * i
s = s + 1 / t
i = i + 2
Loop Until 1 / t < 0.000001
Print s
End Sub
答案2:
Private Sub Form_Click()
Dim s!, i%, t!
s = 0: i = 2
Do
t = 1
For x = 1 To i
t = t * x
Next x
s = s + 1 / t
i = i + 2
Loop Until 1 / t < 0.000001
Print s
End Sub
FOR循环求和。
1. 从键盘上给出n,求S=1+(1+2)+(1+2+3)+…+(1+2+3+…+n)的值。
答案1:
Private Sub Form_Click()
Dim i%, j%, n%, s%
n = Val(InputBox("请输入n的值"))
s = 0
For i = 1 To n
For j = 1 To i
s = s + j
Next j
Next i
Print s
End Sub
答案2:
Private Sub Form_Click()
Dim i%, t%, n%, s%
n = Val(InputBox("请输入nprintform的值"))
s = 0
t = 0
For i = 1 To n
t = t + i
s = s + t
Next i
Print s
End Sub
2. 从键盘上给出n和a的值,求SN=a+aa+aaa+aaaa+…最后一项为n个a的值。
Private Sub Form_Click()
Dim i%, n%, a%, s%, t%
n = Val(InputBox("请输入n的值"))
a = Val(InputBox("请输入a的值"))
s = 0
t = 0
For i = 1 To n
t = t * 10 + a
s = s + t
Next i
Print s
End Sub
图形输出问题:
图1: 图2: 图3:
图1:
Private Sub Form_Click()
Dim i%, j%
For i = 1 To 5
Print Tab(5);
Print Spc(6 - i);
For j = 1 To 2 * i - 1
Print "*";
Next j
Next i
For i = 4 To 1 Step -1
Print Tab(5);
Print Spc(6 - i);
For j = 1 To 2 * i - 1
Print "*";
Next j
Next i
End Sub
图2:
Private Sub Form_Click()
Dim i%, j%
For i = 1 To 5
Print Tab(5);
Print Spc(6 - i);
For j = 1 To i
Print Chr(64 + j);
Next j
For j = i - 1 To 1 Step -1
Print Chr(64 + j);
Next j
Next i
End Sub
图3:
Private Sub Form_Click()
Dim i%, j%
For i = 1 To 5
Print Tab(5);
Print Spc(6 - i);
For j = 1 To i
Print Format(j, "0");
Next j
For j = i - 1 To 1 Step -1
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论