谢谢!我向你学习,学习学习这个程序!
下面是网上搞到的加减法程序,0是加法,1是减法,速度比我的程序快一点好象,时间是此程序为E-03,我的程序时间是E-02,相同的数据,是差一个小数点吧?
Private Sub Command1_Click()
Dim t As Double
t = Timer
Text3 = js(Trim(Text1), Trim(Text2), 0)
Text4 = Timer - t
End Sub
Private Sub Command2_Click()
Dim t As Double
t = Timer
Text3 = js(Trim(Text1), Trim(Text2), 1)
Text4 = Timer - t
End Sub
Private Sub Command3_Click()
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
End Sub
'2个长整数的加减法运算(最大长度无限)
Function js(num1 As String, num2 As String, mType As Integer) As String
Dim a1 As String, a2 As String
Dim s1() As String, S2() As String, Resu() As String
Dim I As Integer, J As Integer, k As Integer, Tmp As String
Dim t1 As Integer, t2 As Integer, JW As Integer, Fh As String
If Not IsNumeric(num1) Or Not IsNumeric(num2) Then
MsgBox "参于运算的只能是数字,不能含有其他字符", vbCritical, "错误提示"
Exit Function
End If
If Len(num1) < Len(num2) Or (Len(num1) = Len(num2) And Left(num1, 1) < Left(num2, 1)) Then
a1 = num2
a2 = num1
Fh = IIf(mType = 1, "-", "") '减法运算出现负数的情况
Else
a1 = num1
a2 = num2
End If
I = Len(a1)
J = Len(a2)
ReDim s1(I - 1), S2(J - 1)
ReDim Resu(IIf(mType = 0, I, I - 1))
For k = Len(a1) To 1 Step -1 '把数a1逐个放入数组s1
s1(I - k) = Mid(a1, k, 1)
Next
For k = Len(a2) To 1 Step -1 '把数a2逐个放入数组s2
S2(J - k) = Mid(a2, k, 1)
Next
JW = 0
For k = 0 To UBound(Resu) '从个位数开始相加减,结果入入数组resu
If k > UBound(s1) Then
t1 = 0
Else
t1 = Val(s1(k))
End If
If k > UBound(S2) Then
t2 = 0
Else
t2 = Val(S2(k))
End If
Select Case mType
Case 0 '加法运算
Tmp = t1 + t2 + JW
If Len(Tmp) > 1 Then
JW = Val(Left(Tmp, Len(Tmp) - 1))
Else
JW = 0
End If
Tmp = Right(Tmp, 1)
Case 1 '减法运算
I = t1 + JW - t2
If I < 0 Then
Tmp = I + 10
JW = -1
Else
Tmp = I
JW = 0
End If
End Select
Resu(k) = Tmp
Next
J = UBound(Resu)
For i = 0 To j '合并数组resu,结果输出到js1
js = js & Resu(J - I)
Next
For i = 1 To Len(js) '去掉前面的0
If Mid(js, I, 1) > "0" Then Exit For
Next
js = Fh & Mid(js, I)
If Len(js) = 0 Then
js = 0
Else
js = js
End If
End Function
[此贴子已经被作者于2020-2-23 19:03编辑过]