C++代码源自《C++语言程序设计(第4版)》(清华大学计算机系列教材) 郑莉, 董渊, 何江舟 清华大学出版社
7_10
C++源码
//7_10.cpp
#include "account.cpp"
#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);
//11月份的几笔账目
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));
//12月份的几笔账目
ca.deposit(Date(2008, 12, 1), 2016, "repay the credit");
sa1.deposit(Date(2008, 12, 5), 5500, "salary");
//结算所有账户
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;
}
//account.cpp
#include "account.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 << " #" << id << " created" << endl;
}
void Account::record(const Date &date, double amount, const string &desc) {
amount = floor(amount * 100 + 0.5) / 100; //保留小数点后两位
balance += amount;
total += amount;
date.show();
cout << " #" << id << " " << amount << " " << balance << " " << desc << endl;
}
void Account::show() const {
cout << id << " Balance: " << 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 credit");
} 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 << " Available credit:" << getAvailableCredit();
}
//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#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; }
//显示账户信息
void show() const;
};
class SavingsAccount : public Account { //储蓄账户类
private:
Accumulator acc; //辅助计算利息的累加器
double rate; //存款的年利率
public:
//构造函数
SavingsAccount(const Date &date, const std::string &id, double rate);
double getRate() const { 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);
};
class CreditAccount : public Account { //信用账户类
private:
Accumulator acc; //辅助计算利息的累加器
double credit; //信用额度
double rate; //欠款的日利率
double fee; //信用卡年费
double getDebt() const { //获得欠款额
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
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日调用一次该函数
void settle(const Date &date);
void show() const;
};
#endif //__ACCOUNT_H__
//accumulator.h
#ifndef __ACCUMULATOR_H__
#define __ACCUMULATOR_H__
#include "date.cpp"
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.distance(lastDate);
}
//在date将数值变更为value
void change(const Date &date, double value) {
sum = getSum(date);
lastDate = date;
this->value = value;
}
//初始化,将日期变为date,数值变为value,累加器清零
void reset(const Date &date, double value) {
lastDate = date;
this->value = value;
sum = 0;
}
};
#endif //__ACCUMULATOR_H__
//date.cpp
#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();
}
//date.h
#ifndef __DATE_H__
#define __DATE_H__
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 distance(const Date& date) const {
return totalDays - date.totalDays;
}
};
#endif //__DATE_H__
C++运行结果
Java代码
//Test.java
public class Test {
public static void main(String[] args) {
Date date=new Date(2008, 11, 1); //起始日期
//建立几个账户
SavingsAccount sa1=new SavingsAccount(date, "S3755217", 0.015);
SavingsAccount sa2=new SavingsAccount(date, "02342342", 0.015);
CreditAccount ca=new CreditAccount(date, "C5392394", 10000, 0.0005, 50);
//11月份的几笔账目
sa1.deposit(new Date(2008, 11, 5), 5000, "salary");
ca.withdraw(new Date(2008, 11, 15), 2000, "buy a cell");
sa2.deposit(new Date(2008, 11, 25), 10000, "sell stock 0323");
//结算信用卡
ca.settle(new Date(2008, 12, 1));
//12月份的几笔账目
ca.deposit(new Date(2008, 12, 1), 2016, "repay the credit");
sa1.deposit(new Date(2008, 12, 5), 5500, "salary");
//结算所有账户
sa1.settle(new Date(2009, 1, 1));
sa2.settle(new Date(2009, 1, 1));
ca.settle(new Date(2009, 1, 1));
//输出各个账户信息
System.out.println();
sa1.show();System.out.println();
sa2.show(); System.out.println();
ca.show(); System.out.println();
System.out.println("Total: " + Account.getTotal());
}
}
//Account.java
public class Account {
private String id; //帐号
double balance; //余额
static double total=0; //所有账户的总金额
final String getId() { return id; }
final double getBalance() { return balance;}
static double getTotal() { return total;}
Account(final Date date, final String id){
this.id=id;
this.balance=0;
date.show();
System.out.println(" #" + id + "created");
}
void record(final Date date, double amount,final String desc) {
amount = Math.floor((amount * 100 + 0.5) / 100);// 保留小数点后两位
balance += amount;
total += amount;
date.show();
System.out.println(" #" + id + " " + amount + " " + balance + " " + desc);
}
void show() {
System.out.print(id + " Balance: " + balance);
}
final void error(final String msg) {
System.out.println("Error(#" + id + "): " + msg +"
");
}
}
//SavingsAccount.java
public class SavingsAccount extends Account {
private Accumulator acc; //辅助计算利息的累加器
private double rate; //存款的年利率
final double getRate() { return rate; }
// 构造函数
SavingsAccount(final Date date, String id, double rate) {
super(date, id);
this.rate=rate;
this.acc=new Accumulator(date, 0);
}
// 存入现金
void deposit(final Date date, double amount,final String desc) {
record(date, amount, desc);
acc.change(date, getBalance());
}
// 取出现金
void withdraw(final Date date, double amount,final String desc) {
if (amount > getBalance()) {
error("not enough money");
} else {
record(date, -amount, desc);
acc.change(date, getBalance());
}
}
// 结算利息,每年1月1日调用一次该函数
void settle(final Date date) {
double interest = acc.getSum(date) * rate //计算年息
/ date.distance(new Date(date.getYear() - 1, 1, 1));
if (interest != 0)
record(date, interest, "interest");
acc.reset(date, getBalance());
}
}
//CreditAccount.java
public class CreditAccount extends Account {
//CreditAccount类相关成员函数的实现//信用账户类
private Accumulator acc; //辅助计算利息的累加器
private double credit; //信用额度
private double rate; //欠款的日利率
private double fee; //信用卡年费
final private double getDebt() { //获得欠款额
double balance = getBalance();
return (balance < 0 ? balance : 0);
}
CreditAccount(final Date date, final String id, double credit, double rate, double fee)
{ super(date, id);
this.credit=credit;
this.rate=rate;
this.fee=fee;
this.acc=new Accumulator(date, 0);
}
//构造函数
final double getCredit() { return credit; }
final double getRate(){ return rate; }
final double getFee() { return fee; }
final double getAvailableCredit() { //获得可用信用
if (getBalance() < 0)
return credit + getBalance();
else
return credit;
}
// 存入现金
void deposit(final Date date, double amount,final String desc) {
record(date, amount,desc);
acc.change(date, getDebt());
}
// 取出现金
void withdraw(final Date date, double amount,final String desc) {
if (amount - getBalance() > credit) {
error("not enough credit");
} else {
record(date, -amount, desc);
acc.change(date, getDebt());
}
}
// 结算利息,每年1月1日调用一次该函数
void settle(final 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());
}
final void show(){
super.show();
System.out.println(" Available credit:" + getAvailableCredit());
}
}
//Accumulator.java
public class Accumulator {
//将某个数值按日累加
private Date lastDate; // 上次变更数值的时期
private double value; // 数值的当前值
private double sum; // 数值按日累加之和
// 构造函数,date为开始累加的日期,value为初始值
Accumulator(final Date date, double value) {
this.lastDate = date;
this.value = value;
this.sum = 0;
}
// 获得到日期date的累加结果
final double getSum(final Date date) {
return sum + value * date.distance(lastDate);
}
// 在date将数值变更为value
void change(final Date date, double value) {
sum = getSum(date);
lastDate = date;
this.value = value;
}
// 初始化,将日期变为date,数值变为value,累加器清零
void reset(final Date date, double value) {
lastDate = date;
this.value = value;
sum = 0;
}
}
//Date.java
public class Date {
// 存储平年中某个月1日之前有多少天,为便于getMaxDay函数的实现,该数组多出一项
final int[] DAYS_BEFORE_MONTH = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
private int year; // 年
int month; // 月
int day; // 日
int totalDays; // 该日期是从公元元年1月1日开始的第几天
public Date(int year, int month, int day) {
this.year = year;
this.month = month;
this.day = day;
if (day <= 0 || day > getMaxDay()) {
System.out.println("Invalid date: ");
show();
System.out.println();
System.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++;
}// 用年、月、日构造日期
final int getYear() {
return year;
}
final int getMonth() {
return month;
}
final int getDay() {
return day;
}
final int getMaxDay() {
if (isLeapYear() && month == 2)
return 29;
else
return DAYS_BEFORE_MONTH[month]- DAYS_BEFORE_MONTH[month - 1];
}; // 获得当月有多少天
final boolean isLeapYear() { //判断当年是否为闰年
return year % 4 == 0 && year % 100 != 0 || year % 400 == 0;
}
final void show() {
System.out.print( getYear()+ "-" + getMonth()+"-" + getDay());//输出当前日期
}
//计算两个日期之间差多少天
final int distance(final Date date) {
return totalDays - date.totalDays;
}
}