• <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>

            elva

            大數(shù)求和

            Declaration

              該文為本人原創(chuàng),如需轉(zhuǎn)載,請(qǐng)注名原作者和本站地址,但未經(jīng)本人同意,嚴(yán)禁用于商業(yè)途徑。

            Text

              由于系統(tǒng)硬件的限制,我們無(wú)法直接進(jìn)行大數(shù)的求和,但是采取一些存儲(chǔ)方面的技術(shù)來(lái)解決這個(gè)問(wèn)題。下面通過(guò)引入字符數(shù)組,并運(yùn)用小學(xué)就學(xué)習(xí)過(guò)的進(jìn)位求和算法來(lái)進(jìn)行大數(shù)的求和運(yùn)算。這里只實(shí)現(xiàn)了大整數(shù)的求和,如果需要運(yùn)用到小數(shù)求和里頭,需要改動(dòng)才行。

            Code

            /*
             * FILE: lns.c
             * Funciton: Count the sum of two large numbers
             */


            /*
             * Author: Wu Zhangjin, <zhangjinw@gmail.com>
             * (c) 2006-11-25 dslab.lzu.edu.cn Lanzhou University
             * License GPL Version 2
             */


            #include <stdio.h>        /* standard input/output functions */
            #include <string.h>         /* strlen function */
            #define MAX_DIGITS 10000    /* define your max digit here */

            /**
             * sumof_ln - count the sum of two large numbers
             * @ln_arr: the address of the array which store the two string of larger numbers
             *
             * Description:
             * For the restrictions of the hardware, we can not count the sum of two
             * large numbers directly, but we can use some skills to work it out.
             * This function use a char array to store the large numbers, and then
             * add every char digit with the carry, until the leftmost digit. Notes that
             * if the digit of two numbers is not equal,The left aligned digit should be
             * treated espectically--you should add a carry if it exist on every left digit.
             * And also if the last carry exist,the leftmost digit should be '1'
             *
             * Returns:
             * the address point of the string of sum
             *
             * Notes:
             * you can define the maximum digit of the number by the macro MAX_DIGITS
             */


            char* sumof_ln(char ln_arr[2][MAX_DIGITS])
            {
                int digit_arr[2], carry, sindex, lindex, i, tmp, align_lindex;
                
                carry = 0;
                digit_arr[0] = strlen(ln_arr[0]);
                digit_arr[1] = strlen(ln_arr[1]);
                sindex = (digit_arr[0] < digit_arr[1])?0:1;
                lindex = 1 - sindex;
                align_lindex = digit_arr[lindex] - digit_arr[sindex];

                for (i = digit_arr[sindex] - 1; i >= 0; i --) {
                    tmp = ln_arr[lindex][align_lindex + i] + ln_arr[sindex][i] + carry;
                    ln_arr[lindex][align_lindex + i] = tmp - 48 - 10 * (carry = (tmp >= 106?1:0));
                }

                if (digit_arr[0] != digit_arr[1])
                for (i = align_lindex - 1; i >= 0 && carry; i --)    {
                    tmp = ln_arr[lindex][i] + carry;
                    ln_arr[lindex][i] = tmp - 10 * (carry = (tmp >= 58?1:0));

                }

                if (carry) {
                    for (i = digit_arr[lindex] - 1; i >= 0; i --)
                        ln_arr[lindex][i+1] = ln_arr[lindex][i];
                    ln_arr[lindex][digit_arr[lindex] + 1] = '\0';
                    ln_arr[lindex][0] = '1';
                }    

                return ln_arr[lindex];
            }

            /**
             * main - main function to call function sumof_ln
             * Description:
             * compile and link this c program and input two larger numbers, you can get the sum of them
             *
             * Notes:
             * you cannot input a number whose digit is larger than MAX_DIGITS, but you can change by modifying the macro.
             */


            int main(int argc, char *argv[])
            {
                char ln_arr[2][MAX_DIGITS];

                printf("Please input two large numbers: \n");
                scanf("%s %s", ln_arr[0], ln_arr[1]);

                printf("The sum is:\n%s\n", sumof_ln( ln_arr ));    
            }


            Demo

            shell> make lns
            cc     lns.c   -o lns
            shell> ./lns
            Please input two large numbers:
            9
            1
            The sum is:
            10
            shell> ./lns
            Please input two large numbers:
            999999999999999999999999999999999999999999999999999999999999999999999999999999999
            1
            The sum is:
            1000000000000000000000000000000000000000000000000000000000000000000000000000000000
            shell> ./lns
            Please input two large numbers:
            5644687481748485751785757181534145412178758718715875187687867168716841687186716876841688676
            45848587878456465146861787116876871687168418178718718768465167867817617687116761867187187971971971954521278
            The sum is:
            45848587878456470791549268865362623472925599712864130947223886583692805374983930584028875158688848796209954
            shell> ./lns
            Please input two large numbers:
            144
            2546
            The sum is:
            2690

            posted on 2007-08-15 17:54 葉子 閱讀(1255) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C\C++

            久久精品国产亚洲av麻豆小说| 怡红院日本一道日本久久 | 一级做a爱片久久毛片| 国产成人精品久久免费动漫| av午夜福利一片免费看久久 | 久久精品无码专区免费青青| 久久久免费精品re6| 久久久精品久久久久特色影视| 伊人色综合久久天天人守人婷 | 97久久香蕉国产线看观看| 亚洲国产成人久久精品动漫| 无码人妻少妇久久中文字幕| 国产精品久久久久天天影视| 2021国内精品久久久久久影院| 国产精品久久亚洲不卡动漫| 婷婷久久精品国产| 久久99免费视频| 久久九九精品99国产精品| 久久人人爽人人爽AV片| 国产精品久久波多野结衣| 一本一本久久aa综合精品| 久久精品国产精品亚洲下载| 久久ZYZ资源站无码中文动漫| 欧美伊人久久大香线蕉综合69| 99久久精品国内| 亚洲精品乱码久久久久久 | 久久九九兔免费精品6| 久久精品国产亚洲麻豆| 久久久久久久久久久久中文字幕 | 国产亚洲美女精品久久久| 精品少妇人妻av无码久久| 久久久SS麻豆欧美国产日韩| 亚洲欧美成人久久综合中文网| 久久精品国产99国产精品澳门| 久久A级毛片免费观看| 无码精品久久久天天影视| 亚洲精品无码久久久久久| 国色天香久久久久久久小说 | 伊人久久五月天| 一日本道伊人久久综合影| 亚洲国产精品成人AV无码久久综合影院|