//自学,对于“获得到指定日期为止的存款金额按日累加值”这句话理解不能T_T。因此修改类部分内容,忽略部分逻辑=_=……
#include <iostream> #include <cmath> using namespace std; class SavingsAccount { //储蓄账户类 private: int id; //账号 double balance; //余额 double rate; //存款的年利率 double interest=0.0; //利息 //记录一笔账,date为日期,amout为金额,desc为说明 void record(int date, double amout); 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 dadte, double amount); //结算利息 double lixi(int date, double amount); //显示账户信息 void show(); }; //SavingsAccount相关成员函数的实现 SavingsAccount::SavingsAccount(int date, int id, double rate) :id(id), balance(0), rate(rate) { cout << date << "\t#" << id << " is created" << endl; } void SavingsAccount::record(int date, double amount) { amount = floor(amount * 100+0.5)/100; //floor函数的目的:保留小数点后两位 balance += amount; cout << date << "\t#" << id << "\t" << amount <<"\t"<< balance << endl; } void SavingsAccount::deposit(int date, double amount) { record(date, amount); lixi(date, amount); } void SavingsAccount::withdraw(int date, double amount) { if (amount > getBalance()) //如果要取的钱大于存款余额 cout << "Error:not enough money" << endl; else record(date, -amount); lixi(date, -amount); } void SavingsAccount::show() { cout << "#" << id << "\tBalance:" << (balance+interest)<<endl; cout << "#" << id << "\tlixi:" << interest; } double SavingsAccount::lixi(int date, double amount) { //计算不同操作下的存款利息,并累加 interest += ((amount*rate) / 365)*(90 - date); interest = floor(interest * 100 + 0.5) / 100; return interest; } 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.show(); cout << endl; sa1.show(); cout << endl; return 0; }