第一范文网 - 专业文章范例文档资料分享平台

7 第6章 数组、指针与字符串(二)

来源:用户分享 时间:2025/5/31 3:33:04 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

#include using namespace std;

namespace { //namespace使下面的定义只在当前文件中有效 项

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 << \; 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(); }

//account.h

#ifndef __ACCOUNT_H__ #define __ACCOUNT_H__ #include \ #include

class SavingsAccount { //储蓄账户类

//存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一

private:

//记录一笔帐,date为日期,amount为金额,desc为说明

void record(const Date &date, double amount, const std::string &desc); //报告错误信息

void error(const std::string &msg) const; //获得到指定日期为止的存款金额按日累积值 std::string id; double balance; double rate;

//帐号 //余额

//存款的年利率

//上次变更余额的时期

Date lastDate;

double accumulation; //余额按日累加之和 static double total; //所有账户的总金额

double accumulate(const Date& date) const {

return accumulation + balance * date.distance(lastDate); } 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;

};

#endif //__ACCOUNT_H__

//account.cpp

#include \ #include #include using namespace std;

double SavingsAccount::total = 0;

//SavingsAccount类相关成员函数的实现

SavingsAccount::SavingsAccount(const Date &date, const string &id, double rate)

: id(id), balance(0), rate(rate), lastDate(date), accumulation(0) { date.show();

cout << \ << id << \ << endl; }

void SavingsAccount::record(const Date &date, double amount, const string &desc) {

accumulation = accumulate(date); lastDate = date;

amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位

balance += amount; total += amount; date.show();

cout << \ << id << \ << amount << \ << balance << \ << desc << endl; }

void SavingsAccount::error(const string &msg) const { cout << \ << 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(\);

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, \); accumulation = 0; }

void SavingsAccount::show() const {

cout << id << \ << balance; }

//例6_25 个人银行帐户管理程序改进.cpp #include \ #include using namespace std;

int main() {

Date date(2008, 11, 1); //起始日期 //建立几个账户

SavingsAccount accounts[] = {

SavingsAccount(date, \, 0.015), SavingsAccount(date, \, 0.015) };

const int n = sizeof(accounts) / sizeof(SavingsAccount); //账户总数 //11月份的几笔账目

accounts[0].deposit(Date(2008, 11, 5), 5000, \);

accounts[1].deposit(Date(2008, 11, 25), 10000, \); //12月份的几笔账目

accounts[0].deposit(Date(2008, 12, 5), 5500, \);

accounts[1].withdraw(Date(2008, 12, 20), 4000, \); //结算所有账户并输出各个账户信息 cout << endl;

}

for (int i = 0; i < n; i++) {

accounts[i].settle(Date(2009, 1, 1)); accounts[i].show(); cout << endl; }

cout << \ << SavingsAccount::getTotal() << endl; return 0;

运行结果:

搜索更多关于: 7 第6章 数组、指针与字符串(二) 的文档
7 第6章 数组、指针与字符串(二).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8ulss5up8i6ksx797jw59jajr88l5800wu5_10.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top