题目
设计一个Bank类,实现银行某账号的资金往来账管理,包括建账号、存入、取出等。Bank类包括私有数据成员top(当前指针)、date(日期)、money(金额)、rest(余额)和sum(累计余额)。另有一个构造函数和3个成员函数bankin()(处理存入账)、bankout()(处理取出账)和disp( )(出明细账)。
【知识点】:2.2 2.3
【参考分】:25分
【难易度】:B
题目理解:
8月20日
- 怎么实现出入账记录?从题目知,认为使用指针、链表(其实,一点也不懂链表相关内容)可以实现。结果,认为自己做不了
2017-08-20
建账号-->实例化
存入-->bankin()
- 取出-->bankout()
- 构造函数
实现案例:
编写过程的思考:
- 变量定义一个合适的名字真难,比如i_m、o_m ? 啥样子才算规范呢?
ToDo:
- 1
/*
2017年8月19日
*/
#include<iostream>
class Bank
{
public:
Bank() {
money = 0;
rest = 0;
sum = 0;
top = nullptr;
}
~Bank() {
}
//入帐
void bankin(double i_m) {
sum += i_m;
std::cout << " Sum : " << sum << " bank_in:" << i_m << std::endl;
}
//取账
void bankout(double o_m) {
sum -= o_m;
std::cout << " Sum : " << sum << " bank_in:" << o_m << std::endl;
}
//出入明细账
void disp() {
}
private:
int* top; // 当前指针
int date; // 时间
double money, rest, sum;
};
int main() {
Bank b1;
b1.bankin(290);
b1.bankout(21);
//b1.disp();
getchar();
return 0;
}
参考案例:
编程技巧感悟:
8月20日
- 数组的应用,利用top变量作为数组的索引。可以列出出入记录
- Q:数组为什么不初始化?在构造函数中只初始化top变量
知识点点:
- setw()函数:打印空格 setw()函数与setfill()函数
#include <iostream.h>
#include <iomanip.h>
#include <string.h>
const int Max=100;
class Bank
{
int top;
char date[Max][10];//日期
int money[Max];//金额
int rest[Max];//余额
static int sum;//累计余额
public:
Bank(){top=0;}
void bankin(char d[ ],int m)
{
strcpy(date[top],d);
money[top]=m;
sum=sum+m;
rest[top]=sum;
top++;
}
void bankout(char d[],int m)
{
strcpy(date[top],d);
money[top]=-m; //取出数用负数表示
sum=sum-m;
rest[top]=sum;
top++;
}
void disp()
{
int i;
cout<<"日期 存入 取出余额"<<endl;
for(i=0;i<top;i++)
{
cout<<setw(10)<<date[i];
if(money[i]<0)
cout<<""<<setw(6)<<-money[i];
else
cout<<setw(6)<<money[i] <<"";
cout<<setw(6)<<rest[i]<<endl;
};
int Bank::sum=0;
void main()
{
Bank obj;
obj.bankin("2005.2.5".1000);
obj.bankin("2006.3.2",2000);
obj.bankout("2007.4.1",500);
obj.bankout("2007.10.5",800);
obj.disp();
}
是否发布到博客Jack_W, 2017年8月21日 15:16.