Skip to content

Commit

Permalink
chapter 10 vector multiMap
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhouwk7 committed Feb 1, 2020
1 parent 55f2bdc commit 3d791e4
Show file tree
Hide file tree
Showing 27 changed files with 118 additions and 168 deletions.
Binary file modified .vs/banksysten1/v16/.suo
Binary file not shown.
Binary file modified .vs/banksysten1/v16/Browse.VC.db
Binary file not shown.
Binary file modified .vs/banksysten1/v16/ipch/AutoPCH/56a00252fd499d32/MAIN.ipch
Binary file not shown.
Binary file modified .vs/banksysten1/v16/ipch/AutoPCH/ecab3328522283e8/ACCOUNT.ipch
Binary file not shown.
Binary file modified .vs/banksysten1/v16/ipch/AutoPCH/efa007da48a9b125/DATE.ipch
Binary file not shown.
Binary file modified Release/account.obj
Binary file not shown.
Binary file modified Release/banksysten1.exe
Binary file not shown.
Binary file modified Release/banksysten1.iobj
Binary file not shown.
Binary file modified Release/banksysten1.ipdb
Binary file not shown.
10 changes: 6 additions & 4 deletions Release/banksysten1.log
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
 正在生成代码
1 of 162 functions ( 0.6%) were compiled, the rest were copied from previous compilation.
0 functions were new in current compilation
0 functions had inline decision re-evaluated but remain unchanged
 account.cpp
main.cpp
正在生成代码
68 of 323 functions (21.1%) were compiled, the rest were copied from previous compilation.
43 functions were new in current compilation
46 functions had inline decision re-evaluated but remain unchanged
已完成代码的生成
banksysten1.vcxproj -> E:\code\cplusplus\banksysten1\Release\banksysten1.exe
Binary file modified Release/banksysten1.pdb
Binary file not shown.
Binary file modified Release/banksysten1.tlog/CL.read.1.tlog
Binary file not shown.
Binary file modified Release/banksysten1.tlog/CL.write.1.tlog
Binary file not shown.
Binary file modified Release/banksysten1.tlog/banksysten1.write.1u.tlog
Binary file not shown.
Binary file modified Release/banksysten1.tlog/link.delete.1.tlog
Binary file not shown.
Binary file modified Release/banksysten1.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified Release/date.obj
Binary file not shown.
Binary file modified Release/main.obj
Binary file not shown.
Binary file modified Release/vc142.pdb
Binary file not shown.
32 changes: 29 additions & 3 deletions account.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
#include <iostream>
#include <utility> // pair
#include "account.h"
//using namespace std;
// AccountRecord 类

AccountRecord::AccountRecord(const Date& date, const Account* account, double amount, double balance, const std::string desc):
date(date), account(account), amount(amount),balance(balance),desc(desc){ }
void AccountRecord::show() const {
date.show();
std::cout << "\t#" << account->getId() << "\t" << amount << "\t" << balance << "\t" << desc << std::endl;
}


// Account 银行账户基类
double Account::total = 0;
double Account::total = 0; // 静态属性定义
RecordMap Account::recordMap;

Account::Account(const Date& date, const std::string& id) :id(id), balance(0) {
date.show();
Expand All @@ -11,13 +25,24 @@ void Account::record(const Date& date, double amount, const std::string& desc) {
amount = floor(amount * 100 + 0.5) / 100;
balance += amount;
total += amount;
date.show();
std::cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << std::endl;
/*date.show();
std::cout << "\t#" << id << "\t" << amount << "\t" << balance << "\t" << desc << std::endl;*/
recordMap.insert(recordMap.end(), std::pair<Date, AccountRecord>(date, AccountRecord(date,this,amount,getBalance(),desc)));
}
void Account::error(const std::string& msg) const {
std::cout << "error(#" << id << "):" << msg << std::endl;
}

void Account::query(const Date& begin, const Date& end) {
if (begin <= end) {
RecordMap::iterator iter1 = recordMap.lower_bound(begin);
RecordMap::iterator iter2 = recordMap.upper_bound(end);
for (RecordMap::iterator iter = iter1; iter != iter2; iter++) {
iter->second.show();
}
}
}

// SavingsAccount
SavingsAccount::SavingsAccount(const Date& date, const std::string& id, double rate) :
Account(date,id), acc(date,0),rate(rate){
Expand Down Expand Up @@ -54,6 +79,7 @@ void CreditAccount::deposit(const Date& date, double amount, const std::string&
acc.change(date, getDebt()); // 信用卡利息为负,不应该使用getBalance()
}
void CreditAccount::withdraw(const Date& date, double amount, const std::string& desc) {
std::cout << "credit" << credit << ",amount" << amount << ",balance" << getBalance() << std::endl;
if (credit < amount - getBalance()) { // 信用卡为正时,可用的金额大于 credit
error("Not enough credit!");
exit(1);
Expand Down
22 changes: 22 additions & 0 deletions account.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,35 @@
#ifndef ACCOUNT_H
#define ACCOUNT_H
#include <iostream>
#include <map>
#include <string>
#include "accumulator.h"
#include "date.h"
using namespace std::rel_ops; // >= ==模板函数生成
class Account;
//class AccountRecord;
//typedef std::multimap<Date, AccountRecord> RecordMap;


class AccountRecord {
private:
const Account* account;
Date date;
double amount;
double balance;
std::string desc;
public:
AccountRecord(const Date& date, const Account* account, double amount, double balance, const std::string desc);
void show() const;
};
typedef std::multimap<Date, AccountRecord> RecordMap;

class Account {
private:
std::string id;
double balance;
static double total;
static RecordMap recordMap;
protected:
Account(const Date& date, const std::string& id);
void record(const Date& date, double amount, const std::string& desc);
Expand All @@ -27,6 +48,7 @@ class Account {
virtual void withdraw(const Date& date, double amount, const std::string& desc) = 0;
virtual void settle(const Date& date) = 0;
virtual void show() const = 0;
static void query(const Date& begin, const Date& end);
};


Expand Down
96 changes: 0 additions & 96 deletions array.h

This file was deleted.

1 change: 0 additions & 1 deletion banksysten1.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@
<ItemGroup>
<ClInclude Include="account.h" />
<ClInclude Include="accumulator.h" />
<ClInclude Include="array.h" />
<ClInclude Include="date.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
Expand Down
3 changes: 0 additions & 3 deletions banksysten1.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,5 @@
<ClInclude Include="accumulator.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="array.h">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
</Project>
35 changes: 16 additions & 19 deletions date.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
#include <iostream> // cout
#include <cstdlib> // exit()
#include<iostream>
#include "date.h"

namespace { // 每月累计天数
int BEFORE_MONTH_DAYS[] = { 0,31,59,90,120,151,181,212,243,273,304,334,365 };
}

const int BEFROE_MONTH_DAYS[] = { 0,31,59,90,120,151,181,212,143,273,304,334,365 };
Date::Date(int year, int month, int day) :year(year), month(month), day(day) {
if (day > getMaxDays() || day <= 0 || month <= 0 || month > 12 || year < 0 ) {
int years = year - 1;
if (day > getMaxDays()) {
std::cout << "Invalid date!" << std::endl;
exit(1); // 日期无效,退出
exit(1);
}
int years = year - 1;
totalDays = years * 365 + years / 4 - years / 100 + years / 400 +
BEFORE_MONTH_DAYS[month - 1] + day;
totalDays = years * 365 + years / 4 - years / 100 + years / 400 + BEFROE_MONTH_DAYS[month - 1] + day;
if (isLeapYear()) totalDays++;
}

int Date::getMaxDays() const { // 获取每月最大天数
return BEFORE_MONTH_DAYS[month] - BEFORE_MONTH_DAYS[month-1];
int Date::getMaxDays() const {
return BEFROE_MONTH_DAYS[month] - BEFROE_MONTH_DAYS[month - 1];
}

bool Date::isLeapYear() const {
return(year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
Date Date::read() {
char c;
int year, month, day;
std::cin >> year >> c >> month >> c >> day;
return Date(year, month, day);
}
void Date::show() const{

void Date::show() const {
std::cout << year << "-" << month << "-" << day;
}
}
17 changes: 11 additions & 6 deletions date.h
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
#ifndef DATE_H
#define DATE_H
class Date {
class Date{
private:
int year;
int month;
int month;
int day;
int totalDays;
public:
Date(int year, int month, int day);
Date(int year=1, int month=1, int day=1);
int getYear() const { return year; }
int getMonth() const { return month; }
int getDay() const { return day; }
bool isLeapYear() const;
int getTotalDays() const{ return totalDays; }
bool isLeapYear() const{ return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }
int getMaxDays() const;
int getTotalDays() const { return totalDays; }
int operator-(const Date& date) const{
int operator-(const Date& date) const {
return totalDays - date.totalDays;
}
static Date read();
bool operator<(const Date& date) const {
return totalDays < date.totalDays;
}
void show() const;
};

#endif
Loading

0 comments on commit 3d791e4

Please sign in to comment.