身上无钱你莫邪

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 莫耶 阅读(525) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

常用链接

留言簿(3)

随笔档案

友情链接

搜索

最新评论

阅读排行榜

评论排行榜