在网上找了很多例子,代码都不是循序渐进的,自己一点一点按章节把代码按照老师的要求都码好了,一点一点进步吧,因为一开始就去看别人整篇的代码实在很难,带给需要帮助的人,这是第六章第二个综合实例,加入Date类以后,又单独写了一个文章,这个CSDN我实在不会用,我想把每一章对应的课件图片传上来的,太麻烦了。我有课件,需要的可以评论区找我要。
我已经到第12章的代码都码好了,以后会陆续上传上来。
//account.h #ifndef _ACCOUNT_H_ #define _ACCOUNT_H_ #include "date.h" #includeclass SavingsAccount //储蓄类账户 { public: SavingsAccount(const Date &date, const std::string &id, double rate);//构造函数 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日调用一次该函数 void settle(const Date& date); //显示账户信息 void show()const; ~SavingsAccount(); 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);//lastDate为已创建的Date类对象 } }; #endif //_ACCOUNT_H_ //account.cpp #include #include "account.h" using namespace std; double SavingsAccount::total = 0; SavingsAccount::SavingsAccount(const Date& date, const std::string& id, double rate) :id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { date.show(); cout << "t#" << id << " is created" << endl; } //记录一笔账,date为日期,amount为金额,desc为说明 void SavingsAccount::record(const Date& date, double amount, const std::string& desc) { accumulation = accumulate(date); //按日累加值 lastDate = date; //变更日期,将本次日期更新成下一次的起算日期 amount = floor(amount * 100 + 0.5) / 100;//保留小数点后两位,需要包含cmath头文件 balance += amount; //变更余额 total += amount; date.show(); cout << "t#" << id << "t" << amount << "t" << balance << "t"< getBalance()) { cout << "Error: not enough money" << endl; } else { record(date, -amount,desc); } } //结算利息,每年一月1日调用一次该函数 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; } SavingsAccount::~SavingsAccount() { } //date.h #ifndef _DATE_H_ #define _DATE_H_ class Date //日期类 { public: Date(int year,int month,int day); ~Date(); int getYear() const { return year; } int getMonth() const { return month; } int getDay() const { return day; } int getMaxDay() const;//得到当前月的天数的getMaxDay函数 bool isLeapYear() const //判断当前年是否闰年isLeapYear函数 { return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } void show() const; //用来将当前日期输出的show函数 int distance(const Date& date) const //用来判断当前日期与指定日期相差天数的distance函数 { return totalDays - date.totalDays; //当前日期-之前以有日期(为已创建的Date类的对象) } private: int year; int month; int day; int totalDays; //该日期是从公元元年1月1日开始的第几天 }; #endif //_DATE_H_ //date.cpp //date.h #include #include #include "date.h" using namespace std; namespace { //namespace使下面的定义只在当前文件中生效 const int DAYS_BEFORE_MONTH[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 }; //31,28,31, 30, 31, 30, 31, 31, 30, 31, 30, 31 //3月有多少天=month[3]-month[2]=90-59=3天; } //getMaxDay()以此为基础计算当前月最大天数 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; //基准日期为公元元年1月1日,即1年1月1日 totalDays = years * 365 + years / 4 - years / 100 + years / 400 + DAYS_BEFORE_MONTH[month - 1] + day; //DAYS_BEFORE_MONTH[month - 1]获得当前month月1日与本年1月1日相差天数,在数组中查询得知 if (isLeapYear() && month > 2) { totalDays++; //闰年,并且大于2月时,总天数+1 } } //得到当前月的天数的getMaxDay函数 int Date::getMaxDay() const { //判断month是多少,然后在DAYS_BEFORE_MONTH数组中找到对应日期 if (isLeapYear()&& month==2) { return 29; } else { return DAYS_BEFORE_MONTH[month] - DAYS_BEFORE_MONTH[month - 1]; } } //用来将当前日期输出的show函数 void Date::show() const { cout << getYear() << "-" << getMonth() << "-" << getDay() ; //cout< #include #include "account.h" #include "date.h" using namespace std; int main() { Date date(2008,11,1); //起始日期 //建立几个账户 SavingsAccount accounts[] = { SavingsAccount(date, "S3755217", 0.015), SavingsAccount(date, "02342342" , 0.015) }; const int n = sizeof(accounts) / sizeof(SavingsAccount); //账户总数目 //11月份几笔账目 accounts[0].deposit(Date(2008,11, 5), 5000,"salary"); accounts[1].deposit(Date(2008, 11, 25), 10000, "sell stock 0323"); //12月份的几笔账目 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; //std::cout << "Hello World!n"; }



