(由網(wǎng)課老師演示后窄赋,本人編寫練習(xí)而來)
基礎(chǔ)算法
一赡勘、分析
(1)數(shù)據(jù)成員
根據(jù)個(gè)人儲蓄賬戶特點(diǎn)嫂便,一個(gè)賬戶(即一個(gè)對象)必須包含如下數(shù)據(jù),另外闸与,還需要顯示整個(gè)系統(tǒng)現(xiàn)存金額顽悼。
數(shù)據(jù)名稱 | 變量 | 類型 |
---|---|---|
賬號 | id |
int |
余額 | balance |
double |
年利率 | rate |
double |
日期(補(bǔ)) | lastDate |
int |
全年每天余額加和(補(bǔ)) | accumulation |
double |
系統(tǒng)總金額 | total |
static double |
“系統(tǒng)總金額” 為整個(gè)類所共有,并非哪個(gè)對象所有几迄,故將其聲明為靜態(tài)變量。
(2)函數(shù)成員
創(chuàng)建一個(gè)賬戶()后冰评,對該賬戶所執(zhí)行的一些必要操作有
- 數(shù)據(jù)信息的公共訪問接口
- 獲取賬戶信息:
getId():int;
- 獲取余額:
getBalance():double;
- 獲取年利率:
getRate():double;
- 獲取總額:
getTotal():double;
- 獲取賬戶信息:
- 對賬戶的一些基本操作
- 存款:
deposit(int date, double amount):void;
- 取款:
withdraw(int date, double amount):void;
- 計(jì)算利息:
settle(int date):void;
- 顯示賬戶信息:
show():void;
- 存款:
二映胁、算法設(shè)計(jì)
(1)UML圖
SavingsAccount |
---|
- id:int - balance:double - rate:double - total:static double // 靜態(tài)數(shù)據(jù)成員必須使用靜態(tài)函數(shù)成員處理 - lastDate:int - accumulation:double
|
+ SavingsAccount():void//構(gòu)造函數(shù) + <<const>>getId():int + <<const>>getBalance():double + <<const>>getRate():double + <<static>>getTotal():double + deposit(int date, double amount):void // amount金額 + withdraw(int date, double amount):void + settle(int date):void + show():void + record(int date, double amount):void
|
(2)核心
- 日期:簡化處理,例如:第2天存入500元甲雅,第5天再存入200解孙,則500元維持的天數(shù)為
5-2=3
天;即日期為一年中第x天抛人。
- 利息計(jì)算
- 日均余額=一年中每天的余額加和(
accumulation
)/一年的總天數(shù)弛姜; - 年利(
interest
)=日均余額*年利率;
- 日均余額=一年中每天的余額加和(
- 由于存妖枚、取廷臼,其對數(shù)據(jù)成員的操作完全相同,故提取出一個(gè)
record(...);
記賬函數(shù)绝页,減少重復(fù)荠商。 - 增加一個(gè)函數(shù)
accumulate(int date);
,計(jì)算在第二次余額發(fā)生變動時(shí)续誉,第一階段的每日余額加和值莱没。
//account.hpp
#ifndef account_hpp
#define account_hpp
#include <stdio.h>
using namespace std;
#define YPERD 365
class SavingsAccount{
private:
int id;
int lastDate;
double balance;
double rate;
double accumulation;
static double total;
void record(int date,double amount);
inline double accumulate(int date) const { return accumulation+balance*(date-lastDate); }
public:
SavingsAccount(int id,int date,double rate);//構(gòu)造函數(shù)
inline int getId() const { return id; }
inline double getBalance() const { return balance; }
inline double getRate() const { return rate; }
inline static double getTotal() { return total; }
void deposit(int date,double amount);
void withdraw(int date,double amount);
void settle(int date);
void show();
};
#endif /* account_hpp */
//account.cpp
#include "account.hpp"
#include <iostream>
using namespace std;
double SavingsAccount::total=0;
//構(gòu)造函數(shù)
SavingsAccount::SavingsAccount(int date,int id,double rate):id(id),lastDate(date),balance(0),rate(rate),accumulation(0){
cout<<"\t#"<<id<<" is create."<<endl;
}
void SavingsAccount::record(int date,double amount){
accumulation=accumulate(date);
amount=(amount*100+0.5)/100;
lastDate=date;
balance+=amount;
total+=amount;
cout<<"\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>balance)
cout<<"Error, No enough money."<<endl;
else
record(date, -amount); }
void SavingsAccount::settle(int date){
double interest=accumulate(date)*rate/YPERD;
if (interest)
record(date, interest);
accumulation=0;
}
void SavingsAccount::show(){
cout <<"\t#"<< id << "\tBalance: " << balance;
}
//main.cpp
#include <iostream>
#include "account.hpp"
using namespace std;
int main(int argc, const char * argv[]) {
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;
cout<<"\t---System Total:"<<SavingsAccount::getTotal()<<"---"<<endl;
return 0;
}
運(yùn)行結(jié)果:
#21325302 is create.
#58320212 is create.
#21325302 5000.01 5000.01
#58320212 10000 10000
#21325302 5500.01 10500
#58320212 -3999.99 6000.01
#21325302 27.642 10527.7
#58320212 21.7858 6021.8
#21325302 Balance: 10527.7
#58320212 Balance: 6021.8
---System Total:16549.4---