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

            習(xí)題12.1 多態(tài)及文本讀入

            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調(diào)用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類,編寫一個應(yīng)用程序,它從文件account.txt中讀入一些帳戶號和對應(yīng)的存款額,創(chuàng)建若干個Savings和Checking帳戶,直到遇到一個結(jié)束標(biāo)志“x 0”,并輸出所有帳戶號的存款數(shù)據(jù)。
            account.txt內(nèi)容如下:

            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[])
            {  
                
            //習(xí)題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 莫耶 閱讀(607) 評論(0)  編輯 收藏 引用


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


            公告

            導(dǎo)航

            <2009年2月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            1234567

            統(tǒng)計

            常用鏈接

            留言簿(3)

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            国产精品久久久久久吹潮| 久久久久18| 久久婷婷五月综合国产尤物app| 囯产精品久久久久久久久蜜桃| 九九精品久久久久久噜噜| 久久综合亚洲色一区二区三区| 久久99精品久久久久久hb无码| 香蕉久久夜色精品国产小说| 亚洲综合久久夜AV | 国产综合久久久久| 午夜精品久久久久久久无码| 91精品国产综合久久久久久| 亚洲午夜久久久| 中文字幕成人精品久久不卡| yy6080久久| 色综合久久中文字幕综合网| 97精品国产97久久久久久免费| 99久久夜色精品国产网站| 久久丫精品国产亚洲av不卡 | 亚洲人成电影网站久久| 色偷偷久久一区二区三区| 午夜精品久久久久久| 99久久99久久精品国产片| 久久99国产综合精品女同| 香蕉久久久久久狠狠色| 久久精品二区| 久久成人精品| 91精品免费久久久久久久久| 国产亚洲欧美精品久久久| 一本色道久久综合亚洲精品| 欧美伊人久久大香线蕉综合69| 精品国产91久久久久久久a| 久久国产精品99国产精| 精品久久人人爽天天玩人人妻| 精品国产热久久久福利| 久久播电影网| 伊人久久无码精品中文字幕| 日韩久久无码免费毛片软件| 久久精品国产精品亚洲下载| 久久久久国产一区二区| 思思久久99热免费精品6|