将整数拆分为数字(数百,数十,单位)并存储在ARRAYS中

时间:2014-04-22 03:30:18

标签: arrays vba split factorial digits

我正在研究一些代码,这些代码将计算非常大的阶乘产品,这些产品太大而不适合长整数。我将使用2个数组,F()和C()。 F用于乘法乘积并存储单位,C()用于"携带",稍后将添加。所以单位存储在F(I)中,例如,C(I + 1)保持数十,C(I + 2)保持数百。稍后你将添加C(I)+ F(I)。

Dim F(0 To 30) As Single, C(0 To 30) As Single, sum As Single
Dim length As Single, split1 As Single, split2 As Single
Dim nexttwo As String, mult As String

Sub LargeFactCalc()
    F(1) = 1
    N = InputBox("Enter the number you would like to factorialize: ")
    For J = 2 To N
        For I = 1 To 30
            mult = J * F(I)
            Call split
        Next I
        sum = C(I) + F(I)
        C(I + 1) = 0
        C(I + 2) = 0
    Next J

Print #1, F(I)
Close #1
End Sub

Sub split()
    Select Case Len(mult)
    Case 1
        F(I) = mult 'UNITS
    Case 2
        C(I + 1) = Left(mult, 1) 'Tens
        F(I) = Right(mult, 1) 'Units
    Case 3
        C(I + 2) = Left(mult, 1) 'Hundreds
        C(I+1) = 'Tens should go here but I don't 
                 'know how to seperate the middle number
        F(I) = Right(mult, 1) 'Units
    End Select
End Sub

2 个答案:

答案 0 :(得分:1)

这里需要的是Mid函数,它返回从特定字符开始的一些字符,或者组合或RightLeft函数。

尝试:

C(I + 1) = Mid(mult, 2, 1)  'Returns 1 character starting from the 2nd character of mult

或:

C(I + 1) = Left(Right(mult, 2), 1) 'Returns the left-most character, 
                                   'of the 2 right-most characters

答案 1 :(得分:0)

格式为Microsoft ACCESS 2010。

我有这种格式,可以将我的AMOUNT字段分成数百个,数十个和单位,然后以称为H,T和U的相同形式将它们输入到各个字段中。 如果大于999,我已经使百个H显示所有数字。即,如果结果为1234,则在T中的H 3中显示12,在U中的4中显示。 这是通过计算AMOUNT字段中的长度来完成的。它简单,简短,可爱,没有错误捕获。

代码在下面。

私人Sub AMOUNT_AfterUpdate()

将LenAmt设置为字符串

LenAmt = Len(AMOUNT)

H =中(右(AMOUNT,LenAmt),1,LenAmt-2)

T = Mid(Right(AMOUNT,2),1,1)

U = Mid(Right(AMOUNT,1),1,1)

我刷新

结束子