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

            DraculaW

              C++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              19 隨筆 :: 0 文章 :: 7 評論 :: 0 Trackbacks
            #ifndef _BIGNUM_HPP_
            #define _BIGNUM_HPP_

            #include <vector>
            #include <string>
            #include <iostream>

            using namespace std;

            class BigNum;

            BigNum operator+(const BigNum& lhs, const BigNum& rhs);

            ostream& operator<<(ostream& os, const BigNum& rhs);

            void Add(const BigNum& lhs, const BigNum& rhs, BigNum& res);

            class BigNum
            {
            public:
                BigNum(int n) : value(n){}
                BigNum(const string& s);
                friend BigNum operator+(const BigNum& lhs, const BigNum& rhs);
                friend ostream& operator<<(ostream& os, const BigNum& rhs);
                friend void Add(const BigNum& lhs, const BigNum& rhs, BigNum& res);
            private:
                vector<char> value;   
            };

            #endif

            #include "BigNum.hpp"

            BigNum::BigNum(const string& s):value(s.length())
            {
                int j = value.size();
                for(string::const_iterator it = s.begin(); it != s.end(); it++)
                {
                    j--;       
                    value[j] = *it;
                }
            }

            ostream& operator<<(ostream& os, const BigNum& rhs)
            {
                size_t i = rhs.value.size();
                bool zero = false;
                if( i == 1)
                    return os << rhs.value[0];

                if(rhs.value[ i - 1 ] == '0')
                    zero = true;
               
                while( i > 0 )
                {
                    i--;
                    while(zero == true)
                    {
                        i--;
                        if(rhs.value[i] != '0')
                            zero = false;
                    }
                    os << rhs.value[i];
                }
                return os;
            }

            void Add(const BigNum& lhs, const BigNum& rhs, BigNum& res)
            {
                int carry = 0;
                char c = 0;
                char tmp = 0;
                size_t i = 0;
                for( ; i < rhs.value.size(); i++)
                {
                    tmp = lhs.value[i] + rhs.value[i] + carry - '0';

                    if( tmp > '9' )
                    {
                        carry = 1;
                        tmp -= 10;
                    }
                    else
                    {
                        carry = 0;
                    }
                    res.value[i] = tmp;
                }

                while( carry != 0 && i < lhs.value.size() )
                {
                    tmp = lhs.value[i] + carry;
                    if( tmp > '9' )
                    {
                        carry = 1;
                        tmp = '0';
                    }
                    else
                    {
                        carry = 0;
                    }
                    res.value[i] = tmp;
                    i++;
                }

                if( carry > 0)
                    res.value[i] = '1';

            }

            BigNum operator+(const BigNum& lhs, const BigNum& rhs)
            {
                size_t lsize, rsize;
                lsize = lhs.value.size();
                       rsize = rhs.value.size();
                size_t n = lsize > rsize ? lsize : rsize;   
                BigNum res(n + 1);
                res.value[0] = '0';
               
                if( lsize > rsize )
                {
                         Add(lhs, rhs, res);
                }
                else
                {
                    Add(rhs, lhs, res);
                }
               
                return res;
            }

            //我自己實現(xiàn)的大數(shù)的加法的程序。。 終于驗證可以使用了 呵呵
            這個程序?qū)懞昧?也算可以了了我的心愿了的
            去年的某個時候 在杭州的UT斯達(dá)康面試 上機(jī)題就是這一道 我死活沒有憋出來,當(dāng)時就很后悔 為什么不好好的看看論壇上的帖子 對上面的問題做做呢?那樣子的話 也許我就不會這么悲慘了,老是在哪里自怨自唉。
            總結(jié)起來 我其實是眼高手低,然后從來都被寵著沒有認(rèn)清過自己。小學(xué)初中,老媽老師都說我數(shù)學(xué)學(xué)的還不錯,其實是有點小聰明,分?jǐn)?shù)還是那么可憐的一點點;到高中,因 為學(xué)校比較一般,考的名次看起來很美,被假象迷惑了;大學(xué),因為自己有點基礎(chǔ),被給哥封為軟件最好,也有點沾沾自喜;到了這個公司,也許因為我身體的原 因,也有點面試時做題速度超快的原因,被經(jīng)理說我程序不錯,還是沒有擺正自己的位子。
            受點挫折才好。嗯
            呵呵, 心態(tài) , 心態(tài)才是一切。
            不要眼高手低
            posted on 2007-11-15 20:35 DraculaW 閱讀(199) 評論(0)  編輯 收藏 引用

            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久久久婷婷| 国产高清国内精品福利99久久| 亚洲精品国产自在久久| 伊人久久大香线蕉综合网站| 欧美国产成人久久精品| 99久久综合狠狠综合久久止| 曰曰摸天天摸人人看久久久| 日本久久中文字幕| 久久久国产乱子伦精品作者| 91精品国产综合久久四虎久久无码一级 | 久久中文字幕一区二区| 99久久国产综合精品网成人影院 | 亚洲精品白浆高清久久久久久| 久久久久高潮毛片免费全部播放 | 久久99这里只有精品国产| 久久Av无码精品人妻系列| 国产激情久久久久影院老熟女| 日本人妻丰满熟妇久久久久久| 亚洲精品美女久久久久99小说| 久久精品成人免费网站| 久久AV无码精品人妻糸列| 99久久精品国产一区二区| 亚洲精品无码久久一线| 合区精品久久久中文字幕一区| 久久国产福利免费| 亚洲国产成人久久精品影视| 91精品国产9l久久久久| 狼狼综合久久久久综合网| 精品国产乱码久久久久软件| 久久综合九色欧美综合狠狠| 久久se精品一区精品二区国产| 伊人色综合久久天天| 777久久精品一区二区三区无码 | av色综合久久天堂av色综合在| 亚洲第一永久AV网站久久精品男人的天堂AV | 91精品国产91久久久久福利| 无码人妻久久一区二区三区免费 | 亚洲精品高清国产一线久久| 丁香色欲久久久久久综合网| 一本色道久久HEZYO无码| 亚洲AV无码久久精品狠狠爱浪潮 |