English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
所有分類

請問高手:
有一個題目
輸入X值,利用副程式計算n!,並計算及輸出下列e^x的結果:
e^x=1+x+x^2/2!+......+x^n/n!(計算至|x^n/n!|<10^-5
這是小弟的做法:

Private Sub Form_Activate()
x = InputBox("請輸入數字")
Dim i As Integer, p As Integer
e = 1
For i = 1 To x
Call fac(i, p)
e = e + i ^ p / p
Next i
Print e
End Sub
Sub fac(a As Integer, b As Integer)
b = 1
For i = 1 To a
b = b * i
Next i
End Sub
希望高手可以指點一下...多謝....

2006-12-15 11:23:07 · 1 個解答 · 發問者 ? 5 in 電腦與網際網路 程式設計

1 個解答

這段錯誤
For i = 1 To x
Call fac(i, p)
e = e + i ^ p / p
Next i

改成
e = 1
i = 0
Do
i = i + 1
Call fac(i, p)
y = (x ^ i / p)
e = e + y
Loop Until Abs(y) < 10 ^ (-5)

變數宣告為
Dim i As Integer, p As Single, x As Integer
Dim e As Single, y As Single

b的型態也要改
Sub fac(a As Integer, b As Single)

2006-12-15 12:25:52 · answer #1 · answered by JJ 7 · 0 0

fedest.com, questions and answers