找回密码
 欢迎注册
查看: 5436|回复: 2

[原创] 多精度通用加法源码

[复制链接]
发表于 2016-12-12 22:00:40 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?欢迎注册

×
本帖最后由 落叶 于 2016-12-12 22:09 编辑

Type StrToZx                            '高精度数的结构头
            ZhFhBz As Boolean                        '正负号标志,正为1,负为0
            XsdWz As Long                          '小数点右边数字的长度。例1234.56中这个数是2
            JzBz As Integer                       '标记数组存的是多少进制的数(十进制或其它进制数)
            strlen As Long                         '操作数长度
            Zx() As Long                           '存放操作数的数组
            eE As Long                             '存放指数
        End Type

Public Function myADD(str1 As StrToZx, str2 As StrToZx) As StrToZx           '加法子程序,直接运算有符号整数,小数,和采用科学计数法表示的数
        Dim cc() As Long                                                     ' 存放字串1,和相加后得数
        Dim dd() As Long                                                     '存放字串2
        Dim jw As Integer                                                    '进位标志
        Dim jg As StrToZx                                                    '存放最终得数
        Dim string1 As StrToZx
        Dim string2 As StrToZx
        If zwbz = True Then
                Exit Function
        End If
        string1 = str1
        string2 = str2

        If string1.ZhFhBz = False And string2.ZhFhBz = True Then                   '符号相反做减法
            string1.ZhFhBz = True
            jg = myMINUS(string2, string1)
            GoTo line:
        End If
        If string1.ZhFhBz = True And string2.ZhFhBz = False Then                    '符号相反做减法
            string2.ZhFhBz = True
            jg = myMINUS(string1, string2)
            GoTo line:
        End If
        
        
        
        x = myCompare(string1, g0)                                                '两个加数中其中一个为零时快速计算
        y = myCompare(string2, g0)
        If x = 0 Then
           myADD = string2
           Exit Function
        End If
        If y = 0 Then
           myADD = string1
           Exit Function
        End If
     
        Dim cszd As Integer                                                         '转换成十进制
        cszd = 1
        string1 = Zjzzh(string1, cszd)
        string2 = Zjzzh(string2, cszd)
        edq string1, string2                                                        '指数对位,参数必需为十进制数
                           
        R1 = string1.XsdWz         '整形 例:123.1234567855+1234567855.123转换成0000000123.1234567855 +1234567855.1230000000
        L1 = string1.strlen - R1
        R2 = string2.XsdWz
        L2 = string2.strlen - R2
        If L1 >= L2 Then                                                           '保存两个数中小数点前方数字长度的最大值
            L = L1
        Else
            L = L2
        End If
        If R1 >= R2 Then                                                           '保存两个数中小数点后方数字长度的最大值
            R = R1
        Else
            R = R2
        End If
        Zlen = L + R
        ZlenTemp = Zlen + 1
        ReDim cc(-1 To ZlenTemp + 2) As Long                                           '字串对位
        ReDim dd(-1 To ZlenTemp + 2) As Long
        j = L - L1 + 2
        For i = 1 To string1.strlen                                                         '通过扩充方式进行字串对位,从左向右对位,但对位结果是向右看齐的
            cc(j) = string1.Zx(i)
            j = j + 1
        Next
        j = L - L2 + 2
        For i = 1 To string2.strlen                                                      '把123.1234567855扩充成0000000123.1234567855
            dd(j) = string2.Zx(i)
            j = j + 1
        Next

        jw = 0
        For i = ZlenTemp To 1 Step -1                                                     '加法运算
            cc(i) = cc(i) + dd(i) + jw
            If cc(i) >= 10 Then
                cc(i) = cc(i) - 10
                jw = 1
            Else
                jw = 0
            End If
        Next

        jg.JzBz = 1
        jg.strlen = ZlenTemp
        jg.XsdWz = R
        If (string1.ZhFhBz = False) And (string2.ZhFhBz = False) Then                              '符号都为负,前面加负号
            jg.ZhFhBz = False
        Else
            jg.ZhFhBz = True
        End If
        jg.Zx = cc()
        jg.eE = string1.eE
        jg = jdkz(jg, (jd + 4))
        jg = qslqwl(jg)
line:

        myADD = jg
        End Function
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2016-12-13 21:25:02 | 显示全部楼层
本帖最后由 落叶 于 2016-12-13 21:39 编辑

来个调试内存变化图
1.23456789+9.87654e3
操作数在内存中前后都多留两位的0
这个是两个加数传入的内存情况
无标题.png
下面是指数对位后两个加数的情况,加数在string1和strong2中
无标题1.png
下面中的cc和dd中存放加数扩充对位后的情况
无标题2.png
运算结果保存在cc中
无标题3.png




毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
 楼主| 发表于 2016-12-13 21:31:12 | 显示全部楼层
本帖最后由 落叶 于 2016-12-13 21:41 编辑

下面的图是最后返回运算结果的图
jg 中存入返回值
无标题4.png
1.23456789+9.87654e3   =9.87777456789E3
毋因群疑而阻独见  毋任己意而废人言
毋私小惠而伤大体  毋借公论以快私情
您需要登录后才可以回帖 登录 | 欢迎注册

本版积分规则

小黑屋|手机版|数学研发网 ( 苏ICP备07505100号 )

GMT+8, 2024-4-19 07:48 , Processed in 0.055936 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表