以下程序测试过正确工作。
text1, text2 tesx3 分别输入 A, B, C
text4 计算结果S
command1 计算S,结果放到text4中
Private Sub Command1_Click()
Dim s1 As Long, s2 As Long, s3 As Long
s1 = 1
s2 = 1
s3 = 1
For i = 2 To Val(Text1.Text)
s1 = s1 * i
Next i
For i = 2 To Val(Text2.Text)
s2 = s2 * i
Next i
For i = 2 To Val(Text3.Text)
s3 = s3 * i
Next i
Text4.Text = Str(s1 + s2 + s3)
End Sub
(阶乘数值很快就变大,即使是long型,也很快就溢出,测试是12!可过,13! 就溢出
可写子程序用指数形式表示大数,就是用 1.024 x 10^31的形式表示大数。)
假设text1是控件组
dim b as integer
for i= 0 to 39
if text1(i).text<>"" then
b = text1(i).text
s1=1
for c=1 to b
s1=s1*c
next
s=s+s1
end if
next
msgbox s