📄 mycc.cpp
字号:
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<iomanip.h>
char* filename(long account,int a);
long CreatNum(int a);
int operate(int a,long account,double money);
class Account
{
public:
Account(long account,double balan,int times);
static Account* pFirst;
void Display(int a);
int write(int a);
// char* getTime();
//int write(int a,int times);
protected:
static int guest;
double balance;
long accountNum;
double domoney;
Account* pNext;
int times;
};
//-------------------------------------------------------------------------------------------------
// account.cpp
/*char*Account::getTime()
{
struct timeb tb;
cout<<"time is ";
ftime(&tb);
char* str=ctime(&tb.time);
cout<<str;
} */
int Account::guest=0;
Account* Account::pFirst=NULL;
Account::Account(long account,double balan,int time)
{
accountNum=account;
balance=balan;
times=time;
//domoney=money;
guest++;
//把新建的对象加入到链表的最后
if(!pFirst)
pFirst=this;
else
{//Account* ps;
for(Account* ps=pFirst;ps->pNext;ps=ps->pNext);
ps->pNext=this;cout<<&ps;
}
pNext=0;
}
int Account::write(int a)
{char cha[100];
char* s=filename(accountNum,a);
strcpy(cha,s); int b;
ofstream ofile(cha);
if(!ofile)
{cerr<<"wrong!can't open the file!";
b=0;
}
else
{char ch[100];
ofile<<accountNum<<" "<<balance<<" "<<times<<endl;
ofile.close();
ofile.open(cha,ios::app);
{if(!ofile)
cerr<<"wrong!";
else
ofile<<setiosflags(ios::left)
<<setw(6)<<times<<setw(20)<<domoney<<setw(20)<<balance<<endl;
b=1;
}
/*while(!file)
ifile.getline(ch,sizeof(ch));
ifile<<times<<" "<<domoney<<" "<<balance<<" "<<endl;
ifile.close();*/
}
return b;
}
void Account::Display(int a)
{
long account;char one[100];
double balan;
int time;
char cha[100];
char* s=filename(accountNum,a);
strcpy(cha,s);
ifstream ifile(cha); //打开记录的文件
/*if(!ifile)
cerr<<"wrong!can't open the file!";
else
{long account;char one[100];
double balan;
int time;
ifile>>account>>balan>>time;
cout<<account<<" "<<balan<<" "<<time<<endl;
while(strcmp(one,"printed")&&(!ifile.eof()))
{ifile>>one;cout<<one;}
while(!ifile.eof())
{
ifile>>one;cout<<one;
}
ofstream ofile(cha,ios::app);
if(!ofile)
cerr<<"wrong 10!";
else
ofile<<"printed";
ofile.close();*/
if(!ifile)
cerr<<"cant open the file \n";
else
{
ifile>>account>>balan>>time;
cout<<endl<<"account:"<<account<<endl<<"your balane is "<<balan<<",this is you "
<<time<<" times come to our bank.";
cout<<endl;
/* while(!ifile.eof())
{ifile>>one;
if(!strcmp(one,"printed"))
break;
//cout<<one;
}*/
cout<<endl<<setiosflags(ios::left)<<setw(6)<<"times"
<<setw(20)<<"domoney"<<setw(20)<<"balance";
while(!ifile.eof())
{char ch;
ifile.get(ch);
cout<<ch;//<<" ";
}
ofstream ofile(cha,ios::app);
if(!ofile)
cerr<<"wrong 10!";
else
ofile<<"printed"<<" ";
ofile.close();
}
}
//------------------------------------------------------------------------
// saveget.h
class SaveGet:public Account
{
public:
SaveGet(long account,double balan,int time);
int save(double money);
int get(double money);
double getBalance();
protected:
};
//-------------------------------------------------------------------------------
SaveGet::SaveGet(long account,double money,int time):Account(account,money,time)
{
}
int SaveGet::save(double money)
{balance+=money; int b;
times++;
domoney=money;
if(write(1))
{//cout<<"write succeed!";
Display(1);
b=1;
}
else
{balance-=money;
times--;
domoney=money;
cout<<"write fallen!";b=0;
}
return b;
}
int SaveGet::get(double money)
{
int b;
if(money>balance)
{cerr<<"you have not enough money in your account!\n";
b=0;
}
else
{
balance-=money;
times++;
domoney=money;
if(write(2))
{//cout<<"\nwrite succeed!";
b=1;
Display(2);
}
else
{balance+=money;
times--;
domoney=money;
cout<<"\nwrite fallen!";b=0;
}
}
return b;
}
double SaveGet::getBalance()
{return balance;
}
//------------------------------------------------------------------------------
// lentrepay.h
class LentRepay:public Account
{
public:
LentRepay(long account,double balan,int time);
int lent(double money);
int repay(double money);
protected:
};
//----------------------------------------------------------------------------------
// lentrepay.cpp
LentRepay::LentRepay(long account,double balan,int time):Account(account,balan,time)
{
}
int LentRepay::lent(double money)
{ int b;
balance+=money;
times++;
domoney=money;
if(write(3))
{//cout<<"\nwrite succeed!";
b=1;
Display(3);
}
else
{
balance-=money; times--;
domoney=0;
cout<<"\n write fallen!"; b=0;
}
return b;
}
int LentRepay::repay(double money)
{int b;
if(money>balance)
{cerr<<"you repay money more than you lent\n";
b=0;
}
else
{
balance-=money;
times++;
domoney=money;
if(write(3))
{//cout<<"\nwrite succeed!";
b=1;
Display(4);
}
else
{
balance+=money; times--;
domoney=0;
cout<<"\nwrite fallen!"; b=0;
}
}
return b;
}
//char* filename(long account,int a);
//long CreatNum(int a);
int operate(int a,long account,double money)
{
int b;
char cha[100];
char*ch=filename(account,a);
strcpy(cha,ch);
ifstream ifile(cha);
if(!ifile)
{cerr<<"\nthe account number isn't exit!\n";
return 0;
}
else
{double balan;
int time;
ifile>>account>>balan>>time;
//cout<<account<<" "<<balan<<" "<<time<<endl;
if(a==1)
{ SaveGet static did(account,balan,time);
b=did.save(money);
}
if(a==2)
{SaveGet static did(account,balan,time);
b=did.get(money);
}
if(a==3)
{LentRepay static did(account,balan,time);
b=did.lent(money);
}
if(a==4)
{LentRepay static did(account,balan,time);
b=did.repay(money);
}
// cout<<"b is:"<<b;
}
return b;
}
//-----------------------------------------------------------------------------------------------
char* filename(long account,int a)
{
char cha[100];
ofstream ofile("test.txt");
if(!ofile)
return NULL;
else
{
switch(a)
{
case 1:ofile<<"saveget"<<"\\"<<"\\"<<account<<".txt"; break;
case 2:ofile<<"saveget"<<"\\"<<"\\"<<account<<".txt"; break;
case 3:ofile<<"repay"<<"\\"<<"\\"<<account<<".txt"; break;
case 4:ofile<<"repay"<<"\\"<<"\\"<<account<<".txt"; break;
}
ofile.close();
ifstream ifile;
ifile.open("test.txt");
if(!ifile)
{
cerr<<"can't open the test.txt";
return NULL;
}
else
ifile.getline(cha,sizeof(cha));//cout<<cha;
}
return cha;//cout<<cha<<"done";
}
//--------------------------------------------------------------------
long CreatNum(int a)
{ long account,accountcp;
if(a!=1&&a!=3)
exit(1);
else
{
char ch[100];
char* str=filename(1,a);
strcpy(ch,str); // cout<<ch<<endl;
ifstream ifile(ch);
if(!ifile)
{//cerr<<"can not open the 1.txt";
ofstream ofile(ch);
if(!ofile)
cerr<<"can't creat the file 1.txt\n";
else
{ofile<<1000000;
ofile.close();
}
}
ifile.close();
ifile.open(ch);
if(!ifile)
cerr<<"wrong";
else
{ifile>>account;
//cout<<endl<<account;
}
ofstream ofile(ch);
if(!ofile)
{ cerr<<"can not open the recent.txt\n";exit(1);
}
else
ofile<<(account+1);
ofile.close();
//
char cha[100];
accountcp=account;
//创建一个帐号的记录文件
char*s=filename(account,a);
strcpy(cha,s);
ofile.open(cha);
double balan=0;
int time=0;
if(!ofile)
cerr<<"wrong!";
else
ofile<<accountcp<<" "<<balan<<" "<<time<<endl;
}
return account;
}
//-----------------------------------------------------------------------------------
void main()
{
cout<<"\nwelcome to use the sofeware!\n";
CHOOSE:cout<<"\n\n1,save money;\n"
<<"2,get money;\n"
<<"3,lent money;\n"
<<"4,repay money;\n"
<<"5,exit;\n";
//<<"6,assist function;\n";
cout<<"your choose is:";
int a;
cin>>a;
if(a<=0||a>5)
{
cout<<"you type wrong!";
goto CHOOSE;
}
if(a==5)
exit(0);
/*if(a==6)
{
cerr<<"1,seach the account;\n"
<<"2,;\n";
} */
cerr<<"\nplease input your account number (type 0 to creat account number):";
long account;
cin>>account;
if(!account)
{ account=CreatNum(a);
cout<<"congratuation! creat account number succeed !\nyour account is:"<<account;
}
switch(a)
{
case 1:cout<<"\nplease input how much money you want to save:"; break;
case 2:cout<<"\nplease input how much money you want to get:"; break;
case 3:cout<<"\nplease input how much money you want to lent:"; break;
case 4:cout<<"\nplease input how much money you want to repay:"; break;
}
double money;
cin>>money;
int judge=operate(a,account,money);
if(!judge)
cerr<<"\noperate fallen!";
else
cerr<<"\noperate succeed!";
goto CHOOSE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -