关于森林的著名画作:vb高手请进来

来源:百度文库 编辑:高考问答 时间:2024/04/29 07:23:59
1. 输入一个字符串,要求分别统计该字符串中出现符号“(”和“)”的次数,并用MsgBox输出统计结果。

2. 编程序计算:1-1/2!+1/3!-1/4!+….+(-1)n-1/n!
其中n为整数,由键盘输入;
n!=n*(n-1)*(n-2)*….*1

3. 编写程序,显示出所有的水仙花数。所谓水仙花数,是指一个3位数,其各位数字立方和等于该数字本身。例如:153是水仙花数,因为153=13+53+33。

每一题均在窗体中画一个命令按钮,然后将代码拷入即可

第一题:
Private Sub Command1_Click()
Dim Lc As Integer, Rc As Integer, i As Integer, tempPos As Integer
Dim inputStr As String, tempStr As String

inputStr = InputBox("请输入字符串", , "这是一个包含有“(”和“)”的字符串")

For i = 1 To Len(inputStr)

tempStr = Mid(inputStr, i, 1)

If tempStr = "(" Then
Lc = Lc + 1
ElseIf tempStr = ")" Then
Rc = Rc + 1
End If
Next

MsgBox "字符串中出现符号“(”的次数为: " & Lc & vbCrLf & "字符串中出现符号“)”的次数为: " & Rc
End Sub

第二题:
Private Sub Command1_Click()
Dim outputNum As Double
Dim i As Integer, inputNum As Integer
inputNum = Val(InputBox("请输入一个正整数", , 100))
For i = 1 To inputNum
outputNum = outputNum + Totle(i)
Next
MsgBox "结果为: " & vbCrLf & outputNum
End Sub

Private Function Totle(n As Integer) As Double
Dim i As Integer
Totle = 1
For i = 1 To n
Totle = Totle * i
Next
Totle = ((-1) ^ (n - 1)) / Totle
End Function

第三题:
Private Sub Command1_Click()
Dim a As Integer, b As Integer, c As Integer
For a = 1 To 9
For b = 0 To 9
For c = 0 To 9
If a ^ 3 + b ^ 3 + c ^ 3 = a * 100 + b * 10 + c Then
Print a * 100 + b * 10 + c
End If
Next
Next
Next
End Sub