• <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 莫耶 閱讀(606) 評論(0)  編輯 收藏 引用

            公告

            導航

            <2008年12月>
            30123456
            78910111213
            14151617181920
            21222324252627
            28293031123
            45678910

            統計

            常用鏈接

            留言簿(3)

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久久久久久久久影院 | 色综合久久88色综合天天 | 婷婷久久香蕉五月综合加勒比| 亚洲色大成网站www久久九| 久久综合噜噜激激的五月天| 91视频国产91久久久| 久久精品国产72国产精福利| 亚洲va国产va天堂va久久| 漂亮人妻被中出中文字幕久久| 精品水蜜桃久久久久久久| 久久99热这里只有精品国产| 国内精品久久国产大陆| 伊人久久大香线蕉综合热线| 国产亚洲婷婷香蕉久久精品| 精品久久久久久成人AV| 国产69精品久久久久APP下载| 久久精品综合一区二区三区| 久久久久久久久久免免费精品| 亚洲精品tv久久久久久久久久| 国产成人精品久久一区二区三区| 久久99中文字幕久久| 草草久久久无码国产专区| 久久精品国产亚洲AV麻豆网站| 一级A毛片免费观看久久精品| 人妻无码精品久久亚瑟影视 | 精品国产一区二区三区久久久狼| 激情伊人五月天久久综合| 青青草原综合久久| 伊人久久综合无码成人网| 国产精品视频久久| 日韩久久久久中文字幕人妻| 久久久噜噜噜久久中文字幕色伊伊| 久久久国产精品亚洲一区 | 久久精品一区二区国产| 久久最新免费视频| 久久国产福利免费| 狠狠色狠狠色综合久久| 伊人久久综在合线亚洲2019| 久久精品视频免费| 伊人色综合九久久天天蜜桃| 99久久精品午夜一区二区|