在主窗体上拽出一个Command1按钮和text1文本框
(以下为递归方法实现)
Public Function f(n As Long) As Long
if n=1 then
f=1
else
f=n*f(n-1)
endif
end function
Private Sub Command1_Click()
dim c as Long,d as long
c=Val(text1.text)
d=f(c)
print d
end sub
这个最多算到200边上吧,要想更大的精确结果,只能递推+高精度计算了。不过如果对于计算机二级那高精度计算肯定不要求的。
(以下为递推方法的函数,如果感兴趣的话高精度这个自己去查,不是难事)
Public Function f(n As Long) As Long
dim temp as long,i as long
for i=1 to n
temp=temp*i
next i
f=temp
end function
函数只能计算12以前的阶乘是因为变量类型定义不恰当。
请参照http://zhidao.baidu.com/question/292688477.html
Private Sub form_click()
Dim m As Long, n As Long, s As Single
m = Val(Text1.Text)
n = Val(Text2.Text)
If m <= n Then
Print "M不应小于N!请重新输入!"
Else
s = jc(m) / (jc(n) * jc(m - n))
Print s
End If
End Sub
Private Function jc(n As Long) As Single
Dim sum As Single, i As Long
sum = 1
For i = 2 To n
sum = sum * i
Next i
jc = sum
End Function
创建一个按钮名为Command1,点击运算
Private Sub Command1_Click()
Dim i%, n%, ans As Long
n = InputBox("输入n")
MsgBox Calc(n)
End Sub
Public Function Calc(n As Integer) As Long
Dim i%
Calc = 2
For i = 3 To n
Calc = Calc * i
Next
End Function