银行系统

本科大二程序设计实践的作业,算是一个比较简单的项目吧,主要使用的编程范式有面向对象编程

其中引入<multimap><map>头文件实现多映射输出存取记录

引入<fstream>实现文件的读取和写入,进而添加每次打开后能够观察前面的存取记录功能。

中间有很多使用了特例的地方,可以作为简单的参考,但是不能完全解决所有问题

一、测试样例

comman.txt

a s S3755217 0.015
a s 02342342 0.015
a c C5392394 10000 0.0005 50
c 5
d 0 5000 salary
c 15
w 2 2000 buy a cell
c 25
d 1 10000 sell stock 0323
n
d 2 2016 repay the credit
c 5
d 0 5500 salary
n
c 15
w 2 1500 buy a television

输出结果

二、代码部分

date.h

#pragma once
//date.h
#include <iostream>
//using namespace std::rel_ops;
using namespace std;
class Date
{
public:
Date() = default;
Date(int, int, int); //利用构造函数完成初始化
~Date(); //析构
bool operator < (const Date& date) const {
if (year > date.year) {
return 0;
}
else if (year < date.year) {
return 1;
}
else if (year == date.year) {
if (month > date.month) {
return 0;
}
else if (month < date.month) {
return 1;
}
else if (month == date.month) {
if (day > date.day) {
return 0;
}
else if (day < date.day) {
return 1;
}
else
return 0;
} } } //Date(int, int, int);
//int deltadate;
bool Isleapyear(int); //是否是闰年
int DayInYear(int, int, int); //这一天是这一年的那一天
int datebetween(int, int, int, int, int, int); //两个日期之间的距离
void showdate(); static Date read(char*); int getDay();
int getMonth();
int getYear();
int getMaxDay(); int year = 0;
int month = 0;
int day = 0; private: };
#pragma once

accounnt.h

#pragma once
//account.h
#ifndef __ACCOUNT_H__
#define __ACCOUNT_H__
#include "date.h"
#include <iostream>
#include <utility>
#include <map>
using namespace std;
extern double total;
class Accumulator :public Date //accumulator 类
{
public:
Accumulator() = default;
Accumulator(Date, double);
~Accumulator();
double getsum(Date);
void change(Date, double);
Date lastdate;
double sum;
double value;
private: };
class AccountRecord
{
public:
Date date;
// const Account* account;
string id;
double amount;
double balance;
string desc;
//std::multimap
void show();
AccountRecord(Date, string,double,double);
AccountRecord();
~AccountRecord();
private:
};
extern multimap<Date, AccountRecord> recordmap;
class Account :public Accumulator , AccountRecord //基类
{
public: string id; //账号
double balance; //余额
double rate; //利率
double accumulation; //余额之和
static double getTotal(); //总余额
static double total; //总余额函数 static void query(Date,Date) ;
static multimap<Date,AccountRecord> recordmap;
virtual void show(); virtual void deposit(Date, double, string) = 0;
virtual void withdraw(Date, double,string) = 0;
virtual void settle(Date)=0; Account();
Account(Date, const string);
~Account();
void record(Date, double); //record函数,记录当前余额
double accumulate(int deltadate) const {
return accumulation + balance * (deltadate);
}
private:
}; class SavingsAccount :public Account
{
public:
SavingsAccount(); //默认构造函数
SavingsAccount(Date, string, double); //构造函数 日期、id、余额
~SavingsAccount(); //析构函数
//void getId(const char*);
//void getRate(double);
void show(); //显示账户信息
void deposit(Date, double, string); //存款
void withdraw(Date, double,string); //取款
void settle(Date); //结算利息
private:
Accumulator acc;
};
class CreditAccount :public Account
{
public:
CreditAccount();
CreditAccount(Date, string, double, double, double); //构造函数
~CreditAccount(); //析构函数
void deposit(Date, double, string); //存钱
void withdraw(Date, double, string); //取钱
void settle(Date);
void show(); //显示账户信息
private:
//Accumulator acc;
double credit;
double fee;
Accumulator acc;
}; #endif

account.cpp

#include <cmath>
#include <utility>
#include <iostream>
#include <map>
#include <cctype>
#include "account.h"
using namespace std;
//using namespace std::rel_ops;
using std::string;
double Account::total;
multimap<Date, AccountRecord> Account::recordmap; AccountRecord::AccountRecord() { } Account::Account(Date tempdate, string Id) :Accumulator() //构造函数
{ AccountRecord r1(tempdate, Id,amount,balance);
lastdate = tempdate;
id = Id;
accumulation = 0;
balance = 0;
}
Account::~Account()
{
} Accumulator::Accumulator(Date tempdate, double Value) :Date(tempdate)
{
sum = 0;
value = Value;
lastdate = tempdate;
}
Accumulator::~Accumulator()
{
}
void Accumulator::change(Date, double) { }
double Accumulator::getsum(Date tempdate) {
int deltadate = datebetween(lastdate.year, lastdate.month, lastdate.day, tempdate.year, tempdate.month, tempdate.day);
return sum + value * deltadate;
} SavingsAccount::SavingsAccount(Date tempdate, string Id, double Rate) :Account(tempdate, Id) //P290
{
acc.sum = 0;
id = Id;
rate = Rate;
tempdate.showdate();
acc.lastdate = tempdate;
cout << " #" << Id << " created" << endl;
}
SavingsAccount::~SavingsAccount()
{
}/*
void SavingsAccount::getId(const char* p ) {
id = p;
}
void SavingsAccount::getRate(double Rate) {
rate = Rate; }*/
void Account::show() { }
void Account::settle(Date date) { } CreditAccount::CreditAccount(Date tempdate, string Id, double Credit, double Rate, double Fee) :Account(tempdate, Id)
{
acc.sum = 0;
balance = 0;
value = 0;
id = Id;
rate = Rate;
credit = Credit;
fee = Fee;
tempdate.showdate();
acc.lastdate = tempdate;
cout << " #" << Id << " created" << endl;
}
CreditAccount::~CreditAccount()
{
} void SavingsAccount::show() {
cout << id << " Balance: " << balance;
}
//show函数定义
void CreditAccount::show() { //显示账户信息
cout << id << " Balance: " << balance << " Avaliable credit:" << credit + balance; }
//show函数定义 void Account::deposit(Date, double, string) { }//实现虚函数的多态
void Account::withdraw(Date, double, string) { }
//实现虚函数的多态 void SavingsAccount::deposit(Date tempdate, double Amount, string desc) {
acc.value = balance;
acc.sum = acc.getsum(tempdate);
record(tempdate, Amount);
cout << " " <<desc << endl;
acc.lastdate = tempdate; //acc的lastdate成员,更新,用于计算每次的sum total = total + balance;
}
//deposit函数定义
void CreditAccount::deposit(Date tempdate, double Amount, string desc) {
acc.value = balance;
acc.sum=acc.getsum(tempdate);
record(tempdate, Amount);
cout << " " << desc << endl;
acc.lastdate = tempdate; //acc的lastdate成员,更新,用于计算每次的sum total = total + balance;
}
//deposit函数定义 void SavingsAccount::withdraw(Date tempdate, double Amount, string desc) { if (Amount > balance) {
cout << "Error:not enough money" << endl;
}
else {
record(tempdate, -Amount);
cout << " " << desc << endl;
acc.lastdate = tempdate; //acc的lastdate成员,更新,用于计算每次的sum
total = total - Amount;
}
}
//withdraw函数定义
void CreditAccount::withdraw(Date tempdate, double Amount, string desc) { if (Amount > credit) {
cout << "Error:not enough money" << endl;
}
else {
record(tempdate, -Amount); //record函数
cout << " " << desc << endl; sum=acc.getsum(tempdate);
value = balance;
acc.lastdate = tempdate; total = total - Amount;
}
}
//withdraw函数定义 double Account::getTotal() {
return total;
}
//getTotal 函数定义 void SavingsAccount::settle(Date tempdate) {
if (balance == 0) { }
else if (tempdate.year - lastdate.year >= 1) {
acc.value = balance;
acc.sum = acc.getsum(tempdate);
double interest = acc.sum * rate / 366;
interest = floor(interest * 100 + 0.5) / 100; //四舍五入
//int deltadate = datebetween(lastdate.year, lastdate.month, lastdate.day, tempdate.year, tempdate.month, tempdate.day);
//double interest = accumulate(deltadate) * rate / 366;
record(tempdate, interest);
cout << " interest" << endl;
acc.sum = 0;
total = total + interest;
acc.lastdate = tempdate;
}
}
//计算利息函数
void CreditAccount::settle(Date tempdate) { if (tempdate.year - lastdate.year >= 1) {
acc.value = balance;
acc.sum = acc.getsum(tempdate);
double interest = acc.sum * rate;
interest = floor(interest * 100 + 0.5) / 100;
//int deltadate = datebetween(lastdate.year, lastdate.month, lastdate.day, tempdate.year, tempdate.month, tempdate.day);
/*interest = Accumulator(tempdate, rate);*/
record(tempdate, interest);
cout << " interest" << endl;
accumulation = 0;
//total = total + balance;
record(tempdate, -fee);
cout << " annual fee" << endl;
total = total -fee ;
acc.sum = 0;
acc.lastdate = tempdate;
}
else { acc.value = balance;
acc.sum = acc.getsum(tempdate);
double interest = acc.sum * rate;
interest = floor(interest * 100 + 0.5) / 100;
//int deltadate = datebetween(lastdate.year, lastdate.month, lastdate.day, tempdate.year, tempdate.month, tempdate.day);
/*interest = Accumulator(tempdate, rate);*/
record(tempdate, interest);
cout << " interest" << endl;
accumulation = 0;
//total = total + balance;
total = total + interest;
acc.sum = 0;
acc.lastdate = tempdate;
} } void Account::record(Date tempdate, double Amount) { //sum = getsum(tempdate,Amount);
//
//accumulation = accumulate(deltadate);
//Amount = floor(Amount * 100 + 0.5) / 100; //四舍五入取整
balance = balance + Amount;
tempdate.showdate();
cout << " " << "#" << id << " " << Amount << " " << balance;//打印
AccountRecord r1(tempdate,id,Amount,balance); recordmap.insert(make_pair(tempdate, r1)); } void Account::query(Date date1, Date date2) { auto iter = recordmap.lower_bound(date1);
auto end = recordmap.upper_bound(date2);
while (iter != end) {
auto pr = recordmap.equal_range(iter->first);
for (iter; iter != end; ++iter)
iter->second.show();
} }
void AccountRecord::show() {
date.showdate();
cout << " #" << id << " " << amount << " " << balance<<endl;
} AccountRecord::AccountRecord(Date tdate, string Id,double tamount,double tbalance)
{
amount = tamount;
balance = tbalance;
id = Id;
date = tdate;
} AccountRecord::~AccountRecord()
{
}

main.cpp

//step5.cpp
#include "account.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm> using namespace std; struct deleter { template <class T> void operator () (T* p) { delete p; } }; int main() {
Date date(2008, 11, 1);//起始日期
vector<Account*> accounts;//创建账户数组,元素个数为0 fstream icin;
icin.open("commands.txt"); char temp[100] = {0};
char cmd;
do { int cun[10] = {0}; //储存desc说明的位置,然后就可以直接输出
icin.getline(temp,100); //读取文件内的一行 int i = 0;
int j = 0;
char x[100][100] = {0};
for (int t = 0; t < 100; t++) {
cun[i] = t;
x[i][j] = temp[t];
j++;
if (temp[t] == ' ') { i++;
j = 0;
continue; } } //显示日期和总金额
//date.showdate();
//std::cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";
char type;
int index, day;
double amount, credit, rate, fee;
string id,desc;
Account* account;
Date date1, date2; string s = temp; cmd = x[0][0];
switch (cmd) { case 'a'://增加账户
type = x[1][0]; if (type == 's') {
id = x[2];
rate = strtod(x[3],NULL);
//rate = x[3];
//cout << cmd << " " << type << " " << id << " " << rate << endl;
account = new SavingsAccount(date, id, rate); } else {
id = x[2]; credit = strtod(x[3], NULL);
rate = strtod(x[4], NULL);
fee = strtod(x[5], NULL); //cout << cmd << " " << type << " " << id << " " << credit<<" "<<fee<<endl;
account = new CreditAccount(date, id, credit, rate, fee); } accounts.push_back(account); break; case 'd'://存入现金
index = atoi(x[1]);
amount = strtod(x[2], NULL);
desc = s.substr(cun[2],30); //s.substr(m,n) 意思是取字bai符串dus中位置m处开始,zhi长为n的子串dao
//cout << cmd << " " << index << " " << amount << " " << desc << endl;
//getline(cin, desc); accounts[index]->deposit(date, amount, desc); break; case 'w'://取出现金 index = atoi(x[1]);
amount = strtod(x[2], NULL);
//getline(cin, desc);
desc = s.substr(cun[2], 30); //s.substr(m,n) 意思是取字bai符串dus中位置m处开始,zhi长为n的子串dao
//cout << cmd << " " << index << " " << amount << " " << desc << endl;
accounts[index]->withdraw(date, amount, desc); break; case 's'://查询各账户信息 for (size_t i = 0; i < accounts.size(); i++) { cout << "[" << i << "] "; accounts[i]->show(); cout << endl; }
break; case 'c'://改变日期
day = atoi(x[1]);
if (day < date.getDay()) cout << "You cannot specify a previous day"; else if (day > date.getMaxDay()) cout << "Invalid day"; else date = Date(date.getYear(), date.getMonth(), day);
//cout << cmd << " "<<day<< endl;
break; case 'n'://进入下个月
//cout << cmd << endl;
if (date.getMonth() == 12)
date = Date(date.getYear() + 1, 1, 1);
else
date = Date(date.getYear(), date.getMonth() + 1, 1);
for (vector<Account*>::iterator iter = accounts.begin(); iter != accounts.end(); ++iter)
(*iter)->settle(date);
break;
// case 'q'://查询一段时间内的账目 // char p1[200];
// cin >> p1;
// char p2[200];
// cin >> p2;
// date1 = Date::read(p1);
// date2 = Date::read(p2);
// Account::query(date1, date2);
// OutFile << p1 << " " << p2 << endl;
// break; }
} while (cmd != 'e'); std::cout << "(a)add account (d)deposit (w)withdraw (s)show (c)change day (n)next month (q)query (e)exit" << endl;
icin.seekg(0, ios::end);
icin << endl; do {
//显示日期和总金额
date.showdate();
std::cout << "\tTotal: " << Account::getTotal() << "\tcommand> ";
char type;
int index, day;
double amount, credit, rate, fee;
string id, desc;
Account* account;
Date date1, date2;
std::cin >> cmd;
icin << cmd<<" ";
switch (cmd) {
case 'a'://增加账户
std::cin >> type >> id;
if (type == 's') { std::cin >> rate;
icin << 's' << " "<<id<<" "<<rate<<endl;
account = new SavingsAccount(date, id, rate);
}
else {
std::cin >> credit >> rate >> fee;
icin << 'c' << " "<<id<<" "<<credit<<" "<<rate<<" "<<fee<<endl;
account = new CreditAccount(date, id, credit, rate, fee);
}
accounts.push_back(account);
break;
case 'd'://存入现金
std::cin >> index >> amount;
desc = "";
accounts[index]->deposit(date, amount, desc);
icin << index << " " << amount << " " << desc << endl;
break;
case 'w'://取出现金
std::cin >> index >> amount;
desc = "";
accounts[index]->withdraw(date, amount, desc);
icin << index << " " << amount << " " << desc << endl;
break;
case 's'://查询各账户信息
for (size_t i = 0; i < accounts.size(); i++) {
std::cout << "[" << i << "] ";
accounts[i]->show();
std::cout << endl;
}
icin << endl;
break;
case 'c'://改变日期
std::cin >> day;
if (day < date.getDay())
std::cout << "You cannot specify a previous day";
else if (day > date.getMaxDay())
std::cout << "Invalid day";
else
date = Date(date.getYear(), date.getMonth(), day);
icin <<day<< endl;
break;
case 'n'://进入下个月
if (date.getMonth() == 12)
date = Date(date.getYear() + 1, 1, 1);
else
date = Date(date.getYear(), date.getMonth() + 1, 1);
for (vector<Account*>::iterator iter = accounts.begin(); iter != accounts.end(); ++iter)
(*iter)->settle(date);
break;
case 'q'://查询一段时间内的账目
char p1[200];
std::cin >> p1;
char p2[200];
std::cin >> p2;
date1 = Date::read(p1);
date2 = Date::read(p2);
Account::query(date1, date2);
icin << p1<<" " <<p2 << endl;
break;
}
} while (cmd != 'e');
for_each(accounts.begin(), accounts.end(), deleter()); icin.close();
return 0; }

date.cpp

#include <iostream>
#include "date.h" Date::Date(int y, int m, int d) //利用构造函数完成初始化
{
year = y;
month = m;
day = d; }
Date::~Date()
{
}
bool Date::Isleapyear(int year) { //计算是不是闰年
return (year % 4 == 0 || year % 400 == 0) && (year % 100 != 0); }
int Date::DayInYear(int year, int month, int day) //这一年的第几天
{
//int _day = 0;
int DAY[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (Isleapyear(year))
DAY[1] = 29;
for (int i = 0; i < month - 1; ++i)
{
day += DAY[i];
}
return day;
}
int Date::datebetween(int year1, int month1, int day1, int year2, int month2, int day2) { if (year1 == year2 && month1 == month2) {
return day2 - day1;
}
//如果年份月份相同
else if (year1 == year2) {
int d1, d2;
d1 = DayInYear(year1, month1, day1);
d2 = DayInYear(year2, month2, day2);
return d1 > d2 ? d1 - d2 : d2 - d1; }
//如果年份相同
else {
//确保year1年份比year2早
if (year1 > year2)
{
//swap进行两个值的交换
swap(year1, year2);
swap(month1, month2);
swap(day1, day2);
}
int d1, d2, d3;
if (Isleapyear(year1))
d1 = 366 - DayInYear(year1, month1, day1); //取得这个日期在该年还于下多少天
else
d1 = 365 - DayInYear(year1, month1, day1);
d2 = DayInYear(year2, month2, day2); //取得在当年中的第几天 d3 = 0;
for (int year = year1 + 1; year < year2; year++)
{
if (Isleapyear(year))
d3 += 366;
else
d3 += 365;
}
return d1 + d2 + d3; }
//都不相同情况 亲一部分+后一部分 }
void Date::showdate(void) {
cout << year << "-" << month << "-" << day; } int Date::getDay() {
return day;
}
int Date::getMonth() {
return month;
} int Date::getYear() {
return year;
}
int Date::getMaxDay() {
int DAY[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
if (Isleapyear(year))
DAY[1] = 29; int MaxDay = DAY[month-1]; return MaxDay;
} Date Date::read(char*p) {
Date date;
int year=0;
int month=0;
int day=0;
int exp = 1000; for (int i = 0; i < 4; i++) {
int temp = p[i] - 48;
year += temp*exp;
exp = exp / 10;
} exp = 10; for (int i = 5; i < 7;i++) {
int temp = p[i] -48;
month +=temp * exp;
exp = exp / 10;
}
for (int i = 8; i < 9; i++) {
day = p[i]-48;
}
date.year = year;
date.month = month;
date.day = day; return date; }

c++程序设计实践——银行系统的更多相关文章

  1. 程序设计实践C++ 程序代写(QQ 928900200)

    程序设计实践 采用C++作为编程语言. 设计开发一个“学生信息”管理系统.该系统模拟数据库管理系统(DBMS)的功能,为用户提供数据存储.查找的能力. 该系统存储的学生信息包括: 学号.姓名.性别.语 ...

  2. 《程序设计实践》【PDF】下载

    <程序设计实践>[PDF]下载链接: https://u253469.ctfile.com/fs/253469-231196319 内容简介 本书从排错.测试.性能.可移植性.设计.界面. ...

  3. 程设刷题 | 程序设计实践II-2017(部分)

    目录 1165-算术题 题目描述 代码实现 1184-Tourist 1 题目描述 代码实现 1186-Tourist 2 题目描述 代码实现 1224-LOVE 题目描述 代码实现 1256-湘潭大 ...

  4. C程序设计实践教学提示

    实践教学要点:实验重心应放在实验室之外,重在实验准备 对实验题目的分析是一个复杂的工作,很发时间的,如全部放在实验上机时来完成,是不现实的.(特别是后面实验的难度增大,或实验代码增多的情况下),而且, ...

  5. 《Akka应用模式:分布式应用程序设计实践指南》读书笔记9

    性能 这也是一个比较大的问题,因为性能不一定是Akka本身的问题,还可能是你代码写的有问题. 优化的第一步就是找出性能的瓶颈,隔离出应用程序里面比较耗时的部分,然后尝试对其优化,减少需要耗费的时间成本 ...

  6. C++程序设计实践指导1.15找出回文数改写要求实现

    改写要求1:用单链表实现 #include <cstdlib> #include <iostream> using namespace std; struct LinkNode ...

  7. C++程序设计实践指导1.14字符串交叉插入改写要求实现

    改写要求:1:以指针为数据结构开辟存储空间 改写要求2:被插入字符串和插入字符串不等长,设计程序间隔插入 如被插入字符串长度为12,待插入字符串长度为5 则插入间隔为2 改写要求3:添加函数Inser ...

  8. C++程序设计实践指导1.13自然数集中找合数改写要求实现

    改写要求1:用单链表实现 改写要求2:析构函数中依次将链表结点删除 #include <cstdlib> #include <iostream> using namespace ...

  9. C++程序设计实践指导1.12数组中数据线性变换改写要求实现

    改写要求1:分别用指针pa.pb代替数组 改写要求2:从键盘输入data元素 元素个数任意,输入0结束 #include <cstdlib> #include <iostream&g ...

随机推荐

  1. nginx安装步骤和加固方案

    安装步骤参考https://blog.csdn.net/qq_37345604/article/details/90034424 出现以下页面就表示安装完成(默认是80端口,我修改成了8009端口,所 ...

  2. python 3 while嵌套

  3. linux 信号机制

    文章目录 1. 实时信号非实时信号 2. 信号状态: 3. 信号生命周期: 4. 信号的执行和注销 信号掩码和信号处理函数的继承 信号处理函数的继承 信号掩码的继承 sigwait 与多线程 sigw ...

  4. 基于DDD+微服务的开发实战(1)

    1 DDD是什么? DDD是领域驱动设计,是Eric Evans于2003年提出的,离现在有17年. 2 为什么需要DDD 当软件越来越复杂,实际开发中,大量的业务逻辑堆积在一个巨型类中的例子屡见不鲜 ...

  5. Java编程系列文章序言

    Java编程系列分为基础编程和高级编程两部分: 其中基础编程包括基础语法如变量和标识符,流程控制等,数组如一维数组二位数组等,及面向对象,异常处理: 高级部分多线程,常用类,注解,Java集合,泛型, ...

  6. (专题四)06 matlab绘图选项卡

    绘图选项卡 例子1--选择已有变量,绘制图形 都是按照选中的先后顺序依次确定坐标, 如果要修改绘制图形 法一,利用绘图工具和停靠图形按钮 法二,命令行窗口中输入命令 >>plottools ...

  7. 针对于Java的35 个代码性能优化总结

    针对于Java的35 个代码性能优化总结前言代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的 ...

  8. linux下Crontab定时任务

    1.命令格式 crontab [-u user] file crontab [-u user] [-e | -l | -r ] 2.命令参数 -u user:用来设定某个用户的crontab服务: f ...

  9. SpringBoot中的异步编程

    @Async 是什么 void test() { A(); B(); C(); } 复制代码 在没有Async的情况下,上面的方法是顺序执行的,也可以称为同步调用. B要在A执行完毕之后执行,C需要在 ...

  10. IDEA 条件断点 进行debug调试

    1. 鼠标左键在要断点的行号点击一下,打个断点 2.鼠标移动到断点处,然后点击一下鼠标右键,之后会弹出: 3.填写条件,可以使用该行中的代码对应的变量作为条件 4.点击Done按钮 至此条件断点设置完 ...