编写Function过程计算n!,调用该函数过程计算下式的值:e^x=1+x⼀1!+x^2⼀2!+x^⼀3!+...+x^n⼀n!

设x=1,2,3,4,5,6时,分别打印e^x的值,要求精度x^n/n!<0.0001
2024-11-17 06:37:55
推荐回答(1个)
回答(1):

不用任何控件,只要窗口足够大,点击窗口就可以了,已经运行过。程序如下(注意最后一段是为了计算n!而编写的Function过程。很简短不是?哈哈!):
Dim X As Byte, N As Byte, H As Single

Private Sub Form_Click()
Cls
Print "计算e^x=1+x/1!+x^2/2!+x^/3!+...+x^n/n!(精度x^n/n!<0.0001)"
For X = 1 To 6
N = 0
H = 0
Do
H = H + X ^ N / JC(N)
N = N + 1
Loop Until X ^ N / JC(N) < 0.0001
Print "e^"; X; "="; Format(H, "0.0000")
Next
End Sub

Private Sub Form_Load()
Form1.AutoRedraw = True
Print "计算e^x=1+x/1!+x^2/2!+x^/3!+...+x^n/n!(精度x^n/n!<0.0001)"
Print "在窗体空白处点击继续!"
End Sub

Private Function JC(A As Byte) As Double
If A = 0 Then JC = 1 Else JC = A * JC(A - 1)
End Function

希望能帮到你!