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

            長整數(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;
            };

            /**長整數(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,從頭結點處入鏈表
                    if(*rIterA+*rIterB+carry>=10) carry=1 計算是否有進位
                    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;//進位標志
                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最高位百位還要進位一次
                    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中每個char對應的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 閱讀(1818) 評論(4)  編輯 收藏 引用 所屬分類: 鏈表

            評論

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

            在底層的系統(tǒng)運算還真是常用到這個。  回復  更多評論   

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

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

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

            不錯,不小心路過。想問下,你是合工大的?  回復  更多評論   

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

            不是的,我是安徽人。好多同學在合工大。  回復  更多評論   

            <2025年6月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統(tǒng)計

            常用鏈接

            留言簿(1)

            隨筆分類

            隨筆檔案

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            麻豆精品久久精品色综合| 无夜精品久久久久久| 亚洲AV无码久久精品蜜桃| 久久亚洲精品无码aⅴ大香| 人妻无码精品久久亚瑟影视| 少妇高潮惨叫久久久久久| 色综合合久久天天综合绕视看| 久久www免费人成看国产片| 亚洲精品美女久久久久99| 国内精品久久久久久久影视麻豆| 免费精品久久天干天干| 久久精品国产精品青草| 精品国产乱码久久久久久呢| 2022年国产精品久久久久| 久久婷婷午色综合夜啪| 青青草国产精品久久| 久久久久亚洲AV无码专区首JN | 国产免费久久精品99久久| 久久久女人与动物群交毛片| 国产 亚洲 欧美 另类 久久| 久久久久久精品免费免费自慰| 国产成人精品久久亚洲高清不卡| 无码伊人66久久大杳蕉网站谷歌 | 欧美精品国产综合久久| 香港aa三级久久三级| 亚洲午夜久久久影院| 国产精品99久久久久久宅男小说| 精品久久人妻av中文字幕| 亚洲中文字幕无码久久综合网| 久久一区二区三区免费| 久久九色综合九色99伊人| 久久天堂电影网| 久久精品国产一区| 99精品久久久久中文字幕| 久久精品毛片免费观看| 久久99国内精品自在现线| 久久精品国产亚洲AV高清热| 久久久免费精品re6| 好属妞这里只有精品久久| 国内精品久久久久久野外| 国产高潮国产高潮久久久91 |