* 版权所有 (C)2015,wangweicheng.
*
* 文件名称:main.cpp
* 文件标识:无
* 内容摘要:该代码包含denglu,shurumima函数的定义
* 其它说明:无
* 当前版本: V1.0
* 作 者:王伟诚
* 完成日期: 20150718
*
* 修改记录1:
* 修改日期: 20150718
* 版本号: V1.0
* 修改人:王伟诚
* 修改内容:创建
**********************************************************/
main.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <conio.h>
#include "bank.h"
using namespace std;
//业务员登陆
<div> /*********************************************************
* 功能描述:业务员登陆函数
* 输入参数:无
* 输出参数:无
* 返回值:整型
* 其它说明:输入正确返回1 错误返回0
************************************************************/ </div>
int denglu()
{
ifstream infile("passer.txt",ios::out);
if(!infile)
{
cout<<"文件打开失败"<<endl;
exit(0);
}
char workname[20];
char workmima[20];
char yonghuname[20];
char yonghumima[20];
char sr;
infile>>workname>>workmima;
infile.close();
int a;
for(int i=3; i>0; --i)
{
cout<<"请输入业务员用户名:";
cin>>yonghuname;
cout<<"请输入业务员密码:";
int j=0;
while((sr=getch())!='\r') //getch在接受输入后,不在屏幕上显示
{
if(sr=='\b')
cout<<'\b'<<'\b';//输入退格键时因为退格为一个字符,所以应该进行两次退格,消除退格键的字符和需要消掉的字符
yonghumima[j++]=sr;
putchar('*'); //接受任何字符,屏幕上只显示*
}
yonghumima[j]='\0';
if(strcmp(workname,yonghuname)!=0)
cout<<endl<<"用户名不存在,进入失败,你还有"<<i-1<<"次机会."<<endl;
else if(strcmp(workmima,yonghumima)!=0)
{
cout<<endl<<"密码错误,你还有"<<i-1<<"次机会。"<<endl;
}
else
{
a=1;
break;
}
a=0;
}
return a;
}
//输入密码返回
<div> /*********************************************************
* 功能描述:输入密码并返回
* 输入参数:无
* 输出参数:无
* 返回值:整型,6位数的密码
* 其它说明:输入六位数,在屏幕上显示的是*
************************************************************/ </div>
int shurumima()
{
int sr;
int j=0;
int mima=0;
for(j=0; j<6; j++)
{
sr=getch();
putchar('*');
if(isdigit(sr))
mima=mima*10+(sr-'0');
else
cout<<"密码不能为数字以外的字符,请重新输入"<<endl;
}
return mima;
}
int main()
{
cout<<" "<<endl;
cout<<" 510人民银行欢迎您! "<<endl;
cout<<" "<<endl;
if(denglu())
{Bank b;
b.caozuo();
}
return 0;
}
bank.cpp
* 版权所有 (C)2015,wangweicheng.
*
* 文件名称:bank.cpp
* 文件标识:无
* 内容摘要:该代码为Bank类的成员函数的定义
* 其它说明:无
* 当前版本: V1.0
* 作 者:王伟诚
* 完成日期: 20150718
*
* 修改记录1:
* 修改日期: 20150718
* 版本号: V1.0
* 修改人: 王伟诚
* 修改内容:创建
**********************************************************/
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include "bank.h"
<div> /*********************************************************
* 功能描述:Bank的默认构造函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:构造函数,将文件中的数据保存到数组中
************************************************************/ </div>
Bank::Bank()
{
ifstream infile("xinxi.txt",ios::in);
if(!infile)
{
cerr<<"文件打开失败"<<endl;
exit(1);
}
int i=0;
int zhang; //账号
string nam; //姓名
int mima; //密码
int sfz;//身份证
string zz;//住址
double yue; //金额
int zhuang;
while(infile>>zhang>>nam>>mima>>yue>>sfz>>zz>>zhuang) //当读取成功……
{
users[i].setuser(zhang,nam,mima,yue,sfz,zz,zhuang);//将读取的数据保存到数组中
i++;
}
sn=i;//实际用户数
infile.close();
}
<div> /*********************************************************
* 功能描述:Bank的析构函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:将数组中的信息写入到文件中
************************************************************/ </div>
Bank::~Bank()
{
ofstream outfile("xinxi.txt",ios::out);
if(!outfile)
{
cerr<<"打开失败"<<endl;
exit(1);
}
int i;
//将数组中的数据读入到文件中
for(i=0; i<sn; i++)
{
outfile<<users[i].zhanghao<<" ";
outfile<<users[i].name<<" ";
outfile<<users[i].mima<<" ";
outfile<<users[i].yue<<" ";
outfile<<users[i].shenfenzheng<<" ";
outfile<<users[i].zhuzhi<<" ";
outfile<<users[i].zhuangtai<<" ";
}
outfile.close();
}
//操作
<div> /*********************************************************
* 功能描述:Bank的操作函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/ </div>
void Bank::caozuo()
{
int i;
cout<<endl<<"业务员登陆成功"<<endl;
cout<<" "<<endl;
cout<<"1、开户 2、销户 3、存款 "<<endl;
cout<<"4、取款 5、查询 6、转账 "<<endl;
cout<<"7、挂失 8、解挂 9、改密 "<<endl;
cout<<"0、退出 "<<endl;
cout<<" "<<endl;
cout<<"请输入操作指令:";
while(cin>>i)
{
//i是0到9的数字
if(i<0||i>9)
cout<<"输入不正确,请重新输入:";
if(i==0) break;
if(i==1){kaihu();
break;}
if(i==2){xiaohu();
break;}
if(i==3) {cunkuan();
break;}
if(i==4){ qukuan();
break;}
if(i==5) {chaxun();
break;}
if(i==6) {zhuanzhang();
break;}
if(i==7) {guashi();
break;}
if(i==8) {jiegua();
break;}
if(i==9) {gaimi();
break;}
}
}
<div> /*********************************************************
* 功能描述:Bank的开户函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/ </div>
void Bank::kaihu()
{
cout<<"正在开户"<<endl;
if(sn==0)
cout<<"您好,您的账号是:"<<100<<endl;
else cout<<"您好,您的账号是:"<<users[sn-1].zhanghao+1<<endl;//开户的账号为开户前最后一个账号加一
cout<<"请输入户主姓名:";
string nam;
cin>>nam;//输入姓名
int sfz;//身份证
string zhz; //家庭住址
cout<<"请输入身份证后六位:";
cin>>sfz;
cout<<"请输入家庭住址:";
cin>>zhz;
int password0;
int password1;
cout<<"请设置密码:";
password0=shurumima();
cout<<endl<<"请确认密码:";
password1=shurumima();
if(password0==password1)//校验密码
{
cout<<endl;
cout<<"开户成功."<<endl;
if(sn==0) users[sn].zhanghao=100;
else users[sn].zhanghao=users[sn-1].zhanghao+1;
users[sn].mima=password0;
users[sn].name=nam;
users[sn].shenfenzheng=sfz;
users[sn].zhuangtai=0;
users[sn].zhuzhi=zhz;
sn=sn+1;
cout<<"请选择其他服务:"<<endl;
caozuo();
}
else
{
cout<<endl<<"两次密码不一致,开户失败。";
cout<<"请选择其他服务:"<<endl;
caozuo();
}
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的销户函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
<pre name="code" class="cpp">void Bank::xiaohu()
{
int us;
us=getuser();
if(us>=0)
{
users[us].showname();
if(users[us].passwordisright())
{
users[us].showyue("余额");
cout<<"确认销户? 确认输入y,取消输入n";
char a;
cin>>a;
if(a=='y')
{
users[us].showyue("销户成功!本次取款金额为");
users[us].yue=0;
users[us].zhuangtai=2;//状态改为销户状态
yewu(users[us].zhanghao,"销户,余额",0);
}
else
{
cout<<"你取消了操作,销户失败!"<<endl;
}
}
}
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的存款函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::cunkuan()
{
int us;
double money;
us = getuser();
if(us>=0)
{
if(users[us].zhuangtai==0)
{
users[us].showname();
cout<<"输入存款额:";
cin>>money;
users[us].yue+=money;
users[us].showyue("存款后,您有");
yewu(users[us].zhanghao,"操作:存款,存入",money);//业务明细
}
else if(users[us].zhuangtai==1)
{
cout<<"该用户处于挂失状态,存款失败!"<<endl;
}
else
{
cout<<"该用户已经销户,存款失败!"<<endl;
}
}
cout<<"请选择其他操作";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的取款函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::qukuan()
{
int us; //查询信息
double money;
us = getuser();
if(us>=0)
{
if(users[us].isnormaluser())
{
users[us].showname();
if(users[us].passwordisright())
{
cout<<"输入取款额:";
cin>>money;//要取的金额
if(money>users[us].yue)
{
cout<<"余额不足,取款失败!"<<endl;
}
else
{
users[us].yue=users[us].yue-money;
users[us].showyue("取款成功,您还有");
}
}
}
yewu(users[us].zhanghao,"取款,取出:",money);
}
cout<<"请选择其他操作";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的查询函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::chaxun()
{
int us;
string zhu[3]= {"正常","挂失","已经销户"};
us = getuser();//返回用户在数组中的下标
if(us>=0)
{
users[us].showname();
if(users[us].passwordisright())
{
cout<<"户主姓名:";
users[us].showname();
cout<<endl<<"户主身份证号后六位:"<<users[us].shenfenzheng<<endl;
cout<<"户主住址:"<<users[us].zhuzhi<<endl;
users[us].showyue("余额");
cout<<"状态:"<<zhu[users[us].zhuangtai]<<endl;
}
}
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的状态函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::zhuanzhang()
{
int uschu, usjin;
double money;
cout<<"转出自";
uschu=getuser();
if(uschu>=0)
{
if(users[uschu].isnormaluser())
{
users[uschu].showname();
if(users[uschu].passwordisright())
{
cout<<"输入转账金额:";
cin>>money;
if(money>users[uschu].yue)
{
cout<<"余额不足,转账失败!"<<endl;
}
else
{
cout<<"转出到";
usjin = getuser();
if(usjin>=0)
{
if(users[usjin].isnormaluser())//验证通过
{
users[uschu].yue-=money;
users[usjin].yue+=money;
users[uschu].showyue("转账后,您还有");
}
}
}
}
}
yewu(users[uschu].zhanghao,"转账,转出",money);//业务明细
yewu(users[usjin].zhanghao,"转账,转入",money);
}
cout<<"请选择其他服务";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的挂失函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::guashi()
{
int us;
us = getuser();
if(us>=0)
{
users[us].showname();
if(users[us].passwordisright())
{
if(users[us].zhuangtai==0)
{
users[us].zhuangtai=1;
cout<<"挂失成功"<<endl;
}
else if(users[us].zhuangtai==1)
{
cout<<"该账户已经处于挂失状态"<<endl;
}
else
{
cout<<"该账户已销户,不能挂失"<<endl;
}
}
yewu(users[us].zhanghao,"挂失,余额为:",users[us].yue);//业务明细
}
cout<<"请选择其他服务";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的解挂函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::jiegua()
{
int us;
us = getuser();
if(us>=0)
{
users[us].showname();
if(users[us].passwordisright())
{
if(users[us].zhuangtai==0)
{
cout<<"该账户处于正常状态,不需要解除挂失"<<endl;
}
else if(users[us].zhuangtai==1)
{
users[us].zhuangtai=0;
cout<<"解除挂失成功"<<endl;
}
else
{
cout<<"该账户已销户,操作无效"<<endl;
}
}
yewu(users[us].zhanghao,"解挂,余额为:",users[us].yue);
}
cout<<"请选择其他服务";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的改密函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/
void Bank::gaimi()
{
int us; //业务明细
int mima1, mima2;
us = getuser();
if(us>=0)
{
users[us].showname();//显示姓名
if(users[us].passwordisright())
{
cout<<"新密码:";
mima1=shurumima();
cout<<"确认密码:";
mima2=shurumima();
if(mima1==mima2)
{
users[us].mima=mima1;
cout<<"修改成功!"<<endl;
}
else
{
cout<<"两次输入不同,修改失败!"<<endl;
}
}
yewu(users[us].zhanghao,"改密,新密码为:",mima1);
}
cout<<"请选择其他服务";
caozuo();
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的业务明细函数
* 输入参数:账号,提示词,金额
* 输出参数:无
* 返回值:无
* 其它说明:保存用户的每笔操作
************************************************************/
void Bank::yewu(int z,string a,double b)
{ //当前时间
time_t curtime=time(0);
tm tim=*localtime(&curtime);
int day,mon,year,hour,minute,second;
day=tim.tm_mday;
mon=tim.tm_mon;
year=tim.tm_year;
hour=tim.tm_hour;
minute=tim.tm_min;
second=tim.tm_sec;
ofstream outfile("yewu.txt",ios::out|ios::app);//保存到文件的后面
outfile<<year+1900<<"年"<<mon+1<<"月"<<day<<"日"<<hour<<"时"<<minute<<"分"<<second<<"秒"<<"账号"<<z<<a<<b<<endl;
}
<pre name="code" class="cpp"> /*********************************************************
* 功能描述:Bank的查找用户函数
* 输入参数:无
* 输出参数:无
* 返回值:整型,用户的数组下标
* 其它说明:无
************************************************************/
int Bank::getuser()//二分查找
{
int zh;
cout<<"账号:";
cin>>zh;
int index=-1;
int low=0, high=sn-1, mid;
while(low<=high)
{
mid = (low+high)/2;
if(users[mid].zhanghao==zh)
{
index=mid;
break;
}
else if (users[mid].zhanghao>zh) high=mid-1;
else low=mid+1;
}
if (index<0) cout<<"该用户不存在,本次操作失败!"<<endl;
return index;
}
User.cpp
/*********************************************************** * 版权所有 (C)2015,wangweicheng. * * 文件名称:User.cpp * 文件标识:无 * 内容摘要:该代码包含User类成员函数的定义* 其它说明:无 * 当前版本: V1.0 * 作 者:王伟诚* 完成日期: 20150718* * 修改记录1: * 修改日期: 20150718* 版本号: V1.0 * 修改人:王伟诚* 修改内容:创建 **********************************************************/
#include <iostream>
#include <fstream>
#include <string>
#include "bank.h"
using namespace std;
<div> /*********************************************************
* 功能描述:User类的检验密码函数
* 输入参数:无
* 输出参数:无
* 返回值:bool型
* 其它说明:无
************************************************************/ </div>
bool User::passwordisright()
{
int pass;
bool right=true;
cout<<"输入密码:";
pass=shurumima();
if(pass!=mima)
{
right =false;
cout<<"输入密码错误,不能继续操作!"<<endl;
}
return right;
}
<pre name="code" class="cpp"><div> /*********************************************************
* 功能描述:User类的赋值函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/ </div>
void User::setuser(int zhang, string nam, int mi, double yu,int shen,string zhu,int zhuang)
{ zhanghao=zhang; name=nam; mima=mi; yue=yu; shenfenzheng=shen; zhuzhi=zhu; zhuangtai=zhuang;}
<div> /*********************************************************
* 功能描述:User的显示姓名函数
* 输入参数:无
* 输出参数:无
* 返回值:无
* 其它说明:无
************************************************************/ </div>
void User::showname()
{
cout<<name;
}
void User::showyue(string prompt)
{
cout<<prompt<<":"<<yue<<"元"<<endl;
}
<div> /*********************************************************
* 功能描述:User的判断用户状态函数
* 输入参数:无
* 输出参数:无
* 返回值:bool型
* 其它说明:无
************************************************************/ </div>
bool User::isnormaluser()
{
bool normal = true;
if(zhuangtai!=0)
{
normal = false;
cout<<"该账户处于"<<(zhuangtai==1?"挂失":"销户")<<"状态,不能继续操作..."<<endl;
}
return normal;
}
bank.h
/***********************************************************
* 版权所有 (C)2015,wangweicheng.
*
* 文件名称:bank.h.cpp
* 文件标识:无
* 内容摘要:该代码包含Bank类,User类成员函数的声明
* 其它说明:无
* 当前版本: V1.0
* 作 者:王伟诚
* 完成日期: 20150718
*
* 修改记录1:
* 修改日期: 20150718
* 版本号: V1.0
* 修改人:王伟诚
* 修改内容:创建
**********************************************************/
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <string>
using namespace std;
const int n=2000;
class Bank;
class User
{
public:
void setuser(int zhang, string nam, int mi, double yu,int shen,string zhu,int zhuang);
void showname();
void showyue(string prompt); //显示余额,前面加上提示词prompt
bool passwordisright(); //校验密码,输入的密码正确则返回true
bool isnormaluser(); //存款、取款、转帐等功能,需要账户处于正常姿态,处于正常状态时返回true,其他情形返回false并提示
friend class Bank; //将Bank声明为友元类,方便其访问数据成员
private:
int zhanghao; //账号
int mima; //密码
string name; //用户名
double yue; //账户余额
int shenfenzheng;//身份证
string zhuzhi;//住址
int zhuangtai; //状态 0-正常 1-挂失 2-销户
};
class Bank
{
public:
Bank(); //开始前从文件中读数据,存在数组中
~Bank(); //程序结束前,将数组中的数据写入到文件中
void caozuo(); //业务驱动
void kaihu(); //开户
void xiaohu(); //注销账户
void cunkuan(); //存款
void qukuan(); //取款
void chaxun(); //查询余额
void zhuanzhang(); //转账
void guashi(); //挂失
void jiegua(); //解除挂失
void gaimi(); //更改密码
void yewu(int z,string a,double b);
int getuser(); //输入账号查询用户,返回用户在对象数组中的下标
private:
int sn; //实际的用户数目
User users[n];
};
int denglu(); //业务员登录
int shurumima(); //返回键盘输入的密码
#endif // BANK_H_INCLUDED
* 版权所有 (C)2015,wangweicheng.
*
* 文件名称:main.cpp
* 文件标识:无
* 内容摘要:该代码包含denglu,shurumima函数的定义
* 其它说明:无
* 当前版本: V1.0
* 作 者:王伟诚
* 完成日期: 20150718
*
* 修改记录1:
* 修改日期: 20150718
* 版本号: V1.0
* 修改人:王伟诚
* 修改内容:创建
**********************************************************/