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

            長(zhǎng)整數(shù)相加

            // test18.cpp : Defines the entry point for the console application.
            //

            #include 
            "stdafx.h"
            #include
            <iostream>
            #include
            <string>
            #include
            <list>
            using namespace std;

            class BigInt{
                friend istream
            & operator>>(istream&, BigInt&);
                friend ostream
            & operator<<(ostream&, BigInt&);
                friend BigInt
            & operator+(BigInt&, BigInt&);
            private:
                list
            <int> intLst;
            };

            /**長(zhǎng)整數(shù)加法
            基本思路是利用反向迭代器(加法是從地位到高位)實(shí)現(xiàn)鏈表并歸
            BigInt& operator+(a,b)
                while(rIterA!=a.intLst.rend && rIterB!=a.intLst.rend)
                    c->intLst.push_front((*rIterA+*rIterB+carry)%10); 注意是push_front,從頭結(jié)點(diǎn)處入鏈表
                    if(*rIterA+*rIterB+carry>=10) carry=1 計(jì)算是否有進(jìn)位
                    else carry=0
                    rIterA++, rIterB++
                while(rIterA!=a.intLst.rend)
                    c->intLst.push(*rIterA++ + carry)
                    carry=0;
                while(rIterB!=b.intLst.rend)
                    c->intLst.push(*rIterB++ +carry)
                    carry=0
                return *c
            */
            BigInt
            & operator+(BigInt& a, BigInt& b){
                
            int carry=0;//進(jìn)位標(biāo)志
                list<int>::reverse_iterator rIterA=a.intLst.rbegin();
                list
            <int>::reverse_iterator rIterB=b.intLst.rbegin();

                BigInt
            * c=new BigInt();
                
            while(rIterA!=a.intLst.rend() && rIterB!=b.intLst.rend()){
                    c
            ->intLst.push_front((*rIterA+*rIterB+carry)%10);
                    
            if((*rIterA+*rIterB+carry)>=10)
                        carry
            =1;
                    
            else
                        carry
            =0;
                    rIterA
            ++, rIterB++;
                }
                
            while(rIterA!=a.intLst.rend()){
                    c
            ->intLst.push_front(*rIterA++ + carry);//如99+123最高位百位還要進(jìn)位一次
                    carry=0;
                }
                
            while(rIterB!=b.intLst.rend()){
                    c
            ->intLst.push_front(*rIterB++ +carry);
                    carry
            =0;
                }
                
            return *c;
            }

            istream
            & operator>>(istream& in, BigInt& bint){
                
            string str;
                
            in>>str;
                
            for(string::iterator iter=str.begin(); iter!=str.end(); iter++){
                    bint.intLst.push_back(
            *iter-'0'); //*iter是stirng中每個(gè)char對(duì)應(yīng)的ASCII碼
                }
                
            return in;
            }

            ostream
            & operator<<(ostream& out, BigInt& bint){
                
            for(list<int>::iterator iter=bint.intLst.begin();iter!=bint.intLst.end();iter++)
                    
            out<<*iter;
                
            out<<endl;
                
            return out;
            }

            int main(){
                BigInt
            * a=new BigInt();
                BigInt
            * b=new BigInt();
                cout
            <<"cin>>A: ";
                cin
            >>*a;
                cout
            <<"cin>>B: ";
                cin
            >>*b;
                BigInt c
            =*a+*b;
                cout
            <<c;
                system(
            "pause");
            }

            輸入輸出如下;
            cin>>A: 3434685897609608780
            cin>>B: 1234234236535634764376
            1237668922433244373156

            posted on 2008-10-25 12:48 deep2 閱讀(1814) 評(píng)論(4)  編輯 收藏 引用 所屬分類: 鏈表

            評(píng)論

            # re: 長(zhǎng)整數(shù)相加 2008-10-25 16:44 金山毒霸2008

            在底層的系統(tǒng)運(yùn)算還真是常用到這個(gè)。  回復(fù)  更多評(píng)論   

            # re: 長(zhǎng)整數(shù)相加 2008-10-26 16:06 春天

            while(rIterA!=a.intLst.rend()){
            c->intLst.push_front(*rIterA++ + carry);//如99+123最高位百位還要進(jìn)位一次
            carry=0;
            }
            while(rIterB!=b.intLst.rend()){
            c->intLst.push_front(*rIterB++ +carry);
            carry=0;
            }
            你考慮了最高位還會(huì)進(jìn)一位,沒(méi)有考慮再進(jìn)n位;如果999999+5,試下。  回復(fù)  更多評(píng)論   

            # re: 長(zhǎng)整數(shù)相加 2008-10-31 11:09 合工大

            不錯(cuò),不小心路過(guò)。想問(wèn)下,你是合工大的?  回復(fù)  更多評(píng)論   

            # re: 長(zhǎng)整數(shù)相加 2008-11-01 12:03 春天

            不是的,我是安徽人。好多同學(xué)在合工大。  回復(fù)  更多評(píng)論   

            <2008年10月>
            2829301234
            567891011
            12131415161718
            19202122232425
            2627282930311
            2345678

            導(dǎo)航

            統(tǒng)計(jì)

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            美女写真久久影院| 日韩久久无码免费毛片软件| 无码任你躁久久久久久久| 久久综合给合综合久久| 久久久久香蕉视频| 久久av高潮av无码av喷吹| 久久中文字幕无码专区| 国产精品一区二区久久精品涩爱| 国产精品无码久久四虎| 熟妇人妻久久中文字幕| 乱亲女H秽乱长久久久| 国产成人无码精品久久久久免费| 久久久噜噜噜久久中文字幕色伊伊| 亚洲伊人久久综合中文成人网| 伊人色综合久久天天人手人婷| 亚洲国产精品无码久久一区二区| 精品久久久久久久| 99热精品久久只有精品| 伊人 久久 精品| 嫩草伊人久久精品少妇AV| 久久99精品免费一区二区| 久久无码专区国产精品发布| 久久久久久国产精品无码超碰| 久久福利资源国产精品999| 国产精品久久99| 国产亚洲精午夜久久久久久| 久久人与动人物a级毛片| 国产成人AV综合久久| 婷婷伊人久久大香线蕉AV| 日韩久久无码免费毛片软件| aaa级精品久久久国产片| 久久中文字幕人妻熟av女| 大香网伊人久久综合网2020| 亚洲色大成网站www久久九| 久久亚洲AV无码西西人体| 久久久91精品国产一区二区三区 | 日本WV一本一道久久香蕉| 久久久久综合网久久| 国产精品一区二区久久不卡| 亚洲精品无码专区久久久| 亚洲国产精品无码久久九九|