# 怎麼用vb寫出這些題目,請大大幫忙 "

*

**

***

****

.

.

.

***********(n個)

### 1 個解答

• 1 0 年前
最佳解答

=====================================

階乘(Factorial)，適用於 Visual Basic 6

=====================================

Option Explicit

Private Const mstrZero As String = &quot;0&quot;

Private Const mstrOne As String = &quot;1&quot;

Private Const mstrTwo As String = &quot;2&quot;

Private Const mstrThree As String = &quot;3&quot;

Private Const mstrFour As String = &quot;4&quot;

Private Const mstrFive As String = &quot;5&quot;

Private Const mstrDot As String = &quot;.&quot;

Private Const mstrNeg As String = &quot;-&quot;

Private Const basechr As String = &quot;B&quot;

Private Const basesep As String = &quot;,&quot;

Private Const mintAsc0 As Integer = 48

Private Const negative As Integer = -1

Private Const positive As Integer = 1

Private Const mintMaxLongDigit As Integer = 8

Private Sub Command1_Click()

Dim strX As String

strX = dhFactorial(Text1.Text)

txtResult.Text = strX

End Sub

&#39; 目 的: 階乘(Factorial)。

Public Function dhFactorial(ByVal strX As String) As String &#39;, strOutput As String)

Dim lngIndex As Long

Dim lngLast As Long

Dim lngNumber As Long

Dim lngProduct As Long

Dim strMultiply As String

Dim strOutput As String

Dim strSwitch As String

Dim strTemp As String

If Len(strX) &lt;= mintMaxLongDigit Then GoSub bfLong

dhFactorial = strOutput

Exit Function

bfLong:

strOutput = mstrOne

For lngNumber = 2 To CLng(Val(strX))

strMultiply = strOutput

lngLast = Len(strMultiply)

strOutput = Space(lngLast)

lngProduct = 0

For lngIndex = lngLast To 1 Step -1

lngProduct = lngProduct + Val(Mid(strMultiply, lngIndex, 1)) * lngNumber

Mid(strOutput, lngIndex, 1) = Chr(mintAsc0 + CInt(lngProduct Mod 10))

lngProduct = lngProduct \ 10

Next lngIndex

If lngProduct Then

strOutput = LTrim(Str(lngProduct)) & strOutput

End If

Next lngNumber

Return

End Function