在VFP中如何进行大数(至少128字节)的运算,而且还要支持16进制等其他进制的问题。
------解决方案--------------------------------------------------------
接上
*!* Function ll_div divides integer, represented by strings m.s1 by the
*!* integer, epresented by m.s1. (int(m.s1/m.s2)). If parameter m.md is
*!* passed by reference, mod(m.s1,m.s2) is returned in m.md
Function ll_div
Lparameter m.s1, m.s2, m.md
Do Case
Case Left(m.s1,1) = "-" And Left(m.s2,1) <> "-"
Return "-" + ll_div(Substr(m.s1,2), m.s2)
Case Left(m.s1,1) <> "-" And Left(m.s2,1) = "-"
Return "-" + ll_div(m.s1, Substr(m.s2,2))
Case Left(m.s1,1) = "-" And Left(m.s2,1) = "-"
Return ll_div(Substr(m.s1,2), Substr(m.s2,2))
Endcase
*!* Uncomment next rows if you want some additional data checking,
*!* but this may dramaticaly reduce performance on large strings
*!* local m.ln1, m.ln2, m.sm, m.i, m.ts1, m.ts1e, m.cnt
*!* if len(chrtran(m.s1, "0123456789", "")) > 0
*!* wait window "Long_ar: error in s1"
*!* return ""
*!* endif
*!* if len(chrtran(m.s2, "0123456789", "")) > 0
*!* wait window "Long_ar: error in s2"
*!* return ""
*!* endif
If ll_less(m.s1, m.s2)
m.md = m.s1
Return "0"
Endif
Local m.ln1, m.ln2, m.sm, m.i, m.ts1, m.ts1e, m.cnt
m.ln2 = Len(m.s2)
m.sm = ""
m.ts1 = Left(m.s1, m.ln2)
m.ts1e = Substr(m.s1, m.ln2 + 1)
m.ln1 = Len(m.ts1e)
For m.i = 1 To m.ln1 +1
m.cnt = 0
Do While !ll_less(m.ts1, m.s2)
m.ts1 = ll_sub(m.ts1, m.s2)
m.cnt = m.cnt + 1
Enddo
m.sm = m.sm + Allt(Str(m.cnt))
m.ts1 = m.ts1 + Left(m.ts1e, 1)
m.ts1e = Substr(m.ts1e, 2)
Next
m.sm = tr0(m.sm)
m.md = m.ts1
Return m.sm
*!* Function ll_mod returns mod(m.s1, m.s2)
Function ll_mod
Lparameter m.s1, m.s2
Do Case
Case Left(m.s1,1) = "-" And Left(m.s2,1) <> "-"
Return ""
Case Left(m.s1,1) <> "-" And Left(m.s2,1) = "-"
Return ""
Case Left(m.s1,1) = "-" And Left(m.s2,1) = "-"
Return ""
Endcase
Local m.md
ll_div(m.s1, m.s2, @m.md)
Return m.md
*!* Function ll_less compare two integers, represented by m.s1 and m.s2.
*!* .t. is returned if m.s1 < m.s2
Function ll_less
Lparameter m.s1, m.s2
Do Case
Case Left(m.s1,1) = "-" And Left(m.s2,1) <> "-"
Return .T.
Case Left(m.s1,1) <> "-" And Left(m.s2,1) = "-"
Return .F.
Case Left(m.s1,1) = "-" And Left(m.s2,1) = "-"
Return ll_less(Substr(m.s2,2), Substr(m.s1,2))
Endcase
m.s1 = tr0(m.s1)
m.s2 = tr0(m.s2)
Return Len(m.s1) < Len(m.s2) Or (Len(m.s1) = Len(m.s2) And m.s1 < m.s2)
*!* Function tr0 is used by function ll_less
Function tr0
Lparameter m.s1
Do While Left(m.s1, 1) = "0" And Len(m.s1) > 1
m.s1 = Substr(m.s1, 2)
Enddo
Return m.s1
*!* Funnction ll_intsqrt returns mod(m.s1, m.s2)
Function ll_intsqrt
Lparameter m.s1
*!* Uncomment next rows if you want some additional data checking,
*!* but this may dramaticaly reduce performance on large strings