個人銀行賬戶小程序
#ifndef _ACCOUNTH
#define _ACCOUNTH
class SavingsAccount //儲蓄賬戶
{
public:
SavingsAccount(int date,int id,double rate);
int getid()const
{return id;}
double getbalance()const
{return balance;}
double getrate()
{return rate;}
static double gettotal()
{return total;}
void deposit(int date,double amount); //存入現金
void settle(int date); //結算利息,每年1月1日調用一次該函數
void show()const; //顯示賬戶信息
private:
int id; //賬號
double balance; //余額
double rate; //存款的年利率
int lastdate; //上次變更余額的日期
double accumulation; //余額按日累加之和
static double total; //所有賬戶的總金額
//記錄一筆賬,date為日期,amount為金額
double accumulate(int date)const
//獲得到指定日期位置的存款金額按日累積值
{
return accumulation+balance*(date-lastdate);
}
};
#endif
#endif
#if 0 //2
#ifndef _ACCOUNTH
#define _ACCOUNTH
#include"date.h"
#include<string>
class SavingsAccount //儲蓄賬戶類
{
public:
SavingsAccount(const Date &date,const std::string &id,double rate);
const std::string &getid()const
{return id;}
double getbalance()const
{return balance;}
double getrate()const
{return rate;}
static double gettotal()
{return total;}
//存入現金
void deposit(const Date &date,double amount,const std::string &desc);
//取出現金
void withdraw(const Date &date,double amount,const std::string &desc);
//結算利息,每年的1月1日調用一次該函數
void settle(const Date &date);
void show()const;
private:
std::string id; //賬號
double balance; //余額
double rate; //存款的年利率
Date lastdate; //上次變更余額的日期
double accumulation; //余額按日累加之和
static double total; //所有賬戶的總金額
//記錄一筆賬,date為日期,amount為金額,desc為說明
void record(const Date &date,double amount,const std::string &desc);
//報告錯誤信息
void error(const std::string &msg)const;
//獲得到指定日期位置的存款金額按日累加值
double accumulate(const Date &date)const
{return accumulation+balance*date.distance(lastdate);}
};
#endif
#endif
#if 0 //3
#ifndef _ACCOUNTH
#define _ACCOUNTH
#include "date.h"
#include "accumulator.h"
#include<string>
class Account //賬戶類
{
public:
const std::string &getid()const
{return id;}
double getbalance()const
{return balance;}
static double gettotal()
{return total;}
void show()const; //顯示賬戶信息
protected:
//供派生類調用的構造函數,id為賬戶
Account(const Date &date,const std::string &id);
//記錄一筆賬,date為日期,amount為金額,desc為說明
void record(const Date &date,double amount,const std::string &desc);
//報告錯誤信息
void error(const std::string &msg)const;
private:
std::string id; //賬號
double balance; //余額
static double total; //所有賬戶的總金額
};
class SavingsAccount:public Account //儲蓄賬戶類
{
{
public:
//構造函數
SavingsAccount(const Date &date,const std::string &id,double rate);
double getrate()
{return rate;}
//存入現金
void deposit(const Date &date,double amount,const std::string &desc);
//取出現金
void withdraw(const Date &date,double amount,const std::string &desc);
//結算利息,每年1月1日調用一次該函數
void settle(const Date &date);
private:
Accumulator acc; //輔助計算利息的累加器
double rate; //存款的年利率
};
class CreditAccount:public Account //信用賬戶類
{
public:
//構造函數
CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee);
double getcredit()const
{return credit;}
double getrate()const
{return rate;}
double getfee()const
{return fee;}
double getavailablecredit()const //獲得可用信用額度
{
if(getbalance()<0)
return credit+getbalance();
else
return credit;
}
//存入現金
void deposit(const Date &date,double amount,const std::string &desc);
//取出現金
void withdraw(const Date &date,double amount,const std::string &desc);
//結算利息,每年1月1日調用一次該函數
void settle(const Date &date);
void show()const;
private:
Accumulator acc; //輔助計算利息的累加器
double credit; //信用額度
double rate; //欠款的日利率
double fee; //信用卡年費
double getdebt()const //獲得欠款額
{
double balance=getbalance();
return (balance<0?balance:0);
}
};
#endif
#endif
#if 0 //4
#ifndef _ACCOUNTH
#define _ACCOUNTH
#include "date.h"
#include "accumulator.h"
#include<string>
class Account //賬戶類
{
private:
std::string id; //賬號
double balance; //余額
static double total; //所有賬戶的總金額
protected:
//供派生類調用的構造函數,id為賬戶
Account(const Date &date,const std::string &id);
//記錄一筆賬,date為日期,amount為金額,desc為說明
void record(const Date &date,double amount,const std::string &desc);
//報告錯誤信息
void error(const std::string &msg)const;
public:
const std::string &getid()const
{return id;}
double getbalance()const
{return balance;}
static double gettotal()
{return total;}
virtual void deposit(const Date &date,double amount,const std::string &desc)=0;
virtual void withdraw(const Date &date,double amount,const std::string &desc)=0;
//結算(計算利息,年費等),每月結算一次,date為結算日期
virtual void settle(const Date &date)=0;
virtual void show()const;
};
class SavingsAccount:public Account //儲蓄賬戶類
{
public:
//構造函數
SavingsAccount(const Date &date,const std::string &id,double rate);
double getrate()
{return rate;}
//存入現金
void deposit(const Date &date,double amount,const std::string &desc);
//取出現金
void withdraw(const Date &date,double amount,const std::string &desc);
//結算利息,每年1月1日調用一次該函數
void settle(const Date &date);
private:
Accumulator acc; //輔助計算利息的累加器
double rate; //存款的年利率
};
class CreditAccount:public Account //信用賬戶類
{
public:
//構造函數
CreditAccount(const Date &date,const std::string &id,double credit,double rate,double fee);
double getcredit()const
{return credit;}
double getrate()const
{return rate;}
double getfee()const
{return fee;}
//獲得可用信用額度
double getavailablecredit()const
{
if(getbalance()<0)
return credit+getbalance();
else
return credit;
}
//存入現金
void deposit(const Date &date,double amount,const std::string &desc);
//取出現金
void withdraw(const Date &date,double amount,const std::string &desc);
//結算利息,每年1月1日調用一次該函數
void settle(const Date &date);
void show()const;
private:
Accumulator acc; //輔助計算利息的累加器
double credit; //信用額度
double rate; //欠款的日利息
double fee; //信用卡年費
double getdebt()const //獲得欠款額
{
double balance=getbalance();
return (balance<0?balance:0);
}
};
#endif
#endif
#if 0 //2 3
#ifndef _DATEH
#define _DATEH
class Date //日期類
{
public:
Date(int year,int month,int day);//用年,月,日構造日期
int getyear()const
{return year;}
int getmonth()const
{return month;}
int getday()const
{return day;}
int getmaxday()const; //獲得當月有多少天
bool isleapyear()const //判斷當年是否為閏年
{
return year%4==0&&year%100!=0||year%400==0;
}
void show()const; //輸出當前日期
int distance(const Date &date)const //計算兩個日期之間差多少天
{
return totaldays-date.totaldays;
}
private:
int year; //年
int month; //月
int day; //日
int totaldays; //該日期是從公元元年1月1日開始的第幾天
};
#endif
#endif
#if 0 //4
#ifndef _DATEH
#define _DATEH
class Date //日期類
{
private:
int year; //年
int month; //月
int day; //日
int totaldays; //該日期是從公元元年1月1日開始的第幾天
public:
Date(int year,int month,int day); //用年,月,日構造日期
int getyear()const
{return year;}
int getmonth()const
{return month;}
int getday()const
{return day;}
int getmaxday()const; //獲得當月有多少天
bool isleapyear()const //判斷當年是否為閏年
{
return year%4==0&&year%100!=0||year%400==0;
}
void show()const; //輸出當前日期
int operator-(const Date &date)const //計算兩個日期之間差多少天
{
return totaldays-date.totaldays;
}
};
#endif
#endif
#if 0 //2 3 4
#include"date.h"
#include<iostream>
#include<cstdlib>
using namespace std;
namespace //namespace使下面的定義只在當前文件中有效
{//存儲平年中的某個月1日之前有多少天,為方便於getmaxday函數的實現,該數組多出一項
const int DAYS_BEFORE_MONTH[]={0,31,59,90,120,151,181,212,243,273,304,334,365};
}
Date::Date(int year,int month,int day):year(year),month(month),day(day)
{
if(day<=0||day>getmaxday())
{
cout<<"invalid date";
show();
cout<<endl;
exit(1);
}
int years=year-1;
totaldays=years*365+years/4-years/100+years/400+DAYS_BEFORE_MONTH[month-1]+day;
if(isleapyear()&&month>2)
{totaldays++;}
}
int Date::getmaxday()const
{
if(isleapyear()&&month==2)
return 29;
else
return DAYS_BEFORE_MONTH[month]-DAYS_BEFORE_MONTH[month-1];
}
void Date::show()const
{cout<<getyear()<<"-"<<getmonth()<<"-"<<getday();}
#endif
#if 0 //3
#ifndef _ACCUMULATORH
#define _ACCUMULATORH
#include"date.h"
class Accumulator
{
public:
Accumulator(const Date &date,double value)
:lastdate(date),value(value),sum(0){}
double getsum(const Date &date)const
{return sum+value*date.distance(lastdate);}
void change(const Date &date,double value)
{
sum=getsum(date);
lastdate=date;
this->value=value;
}
void reset(const Date &date,double value)
{
lastdate=date;
this->value=value;
sum=0;
}
private:
Date lastdate;
double value;
double sum;
};
#endif
#endif
#if 0 //4
#ifndef _ACCUMULATORH
#define _ACCUMULATORH
#include"date.h"
class Accumulator //將某個數值按日累加
{
private:
Date lastdate; //上次變更數值的日期
double value; //數值的當前值
double sum; //數值按日累加之和
public:
//構造函數,date為開始累加的日期,value為初始值
Accumulator(const Date &date,double value)
:lastdate(date),value(value),sum(0){}
//獲得日期date累加的結果
double getsum(const Date &date)const
{
return sum+value*(date-lastdate);
}
//在date將數值變更為value
void change(const Date &date,double value)
{
sum=getsum(date);
lastdate=date;
this->value=value;
sum=0;
}
//初始化,將日期變更為date,數值變更為value,累加器清零
void reset(const Date &date,double value)
{
lastdate=date;
this->value=value;
sum=0;
}
};
#endif
#endif
#if 0 //0
#include<iostream>
#include<cmath>
using namespace std;
class savingsaccount
{
private:
int id;
double balance;
double rate;
int lastdate;
double accumulation;
void record(int date,double amount);
double accumulate(int date)const
{
return accumulation+balance(date-lastdate);
}
public:
savingsaccount(int date,int id,double rate);
int getID()
{
return id;
}
double getbalance()
{
return balance;
}
double getrate()
{
return rate;
}
void deposit(int date,double amount);
void withdraw(int date,double amount);
void settle(int date);
void show();
};
savingsaccount::savingsaccount(int date,int id,double rate)
:id(id),balance(0),rate(rate),lastdate(date),accumulation(0)
{
cout<<date<<"\t#"<<id<<"is created"<<endl;
}
void savingsaccount::record(int date,double amount)
{
accumulation=accumulate(date);
lastdate=date;
amount=floor(amount100+0.5)/100;
balance+=amount;
cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void savingsaccount::deposit(int date,double amount)
{
record(date,amount);
}
void savingsaccount::withdraw(int date,double amount)
{
if(amount>getbalance())
cout<<"error:not enough money"<<endl;
else
record(date,-amount);
}
void savingsaccount::settle(int date)
{
double interest=accumulate(date)*rate/365;
if(interest!=0)
record(date,interest);
accumulation=0;
}
void savingsaccount::show()
{
cout<<"#"<<id<<"\tbalance:"<<balance;
}
int main()
{
savingsaccount sa0(1,21325302,0.015);
savingsaccount sa1(1,58320212,0.015);
sa0.deposit(5,5000);
sa1.deposit(25,10000);
sa0.deposit(45,5500);
sa1.withdraw(60,4000);
sa0.settle(90);
sa1.settle(90);
sa0.show();
cout<<endl;
sa1.show();
cout<<endl;
return 0;
}
#endif
#if 0 //1
#include"個人銀行賬戶.h"
#include<iostream>
using namespace std;
int main()
{
//建立幾個賬戶
SavingsAccount sa0(1,21325302,0.015);
SavingsAccount sa1(1,58320212,0.015);
//幾筆賬目
sa0.deposit(5,5000);
sa1.deposit(25,10000);
sa0.deposit(45,5500);
sa1.withdraw(60,4000);
//開戶後第90天到了銀行的計息日,結算所有賬戶的年息
sa0.settle(90);
sa1.settle(90);
//輸出各賬戶信息
sa0.show(); cout<<endl;
sa1.show(); cout<<endl;
cout<<"total:"<<SavingsAccount::gettotal()<<endl;
return 0;
}
#endif
#if 0 //2
#include"個人銀行賬戶.h"
#include<iostream>
using namespace std;
int main()
{
Date date(2008,11,1); //起始日期
//建立幾個賬戶
SavingsAccount accounts[]={ SavingsAccount(date,"03755217",0.015), SavingsAccount(date,"02342342",0.015)};
const int n=sizeof(accounts)/sizeof( SavingsAccount); //賬戶總數
accounts[0].deposit(Date(2008,11,5),5000,"salary");
accounts[1].deposit(Date(2008,11,25),10000,"seii stock 0323");
accounts[0].deposit(Date(2008,12,5),5500,"salary");
accounts[1].withdraw(Date(2008,12,20),4000,"buy a laptop");
cout<<endl;
//結算所有賬戶並輸出各賬戶信息
for(int i=0;i<n;i++)
{
accounts[i].settle(Date(2009,1,1));
accounts[i].show();
cout<<endl;
}
cout<<"total:"<< SavingsAccount::gettotal()<<endl;
return 0;
}
#endif
#if 0 //3
#include"個人銀行賬戶.h"
#include<iostream>
using namespace std;
int main()
{
Date date(2008,11,1); //起始日期
//建立幾個賬戶
SavingsAccount sa1(date,"s3755217",0.015);
SavingsAccount sa2(date,"02342342",0.015);
CreditAccount ca(date,"c5392394",10000,0.0005,50);
sa1.deposit(Date(2008,11,5),5000,"salary");
ca.withdraw(Date(2008,11,15),2000,"buy a cell");
sa2.deposit(Date(2008,11,25),10000,"sell stock 0323");
ca.settle(Date(2008,12,1)); //結算信用卡
ca.deposit(Date(2008,12,1),2016,"repay the credit");
sa1.deposit(Date(2008,12,5),5500,"salarly");
//結算所有賬戶
sa1.settle(Date(2009,1,1));
sa2.settle(Date(2009,1,1));
ca.settle(Date(2009,1,1));
//輸出各賬戶的信息
cout<<endl;
sa1.show();
cout<<endl;
sa2.show();
cout<<endl;
ca.show();
cout<<endl;
cout<<"total:"<<Account::gettotal()<<endl;
return 0;
}
#endif
#if 0 //4
#include"個人銀行賬戶.h"
#include<iostream>
using namespace std;
int main()
{
Date date(2008,11,1); //起始日期
SavingsAccount sa1(date,"s3755217",0.015);
SavingsAccount sa2(date,"02342342",0.015);
CreditAccount ca(date,"c5392394",10000,0.0005,50);
Account accounts[]={&sa1,&sa2,&ca};
const int n=sizeof(accounts)/sizeof(Account); //賬戶總數
cout<<"(d)deposit(w)withdraw(s)show(c)change day(n)next month(e)exit"<<endl;
char cmd;
do{ //顯示日期和總金額
date.show();
cout<<"\ttotal:"<<Account::gettotal()<<"\tcommand>";
int index,day;
double amount;
string desc;
cin>>cmd;
switch(cmd)
{
case ‘d‘: //存入現金
cin>>index>>amount;
getline(cin,desc);
accounts[index]->deposit(date,amount,desc);
break;
case ‘w‘: //取出現金
cin>>index>>amount;
getline(cin,desc);
accounts[index]->withdraw(date,amount,desc);
break;
case ‘s‘: //查詢各賬戶信息
for(int i=0;i<n;i++)
{
cout<<"["<<i<<"]";
accounts[i]->show();
cout<<endl;
}
break;
case ‘c‘: //改變日期
cin>>day;
if(day<date.getday())
cout<<"you cannot specify a previous day";
else if(day>date.getmaxday())
cout<<"invalid day";
else
date=Date(date.getyear(),date.getmonth(),day);
break;
case ‘n‘: //進入下個月
if(date.getmonth()==12)
date=Date(date.getyear()+1,1,1);
else
date=Date(date.getyear(),date.getmonth()+1,1);
for(int i=0;i<n;i++)
accounts[i]->settle(date);
break;
}
}while(cmd!=‘e‘);
return 0;
}
#endif
#if 0 //1
#include"個人銀行賬戶.h"
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total=0;
//SavingsAccount 類相關成員函數的實現
SavingsAccount::SavingsAccount(int date,int id,double rate)
:id(id),balance(0),rate(rate),lastdate(date),accumulation(0)
{
cout<<date<<"\t#"<<id<<"is created"<<endl;
}
void SavingsAccount::record(int date,double amount)
{
accumulation=accumulate(date);
lastdate=date;
amount=floor(amount100+0.5)/100; //保留小數點後兩位
balance+=amount;
total+=amount;
cout<<date<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<endl;
}
void SavingsAccount::deposit(int date,double amount)
{record(date,amount);}
void SavingsAccount::withdraw(int date,double amount)
{
if(amount>getbalance())
{cout<<"error:not endough money"<<endl;}
else
record(date,-amount);
}
void SavingsAccount::settle(int date)
{
double interest=accumulate(date)rate/365;
if(interest!=0)
record(date,interest);
accumulation=0;
}
void SavingsAccount::show()const
{
cout<<"#"<<id<<"\tbalance:"<<balance;
}
#endif
#if 0 //2
#include"個人銀行賬戶.h"
#include<cmath>
#include<iostream>
using namespace std;
double SavingsAccount::total=0;
//類相關成員函數的實現
SavingsAccount:: SavingsAccount(const Date &date,const string &id,double rate)
:id(id),balance(0),rate(rate),lastdate(date),accumulation(0)
{
date.show();
cout<<"\t#"<<id<<"created"<<endl;
}
void SavingsAccount::record(const Date &date,double amount,const string &desc)
{
accumulation=accumulate(date);
lastdate=date;
amount=floor(amount100+0.5)/100; //保留小數點後兩位
balance+=amount;
total+=amount;
date.show();
cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
}
void SavingsAccount::error(const string &msg)const
{cout<<"error(#"<<id<<"):"<<msg<<endl;}
void SavingsAccount::deposit(const Date &date,double amount,const string &desc)
{record(date,amount,desc);}
void SavingsAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount>getbalance())
error("not enough money");
else
record(date,-amount,desc);
}
void SavingsAccount::settle(const Date &date) //計算年息
{
double interest=accumulate(date)rate/date.distance (Date(date.getyear()-1,1,1));
if(interest!=0)
record(date,interest,"interest");
accumulation=0;
}
void SavingsAccount::show()const
{
cout<<id<<"\tbalance:"<<balance;
}
#endif
#if 0 //3
#include"個人銀行賬戶.h"
#include<cmath>
#include<iostream>
using namespace std;
double Account::total=0;
//Account類的實現
Account::Account(const Date &date,const string &id):id(id),balance(0)
{
date.show();
cout<<"\t#"<<id<<"created"<<endl;
}
void Account::record(const Date &date,double amount,const string &desc)
{
amount=floor(amount100+0.5)/100; //保留小數點後兩位
balance+=amount;
total+=amount;
date.show();
cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
}
void Account::show()const
{cout<<id<<"\tbalance:"<<balance;}
void Account::error(const string &msg)const
{cout<<"error(#"<<id<<"):"<<msg<<endl;}
//SavingsAccount類相關成員函數的實現
SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate)
:Account(date,id),rate(rate),acc(date,0){}
void SavingsAccount::deposit(const Date &date,double amount,const string &desc)
{
record(date,amount,desc);
acc.change(date,getbalance());
}
void SavingsAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount>getbalance())
error("not enough money");
else
{
record(date,-amount,desc);
acc.change (date,getbalance());
}
}
void SavingsAccount::settle(const Date &date) //計算年息
{
double interest=acc.getsum(date)rate/date.distance(Date(date.getyear()-1,1,1));
if(interest!=0)
record(date,interest,"interest");
acc.reset(date,getbalance());
}
//CreditAccount類相關成員函數的實現
CreditAccount::CreditAccount(const Date &date,const string &id,double credit,double rate,double fee)
:Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){}
void CreditAccount::deposit(const Date &date,double amount,const string &desc)
{
record(date,amount,desc);
acc.change(date,getdebt());
}
void CreditAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount-getbalance()>credit)
error("not enough money");
else
{
record(date,-amount,desc);
acc.change(date,getdebt());
}
}
void CreditAccount::settle(const Date &date)
{
double interest=acc.getsum(date)*rate;
if(interest!=0)
record(date,interest,"interest");
if(date.getmonth()==1)
record(date,-fee,"annual fee");
acc.reset(date,getdebt());
}
void CreditAccount::show()const
{
Account::show();
cout<<"\tavailable credit:"<<getavailablecredit();
}
#endif
#if 0 //4
#include"個人銀行賬戶.h"
#include<cmath>
#include<iostream>
using namespace std;
double Account::total=0;
//Account類的實現
Account::Account(const Date &date,const string &id):id(id),balance(0)
{
date.show();
cout<<"\t#"<<id<<"created"<<endl;
}
void Account::record(const Date &date,double amount,const string &desc)
{
amount=floor(amount100+0.5)/100; //保留小數點後兩位
balance+=amount;
total+=amount;
date.show();
cout<<"\t#"<<id<<"\t"<<amount<<"\t"<<balance<<"\t"<<desc<<endl;
}
void Account::show()const
{cout<<id<<"\tbalance:"<<balance;}
void Account::error(const string &msg)const
{cout<<"error(#"<<id<<"):"<<msg<<endl;}
//SavingsAccount類相關成員函數的實現
SavingsAccount::SavingsAccount(const Date &date,const string &id,double rate)
:Account(date,id),rate(rate),acc(date,0){}
void SavingsAccount::deposit(const Date &date,double amount,const string &desc)
{
record(date,amount,desc);
acc.change(date,getbalance());
}
void SavingsAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount>getbalance())
error("not enough money");
else
{
record(date,-amount,desc);
acc.change (date,getbalance());
}
}
void SavingsAccount::settle(const Date &date) //每年的1月計算一次利息
{
if(date.getmonth()==1)
{
double interest=acc.getsum(date)rate/(date-Date(date.getyear()-1,1,1));
if(interest!=0)
record(date,interest,"interest");
acc.reset(date,getbalance());
}
}
//CreditAccount類相關成員函數的實現
CreditAccount::CreditAccount(const Date &date,const string &id,double credit,double rate,double fee)
:Account(date,id),credit(credit),rate(rate),fee(fee),acc(date,0){}
void CreditAccount::deposit(const Date &date,double amount,const string &desc)
{
record(date,amount,desc);
acc.change(date,getdebt());
}
void CreditAccount::withdraw(const Date &date,double amount,const string &desc)
{
if(amount-getbalance()>credit)
error("not enough money");
else
{
record(date,-amount,desc);
acc.change(date,getdebt());
}
}
void CreditAccount::settle(const Date &date)
{
double interest=acc.getsum(date)*rate;
if(interest!=0)
record(date,interest,"interest");
if(date.getmonth()==1)
record(date,-fee,"annual fee");
acc.reset(date,getdebt());
}
void CreditAccount::show()const
{
Account::show();
cout<<"\tavailable credit:"<<getavailablecredit();
}
#endif
個人銀行賬戶小程序