• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            xLsDg

            Xiao Lu Software Development Group
            posts - 8, comments - 1, trackbacks - 0, articles - 0
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            大數運算

            Posted on 2014-07-26 08:21 xLsDg 閱讀(405) 評論(1)  編輯 收藏 引用 所屬分類: 代碼庫
              1 #include <stdio.h>
              2 #include <stdlib.h>
              3 #include <string.h>
              4 
              5 #define MAX_BIT ( 255u )
              6 
              7 char* bignum_add( char *a, char *b, char *c )
              8 {
              9     char *pA = NULL, *pB = NULL, *pC = NULL, d = 0;
             10 
             11     if ( strlen( a ) > strlen( b ) ) {
             12         pA = a;
             13         pB = b;
             14 
             15     } else {
             16         pA = b;
             17         pB = a;
             18     }
             19 
             20     for ( pC = c; *pA; pA++, pB++, pC++ ) {
             21         if ( *pB ) {
             22             *pC = ( *pA - '0' ) + ( *pB - '0' ) + d;
             23 
             24         } else {
             25             *pC = ( *pA - '0' ) + d;
             26         }
             27 
             28         if ( 9 < *pC ) {
             29             d = *pC / 10;
             30             *pC %= 10;
             31 
             32         } else {
             33             d = 0;
             34         }
             35 
             36         *pC += '0';
             37     }
             38 
             39     if ( 0 < d ) {
             40         *pC = d + '0';
             41     }
             42 
             43     return c;
             44 }
             45 
             46 char* bignum_sub( char *a, char *b, char *c )
             47 {
             48     char *pA = NULL, *pB = NULL, *pC = NULL, d = 0, flag = 0;
             49 
             50     if ( strlen( a ) >= strlen( b ) ) {
             51         pA = a;
             52         pB = b;
             53         flag = 1;
             54 
             55     } else {
             56         pA = b;
             57         pB = a;
             58         flag = -1;
             59     }
             60 
             61     for ( pC = c; *pA; pA++, pB++, pC++ ) {
             62         *pC = ( *pA - '0' ) + d;
             63 
             64         if ( *pB ) {
             65             *pC = *pC - ( *pB - '0' );
             66         }
             67 
             68         if ( 0 > *pC ) {
             69             if ( *( pA + 1 ) ) {
             70                 d = -1;
             71                 *pC += 10;
             72 
             73             } else {
             74                 *pC = -*pC;
             75                 flag = -1;
             76             }
             77 
             78         } else {
             79             d = 0;
             80         }
             81 
             82         *pC += '0';
             83     }
             84 
             85     if ( 0 > flag ) {
             86         *pC = '-';
             87     }
             88 
             89     return c;
             90 }
             91 
             92 char* bignum_mul( char *a, char *b, char *c )
             93 {
             94     char *pA = a, *pB = b, *pC = c, d = 0;
             95 
             96     for ( pB = b; *pB; pB++ ) {
             97         for ( pA = a, pC = c + ( pB - b ), d = 0; *pA; pA++, pC++ ) {
             98             if ( *pC ) {
             99                 *pC = ( *pC - '0' ) + ( *pA - '0' ) * ( *pB - '0' ) + d;
            100 
            101             } else {
            102                 *pC = ( *pA - '0' ) * ( *pB - '0' ) + d;
            103             }
            104 
            105             if ( 9 < *pC ) {
            106                 d = *pC / 10;
            107                 *pC %= 10;
            108 
            109             } else {
            110                 d = 0;
            111             }
            112 
            113             *pC += '0';
            114         }
            115 
            116         if ( 0 < d ) {
            117             *pC = d + '0';
            118         }
            119     }
            120 
            121     return c;
            122 }
            123 
            124 char* bignum_reverse( char *a )
            125 {
            126     char *p1 = a, *p2 = NULL;
            127 
            128     for ( p2 = a + strlen( a ) - 1; p1 < p2; p1++, p2-- ) {
            129         *p1 ^= *p2;
            130         *p2 ^= *p1;
            131         *p1 ^= *p2;
            132     }
            133 
            134     return a;
            135 }
            136 
            137 int main()
            138 {
            139     char *pA = NULL, *pB = NULL, *pC = NULL;
            140 
            141     pA = ( char* )malloc( MAX_BIT * sizeofchar ) );
            142     pB = ( char* )malloc( MAX_BIT * sizeofchar ) );
            143     pC = ( char* )malloc( MAX_BIT * sizeofchar ) * 2 );
            144 
            145     memset( pA, 0, MAX_BIT * sizeofchar ) );
            146     printf( "A=" ); scanf( "%s", pA );
            147     pA = bignum_reverse( pA );
            148 
            149     memset( pB, 0, MAX_BIT * sizeofchar ) );
            150     printf( "B=" ); scanf( "%s", pB );
            151     pB = bignum_reverse( pB );
            152 
            153     memset( pC, 0, MAX_BIT * sizeofchar ) * 2 );
            154     printf( "--------------------------------\n" );
            155     pC = bignum_add( pA, pB, pC );
            156 
            157     pC = bignum_reverse( pC );
            158     printf( "A+B=%s\n", pC );
            159 
            160     memset( pC, 0, MAX_BIT * sizeofchar ) * 2 );
            161     printf( "--------------------------------\n" );
            162     pC = bignum_sub( pA, pB, pC );
            163 
            164     pC = bignum_reverse( pC );
            165     printf( "A-B=%s\n", pC );
            166 
            167     memset( pC, 0, MAX_BIT * sizeofchar ) * 2 );
            168     printf( "--------------------------------\n" );
            169     pC = bignum_mul( pA, pB, pC );
            170 
            171     pC = bignum_reverse( pC );
            172     printf( "A*B=%s\n", pC );
            173 
            174     return 0;
            175 }
            176 

            Feedback

            # re: 大數運算  回復  更多評論   

            2014-07-26 15:45 by 云騰
            用long long類型的,基本上都能支持了。www.cppentry.com
            欧美久久综合九色综合| 99精品久久久久中文字幕| 成人妇女免费播放久久久| 97久久精品人人澡人人爽| 中文成人久久久久影院免费观看| 色欲综合久久中文字幕网| 久久久久亚洲AV成人网人人软件| 久久国产精品-国产精品| 国产亚洲精品久久久久秋霞| 欧美久久综合九色综合| 久久久精品国产免大香伊 | 人妻少妇久久中文字幕| 久久国产精品免费一区| 久久青青草原综合伊人| 国内精品九九久久久精品| 精品乱码久久久久久夜夜嗨| 97久久国产综合精品女不卡| 精品水蜜桃久久久久久久| 日本人妻丰满熟妇久久久久久| 精品久久久久久久久久中文字幕| 久久久女人与动物群交毛片| 熟妇人妻久久中文字幕| 久久99精品久久久久久野外| 久久精品麻豆日日躁夜夜躁| 亚洲美日韩Av中文字幕无码久久久妻妇 | 亚洲国产精品成人AV无码久久综合影院| 2021国产精品午夜久久| 国内精品久久久久影院亚洲| 97久久精品人人做人人爽| 伊人久久大香线蕉av不卡| 亚洲欧美成人久久综合中文网| 久久se精品一区二区| 国产午夜精品理论片久久影视| 亚洲av日韩精品久久久久久a| 青春久久| 人妻无码精品久久亚瑟影视| 日韩中文久久| 精品久久亚洲中文无码| 久久精品国产色蜜蜜麻豆| 久久免费看黄a级毛片| 久久91精品国产91久|