头文件deposit_card.h
#ifndef DEPOSIT_CARD_H
#define DEPOSIT_CARD_H
#include <string>
using namespace std;
class deposit_card
{
public:
deposit_card();
~deposit_card();
void check_record(void)const;
void remain_amt(void)const;
void deposit(float);
void withdrawal(float);
private:
int oprts=0;
float money=0;
string records[32];
float opmoney[32];
static int t;
};
#endif // DEPOSIT_CARD_H
deposit_card.cpp
#include "deposit_card.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
int deposit_card::t = 0;
deposit_card::deposit_card()
{
t++;
}
deposit_card::~deposit_card()
{
t--;
}
void deposit_card::remain_amt()const
{
cout<<money<<endl;
}
void deposit_card::check_record()const
{
int ts = oprts;
for(int i = 0;i < ts;i++){
cout<<records[i];
cout<<" "<<opmoney[i]<<endl;
}
}
void deposit_card::deposit(float g_number)
{
if(g_number >= 0){
money += g_number;
opmoney[oprts] = g_number;
time_t current;
current = time(¤t);
string timestring = ctime(¤t);
timestring = timestring.substr(0,timestring.length()-1);
records[oprts] = timestring;
oprts ++;
}
else cout<<"the value can not be a negative number"<<endl;
}
void deposit_card::withdrawal(float g_number)
{
if(money > g_number){
money -= g_number;
opmoney[oprts] = -g_number;
time_t current;
current = time(¤t);
string timestring = ctime(¤t);
timestring = timestring.substr(0,timestring.length()-1);
records[oprts] = timestring;
oprts ++;
}
else cout<<"the value can not be larger than your savings!"<<endl;
}
主程序
#include <iostream>
#include "deposit_card.h"
#include <string>
#include <ctime>
using namespace std;
int main()
{
cout<<R"(press 1 to create a new account(but now 1 is out of use)
press 2 to deposit & withdrawal
press 3 to check the records
press 4 to check money remain)"<<endl;
int flag1,flag2,f_account,f_number,f;
deposit_card d1,d2,d3;
while(cin >> flag1){
if ( flag1 == 1 ) cout<<"nothing happens and see the code u will know why QAQ"<<endl;
if ( flag1 == 2 )
{
cout<<"key in your account"<<endl;
cin>>f_account;
cout<<"1 for deposit and 2 for withdrawal"<<endl;
cin>>flag2;
if ( flag2 == 1 )
{
cout<<"key in the number you deposit";
cin>>f_number;
if(f_account == 1) d1.deposit(f_number);
if(f_account == 2) d2.deposit(f_number);
if(f_account == 3) d3.deposit(f_number);
}
if( flag2 == 2 )
{
cout<<"key in the number you withdrawal";
cin>>f_number;
if(f_account == 1) d1.withdrawal(f_number);
if(f_account == 2) d2.withdrawal(f_number);
if(f_account == 3) d3.withdrawal(f_number);
}
}
if ( flag1 == 3 )
{
cout<<"key in your account"<<endl;
cin>>f_account;
if(f_account == 1) d1.check_record();
if(f_account == 2) d2.check_record();
if(f_account == 3) d3.check_record();
}
if ( flag1 == 4 )
{
cout<<"key in your account"<<endl;
cin>>f_account;
if(f_account == 1) d1.remain_amt();
if(f_account == 2) d2.remain_amt();
if(f_account == 3) d3.remain_amt();
}
cout<<R"(press 1 to create a new account
press 2 to deposit & withdrawal
press 3 to check the records
press 4 to check money remain)"<<endl;
}
}
由于我太菜了的原因,居然只会用 if(f_account == 1) d1…这种方法来做索引…其实这次的代码和我之前写的用struct 实现理论上没什么区别(只在于电脑提供的指针或我这样人工插眼).
UPD:其实可以直接创建个数组 和struct建立数组的方法是一样的
但是代码的可读性、简洁度是不是上升了很多?(应该是吧)
(前篇:https://blog.csdn.net/weixin_45817880/article/details/104909981)