• <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 評(píng)論 :: 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;
            }

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

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


            日批日出水久久亚洲精品tv| 青青草原精品99久久精品66| 97久久国产综合精品女不卡| 久久久噜噜噜久久| 99久久亚洲综合精品网站| 久久综合狠狠综合久久| 久久这里有精品| 久久久久久久免费视频| 精品久久久久久久久免费影院| 久久国产成人午夜aⅴ影院| 亚洲国产精品久久久久| 91精品无码久久久久久五月天| 久久综合欧美成人| 国产精品丝袜久久久久久不卡| 伊人丁香狠狠色综合久久| 狠狠久久综合| 无码乱码观看精品久久| 一本综合久久国产二区| 国产欧美久久久精品影院| 一本一本久久a久久综合精品蜜桃| 91麻豆国产精品91久久久| 亚洲精品美女久久777777| 亚洲欧美伊人久久综合一区二区| 久久精品a亚洲国产v高清不卡| 久久久久久毛片免费播放| 老司机国内精品久久久久| 精品久久久久久无码免费| 亚洲欧美国产日韩综合久久| 久久久久久久免费视频| 久久精品国产久精国产思思| 久久久久久国产精品美女 | 久久只有这里有精品4| 狠狠综合久久综合88亚洲| 久久狠狠高潮亚洲精品| 国产精品无码久久四虎| 国产偷久久久精品专区 | 久久精品国产精品亚洲精品| 久久久久无码国产精品不卡| 日产精品99久久久久久| 久久成人永久免费播放| 久久亚洲私人国产精品|