第一题:
计算函数:
Private Function JS(n As Long, x As Double) As Double
Dim mResult As Double
Dim i As Long
mResult = 0
For i = 0 To n - 1
mResult = mResult + ((-x) ^ i) / (i + 1)
Next
JS = mResult
End Function
第二题:
Private Sub Form_Click()
Dim i As Integer
Dim s, f As Double
i = 1
f = 1
s = 1
Do While (1 / f) > 10 ^ (-4)
f = f * i
s = s + 1 / f
i = i + 1
Loop
Print "e=" & Val(s)
End Sub
第一题:
运行条件:form1上面有3个控件,文本框text1,内容代表n,文本框text2,内容代表x,按钮command1,执行计算
Option Explicit
Private Sub Command1_Click()
MsgBox cal(Me.Text1, Me.Text2)
End Sub
Function cal(ByVal n As Integer, ByVal x As Single) As Single
Dim i As Integer
Dim result As Single
If Abs(x) >= 1 Then
cal = 0
End If
result = 0
For i = 0 To n - 1
result = result + ((-x) ^ i) / (i + 1)
Next
cal = result
End Function
Private Sub Command1_Click()
Dim a As Long, s As Long, D As Double, e As Double
D = 0: a = 1: s = 1: e = 1
Do Until Abs(e - D) < 0.0001
D = e
s = s * a
e = e + 1 / s
a = a + 1
Debug.Print e
DoEvents
Loop
Print e
End Sub