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

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品一区二区影院| 精品久久久久久无码人妻蜜桃| 国产69精品久久久久99| 奇米影视7777久久精品| 亚洲国产成人精品女人久久久 | 日韩久久久久久中文人妻| 亚洲精品无码久久久| 国内精品久久久久影院网站| 久久国产精品成人免费| 久久亚洲精品视频| 久久九九青青国产精品| 久久亚洲国产精品一区二区| 亚洲狠狠久久综合一区77777| 国产欧美一区二区久久| 久久久久久a亚洲欧洲aⅴ| 青青草国产精品久久| 999久久久国产精品| 久久精品国产只有精品66| 久久久久亚洲精品无码网址| 久久综合九色综合久99| 亚洲国产精品综合久久一线| 精品久久久久久久国产潘金莲| 精品久久久久久无码不卡| 亚洲午夜久久久影院伊人| 无码人妻久久久一区二区三区| 亚洲国产一成人久久精品| 久久精品无码专区免费东京热| 99久久精品毛片免费播放| 亚洲国产精品久久久久久| 久久精品国产99国产精品| 国产69精品久久久久APP下载| 奇米综合四色77777久久| 2020久久精品国产免费| 精品久久久久中文字幕一区| 亚洲欧洲精品成人久久奇米网| 久久天天躁狠狠躁夜夜躁2O2O | 夜夜亚洲天天久久| 久久性精品| 久久国产精品无码一区二区三区| 亚洲国产精品热久久| 久久狠狠爱亚洲综合影院|