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

            隨筆檔案

            友情鏈接

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            日本久久中文字幕| 韩国无遮挡三级久久| 无码精品久久一区二区三区 | 国产69精品久久久久观看软件| 国内精品久久久久久久涩爱 | 国产成人无码久久久精品一| 精品免费tv久久久久久久| 精品无码久久久久久久久久| 久久精品人人做人人爽电影 | 亚洲精品无码成人片久久| 日韩一区二区久久久久久 | 久久婷婷成人综合色综合| 99久久精品费精品国产 | 久久婷婷五月综合色奶水99啪| 亚洲第一极品精品无码久久| 94久久国产乱子伦精品免费| 婷婷综合久久中文字幕蜜桃三电影| 国产欧美久久久精品| 99久久精品国产一区二区| 久久久久99精品成人片| 人妻精品久久久久中文字幕一冢本| 国产亚州精品女人久久久久久 | 国产产无码乱码精品久久鸭| 午夜精品久久影院蜜桃| 亚洲国产精久久久久久久| 狠狠色婷婷综合天天久久丁香 | 亚洲欧美另类日本久久国产真实乱对白| 人妻少妇久久中文字幕| 精品无码久久久久国产动漫3d| 精品国产婷婷久久久| 国产999精品久久久久久| 久久这里只精品国产99热| 国产午夜精品久久久久免费视| 久久AV高潮AV无码AV| 综合久久国产九一剧情麻豆| 久久精品国产亚洲av麻豆蜜芽| 国产精品99久久久精品无码| 色妞色综合久久夜夜| 精品伊人久久大线蕉色首页| 乱亲女H秽乱长久久久| 亚洲精品无码久久久久|