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

            身上無錢你莫邪

            moye's c++ blog

            習題12.1 多態及文本讀入

            Account類:
            #ifndef HEADER_ACCOUNT
            #define HEADER_ACCOUNT

            #include 
            <string>
            using std::string;

            class Account
            {
            protected:
                
            string acntNumber;
                
            double balance;
            public:
                Account()
            {}
                Account(
            string acntNo,double balan=0.0);
                
            virtual void display()const;
                
            double getBalan()const{return balance;}
                
            void deposit(double amount){balance += amount; display();}
                
            bool operator==(const Account&a){return acntNumber==a.acntNumber;}
                
            virtual void withdrawal(double amount){return;}
            }
            ;

            #endif
            #include "StdAfx.h"
            #include 
            "Account.h"
            #include 
            <iostream>

            using namespace std;

            Account::Account(
            string acntNo, double balan):acntNumber(acntNo),balance(balan){}

            void Account::display() const{
                cout
            <<"Account:"<<acntNumber<<" = "<<balance<<endl;
            }

            Checking類:
            #ifndef HEADER_CHECKING
            #define HEADER_CHECKING

            #include 
            <string>
            #include 
            "account.h"
            using std::string;

            //匯款方式:信匯,電匯,其他
            enum REMIT{remitByPost,remitByCable,Other};

            class Checking : public Account{
                REMIT remittance;
            public:
                Checking()
            {}
                Checking(
            string acntNo,double balan=0.0);
                
            void display()const;
                
            void withdrawal(double amount);
                
            void setRemit(REMIT re){remittance = re;}
            }
            ;

            #endif
            #include "StdAfx.h"
            #include 
            "Checking.h"
            #include 
            <iostream>
            using namespace std;

            Checking::Checking(
            string acntNo, double balan)
                :remittance(REMIT::Other)

                    Account::acntNumber 
            = acntNo;
                    Account::balance 
            = balan;
                }


            void Checking::display() const{
                cout
            <<"Checking ";
                Account::display();
            }


            void Checking::withdrawal(double amount){
                
            if(remittance == REMIT::remitByPost)
                    amount 
            +=30;
                
            if(remittance == REMIT::remitByCable)
                    amount 
            +=60;
                
                
            if(balance<amount)
                    cout
            <<"Insufficient funds withdrawal: "<<amount<<endl;
                
            else{
                    balance 
            -= amount;
                    Account::display();
                }

            }

            Savings類:
            #ifndef HEADER_SAVINGS
            #define HEADER_SAVINGS

            #include 
            "account.h"
            #include 
            <string>
            using std::string;

            class Savings:public Account{
                
            static double minBalance;
            public:
                Savings()
            {}
                Savings(
            string acntNo,double balan=0.0);
                
            void display()const;
                
            void withdrawal(double amount);
            }
            ;

            #endif
            #include "StdAfx.h"
            #include 
            "Savings.h"
            #include 
            <iostream>
            using namespace std;

            double Savings::minBalance =0;

            Savings::Savings(
            string acntNo,double balan){
                Account::acntNumber 
            = acntNo;
                Account::balance 
            =balan;
            }


            void Savings::display() const{
                cout
            <<"Savings ";
                Account::display();
            }

            void Savings::withdrawal(double amount){
                
            if(balance+minBalance < amount)
                    cout
            <<"Insufficient funds withdrawal:"<<amount<<endl;
                
            else{
                    balance 
            -=amount;
                    Account::display();
                }

            }

            AccountList類:
            #ifndef HEADER_ACCOUNTLIST
            #define HEADER_ACCOUNTLIST

            #include 
            "account.h"

            // ---------------------//
            class Node{
            public:
                Account
            & acnt;
                Node 
            *next,*prev;
                Node(Account 
            &a):acnt(a),next(0),prev(0){}
                
            bool operator==(const Node& n)return acnt == n.acnt;}//最終比較acntNumber
                ~Node(){}
            }
            ;
            // ---------------------//
            class AccountList
            {
                
            int size;
                Node 
            *first;
            public:
                AccountList():first(
            0),size(0){};
                Node
            * getFirst()constreturn first;}
                
            int getSize()constreturn size;}
                
            void add(Account &a);
                
            void remove(string acntNo);
                Account
            * find(string acntNo)const;
                
            bool isEmpty()const{return !size;}
                
            void display()const;
                
            ~AccountList();
            }
            ;

            #endif
            #include "StdAfx.h"
            #include 
            "AccountList.h"

            #include 
            <iostream>
            using namespace std;

            void AccountList::add(Account &a){
                
            //自動把新元素放在棧首
                Node* pN=new Node(a);
                
            if(first){
                    pN
            ->next = first;
                    first
            ->prev = pN;
                }

                first 
            = pN;
                size 
            ++;
            }
            //--------------------
            void AccountList::remove(string acntNo){
                Account a(acntNo);
                
            for(Node *= first;p;p=p->next)
                    
            if(*p==Node(a))//compare調用Account的==方法
                        if(p->prev) p->prev->next = p->next;
                        
            if(p->next) p->next->prev = p->prev;
                        
            if(p==first) first = p->next;
                        delete p;
                        size
            --;
                        
            break;
                    }

            }
            //--------------------
            Account* AccountList::find(std::string acntNo) const{
                Account a(acntNo);
                
            for(Node* p=first;p;p = p->next)
                    
            if(*p==Node(a))
                        
            return &(p->acnt);
                
            return 0;
            }
            //--------------------
            void AccountList::display()const{
                cout
            <<"There are "<<size<<" accounts."<<endl;
                
            for(Node* p=first;p;p=p->next)
                    (p
            ->acnt).display();
            }
            //--------------------
            AccountList::~AccountList(){
                
            for(Node* p=first;p=first;delete p){         
                    first 
            = first->next;         
                }
               
            }


            題目:
            使用Account類、Savings類、Checking類及AccountList類,編寫一個應用程序,它從文件account.txt中讀入一些帳戶號和對應的存款額,創建若干個Savings和Checking帳戶,直到遇到一個結束標志“x 0”,并輸出所有帳戶號的存款數據。
            account.txt內容如下:

            savings 123 70000
            checking 661 20000
            savings 128 2000
            savings 131 5000
            checking 681 200000
            checking 688 10000
            x 0

            程序如下:
            #include "stdafx.h"

            #include 
            "savings.h"
            #include 
            "checking.h"
            #include 
            "accountlist.h"

            #include
            <iostream>

            using namespace std;

            int _tmain(int argc, _TCHAR* argv[])
            {  
                
            //習題12.1
                ifstream in("account.txt");
                
            string accType,accNo;
                
            double balance;
                AccountList list;

                
            for(string s;getline(in,s);)
                {
                    istringstream sin(s);
                    sin
            >>accType;
                    
            if(accType!="x"){
                        sin
            >>accNo;
                        sin
            >>balance;
                        
            if(accType== "savings"){
                            Savings 
            *s1 = new Savings(accNo,balance);
                            list.add(
            *s1);
                        }
                        
            else if (accType=="checking"){
                            Checking
            *c1 = new Checking(accNo,balance);
                            list.add(
            *c1);
                        }    
                    }        
                }

                
            if(list.getSize()>0)
                    list.display();
                    system(
            "pause");
                
            return 0;
            }

            posted on 2009-02-03 12:12 莫耶 閱讀(614) 評論(0)  編輯 收藏 引用

            公告

            導航

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

            統計

            常用鏈接

            留言簿(3)

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            色综合久久中文综合网| 2020最新久久久视精品爱| 四虎国产精品成人免费久久| 久久露脸国产精品| 欧美亚洲国产精品久久高清| 久久国产精品成人影院| 国产精品久久久久久影院| 久久久WWW免费人成精品| 色偷偷偷久久伊人大杳蕉| 久久精品视频免费| 伊人 久久 精品| WWW婷婷AV久久久影片| 人妻中文久久久久| 精品久久久久久亚洲| 久久91精品国产91久| 99久久夜色精品国产网站| 久久精品国产99国产精品导航 | 久久中文字幕人妻熟av女| 久久久国产精品网站| 色综合久久无码五十路人妻| 91久久精品国产91性色也| 欧洲人妻丰满av无码久久不卡| 久久夜色精品国产亚洲av| 久久亚洲国产精品一区二区| 日本久久久久亚洲中字幕| 午夜精品久久久久久影视riav| 日本久久久精品中文字幕| 精品久久久久久无码中文字幕一区| 久久国产欧美日韩精品免费| 久久国产三级无码一区二区| 99久久免费国产精品热| 久久影院综合精品| 日韩精品久久久肉伦网站| 伊人久久精品无码av一区| 欧美日韩精品久久久久| 久久夜色撩人精品国产| 久久精品亚洲欧美日韩久久| 久久强奷乱码老熟女| 久久无码国产| 怡红院日本一道日本久久 | 久久夜色精品国产|