界面如上图所示, 其中公式用OLE 控件绘制,将该控件绘制到窗体中,点击右键,插入对象,选择microsoft 公式3 ,即可编辑。代码如下:
Private Sub Command1_Click()
Dim x, n As Integer
Dim p, q As Single
If (Val(Text1.Text) >= 0) And (Val(Text1.Text) >= 0) Then
n = Val(Text1.Text)
x = Val(Text2.Text)
p = x / n
q = f(n) / (f(x) * f((n - x))) * p ^ x * (1 - p) ^ (n - x)
Text3.Text = q
End If
End Sub
Function f(n)
Dim i As Long
f = 1
For i = 1 To n
f = f * i
Next
End Function
如有其它需要,请继续追问~~~
'未作错误处理,假设你的数据都是合法的
Private Sub Form_Load()
Dim f, x, p, n
n = InputBox("N:", , 10)
x = InputBox("X:", , 5)
p = x / n
f = fc(n) / (fc(x) * fc((n - x))) * p ^ x * (1 - p) ^ (n - x)
MsgBox f
Unload Me
End Sub
Function fc(n)
Dim i As Long
fc = 1
For i = 1 To n
fc = fc * i
Next
End Function