实例介绍
【实例简介】
【实例截图】
【核心代码】
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#include <stdlib.h>
#include<string.h>
using namespace std;
//用户类定义
class user
{
public:
user();
void set(char *name,char *pass);
char* getusername();
char* getpassword();
float getbalance();
void draw(); //取款
void deposit(); //存款
void deposit(float money); //存款重载
void transfer(); //转帐
void lookbalance(); //余额查询
void changepass(); //修改密码
private:
char username[12]; //用户名
char password[7]; //密码
float balance; //余额
};
void function(); //前向声明功能函数
//主函数
int main()
{
system("color 2e");
function();
}
void inputpass(char *pass) //密码输入以"******"显示
{
int i=0;
char c;
while(isprint( c=getch() ) )
{
pass[i ]=c;
printf("*");
}
pass[i]='\0';
printf("\n");
}
void saveinf(user ¤t,int i) //保存信息
{
ofstream fout("userdata.dat",ios::binary | ios::out |ios::in);
fout.seekp( i*sizeof(current),ios::beg );
fout.write( (char*)¤t,sizeof(current) );
fout.close();
}
void readuser(user &u,int i) //从数据中读出某个位置的数据到一个用户
{
ifstream fin("userdata.dat",ios::binary | ios::in);
fin.seekg(i*sizeof(u),ios::beg);
fin.read( (char*)&u,sizeof(u) );
fin.close();
}
int finduser(user &want,int &place) //查找用户
{
ifstream fin("userdata.dat",ios::binary | ios::in);
if(fin.fail())
{
cout<<"尚无建立用户数据,请先创建用户"<<endl;
return 0;
}
user temp;
while(!fin.eof())
{
fin.read( (char*)&temp,sizeof(temp) );
if(strcmp( want.getusername(),temp.getusername() ))
{
place ; //用户存储位置后移一个数据块
continue;
}
else
{
fin.close();
return 1;
}
}
fin.close();
return 0;
}
void printdraw() //取款打印
{
cout<<"*************************************************************"<<endl;
cout<<"** 欢迎使用吉利银行ATM自动取款机 **"<<endl;
cout<<"** 交易凭条 **"<<endl;
cout<<"** **"<<endl;
cout<<"** 机器号: JILIBANK 交易编号:215454613147412222 **"<<endl;
cout<<"** 用户名: username 交易类型: 取 款 **"<<endl;
cout<<"** 交易额: 1000 元 交易时间:2019/5/5 13:25:00 **"<<endl;
cout<<"** **"<<endl;
cout<<"*************************************************************"<<endl;
}
void printdeposit() //存款打印
{
cout<<"*************************************************************"<<endl;
cout<<"** 欢迎使用吉利银行ATM自动取款机 **"<<endl;
cout<<"** 交易凭条 **"<<endl;
cout<<"** **"<<endl;
cout<<"** 机器号: JILIBANK 交易编号:215454613147412227 **"<<endl;
cout<<"** 用户名: username 交易类型: 存 款 **"<<endl;
cout<<"** 交易额: 1000 元 交易时间:2019/5/5 13:25:00 **"<<endl;
cout<<"** **"<<endl;
cout<<"*************************************************************"<<endl;
}
void printtransfer()
{
cout<<"*************************************************************"<<endl;
cout<<"** 欢迎使用吉利银行ATM自动取款机 **"<<endl;
cout<<"** 交易凭条 **"<<endl;
cout<<"** **"<<endl;
cout<<"** 机器号: JILIBANK 交易编号:215454613147468788 **"<<endl;
cout<<"** 用户名: username 交易类型: 转 帐 **"<<endl;
cout<<"** 手续费: 0.00 元 交易时间:2019/5/5 13:25:00 **"<<endl;
cout<<"** **"<<endl;
cout<<"*************************************************************"<<endl;
}
void printchangepass() //密码修改打印
{
cout<<"*************************************************************"<<endl;
cout<<"** 欢迎使用吉利银行ATM自动取款机 **"<<endl;
cout<<"** 交易凭条 **"<<endl;
cout<<"** **"<<endl;
cout<<"** 机器号: JILIBANK 交易编号:215454613147468788 **"<<endl;
cout<<"** 用户名: username 交易类型: 密码 修改 **"<<endl;
cout<<"** 手续费: 0.00 元 交易时间:2019/5/5 13:25:00 **"<<endl;
cout<<"** **"<<endl;
cout<<"*************************************************************"<<endl;
}
void printlockuser() //锁卡打印
{
cout<<"*************************************************************"<<endl;
cout<<"** 欢迎使用吉利银行ATM自动取款机 **"<<endl;
cout<<"** 交易凭条 **"<<endl;
cout<<"** **"<<endl;
cout<<"** 机器号: JILIBANK 交易编号:215454613147488887 **"<<endl;
cout<<"** 用户名: username 交易类型: 锁 卡 **"<<endl;
cout<<"** 事故原因:密码错误 交易时间:2019/5/5 13:25:00 **"<<endl;
cout<<"** **"<<endl;
cout<<"*************************************************************"<<endl;
cout<<" "<<endl;
cout<<"由于您的密码错误输入次数已过3次,此卡片被锁定"<<endl;
cout<<"请携带此凭条和您的证件到营业厅取卡,再见!"<<endl;
}
//用户类实现
user::user()
{
balance=0;
}
void user::set(char *name,char *pass)
{
strcpy(username,name);
strcpy(password,pass);
}
char* user::getusername()
{
return username;
}
char* user::getpassword()
{
return password;
}
void user::draw() //取款
{
int money;
cout<<"请输入您要取款的金额(元):\n";
cout<<"取款大于等于100小于等于2000元,以50元为基础单位!\n";
cin>>money;
if((money>=100)&&(money<=2000)&&((money%50)==0))
{
while(balance<money)
{
cout<<"对不起,欲取款金额超出您现有余额,请重新输入\n";
cout<<"退出取款,请输入 0 \n";
cin>>money;
if(money==0)
return ;
}
balance-=money;
cout<<"是否需要打印凭条(y/n):";
char f;
cin>>f;
if(f=='y')
printdraw();
}
else
{
cout<<"取款失败,请重新输入!\n";
}
}
void user::deposit() //存款
{
int money=0;
cout<<"请输入您要存款的金额(元):\n";
cout<<"温馨提示!\n" ;
cout<<"存款大于0小于等于1000元,以50元为基础单位!\n";
cin>>money;
balance =money;
if((money>0)&&(money<=1000)&&((money%50)==0))
{
cout<<"存款成功"<<endl;
cout<<"是否需要打印凭条(y/n):";
char f;
cin>>f;
if(f=='y')
printdeposit();
}
else
{
cout<<"存款失败,请重新输入!\n";
}
}
void user::deposit(float money) //存款重载
{
balance =money;
}
void user::transfer() //转帐
{
user trans;
cout<<"请输入转帐卡用户名"<<endl;
char name[12];
cin>>name;
trans.set(name,"");
int location=0;
if(finduser(trans,location) ) // 找到转帐用户
{
float money;
cout<<"请输入您要转帐的金额(元):\n";
cin>>money;
while(balance<money)
{
cout<<"对不起,欲转帐金额超出您现有余额,请重新输入\n";
cout<<"退出转帐,请输入 0 \n";
cin>>money;
if(money==0)
return ;
}
balance-=money;
user temp;
readuser(temp,location);
temp.deposit(money);
saveinf(temp,location);
cout<<"是否需要打印凭条(y/n):";
char f;
cin>>f;
if(f=='y')
printtransfer();
}
else
cout<<"您要转帐的用户不存在"<<endl;
}
void user::lookbalance() //余额查询
{
cout<<"当余前额:"<<balance<<"元"<<endl;
}
void user::changepass() //修改密码
{
cout<<"请输入当前密码"<<endl;
char passorg[7];
inputpass(passorg);
while(strcmp(password,passorg))
{
cout<<"密码错误,请重新输入 "<<endl;
cout<<"暂不修改密码,输入 0"<<endl;
inputpass(passorg);
if(strcmp(passorg,"0")==0)
return ;
}
char pass1[7],pass2[7];
cout<<"请输入新密码"<<endl;
inputpass(pass1);
cout<<"请再次输入新密码"<<endl;
inputpass(pass2);
if(strcmp(pass1,pass2)==0)
{
strcpy(password,pass1);
cout<<"密码修改成功"<<endl;
cout<<"是否需要打印凭条(y/n):";
char f;
cin>>f;
if(f=='y')
printchangepass();
}
else
{
cout<<"两次密码输入不一致"<<endl;
return ;
}
}
void creat() //开户
{
user newuser;
char name[20],pass[7];
cout<<"请创建新的户名(11位以内的字母和数字组合)"<<endl;
cin>>name;
cout<<"请创建用户密码(6 位以内的字母和数字组合)"<<endl;
inputpass(pass);
newuser.set(name,pass); //创建新用户
ofstream fout("userdata.dat",ios::binary | ios::app | ios::in);
fout.write( (char*)&newuser, sizeof(newuser) );
fout.close();
cout<<"\n用户创建成功"<<endl;
system("pause");
}
void login() //登陆
{
char name[20],pass[7];
user current[2];
while(1)
{
cout<<"请输入用户名"<<endl;
cin>>name;
cout<<"请输入密码"<<endl;
inputpass(pass);
current[0].set(name,pass); //用户输入完毕,同时系统判断开始
int nameflag=0; //标记是否查到用户名
int passflag=0; //标记是否密码正确
int location=0; //标记用户存储位置
nameflag=finduser(current[0],location);
if(nameflag) //找到用户,开始验证密码
{
readuser(current[1],location);
if(strcmp( current[0].getpassword(),current[1].getpassword() )==0)
passflag=1; //密码验证通过
else
{
int l=3;
while( strcmp( current[0].getpassword(),current[1].getpassword() ))
{
cout<<"密码错误,请重新输入"<<endl;
l--;
if(l==0)
{
printlockuser();
exit(0);
}
inputpass(pass);
current[0].set(name,pass);
if(strcmp( current[0].getpassword(),current[1].getpassword() )==0)
{
passflag=1; //密码验证通过
break;
}
}
}
}
else
{
cout<<"用户不存在"<<endl;
return ;
}
cout<<" 登陆成功"<<endl;
int t=1;
while(t)
{
cout<<"请输入您的操作选项"<<endl;
cout<<" 取款----1"<<endl;
cout<<" 存款----2"<<endl;
cout<<" 转帐----3"<<endl;
cout<<" 余额查询--4"<<endl;
cout<<" 修改密码--5"<<endl;
cout<<" 退出登陆--0"<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
current[1].draw();
system("pause");
break;
case 2:
current[1].deposit();
system("pause");
break;
case 3:
current[1].transfer();
system("pause");
break;
case 4:
current[1].lookbalance();
system("pause");
break;
case 5:
current[1].changepass();
system("pause");
break;
default: //保存
{
saveinf(current[1],location);
return ;
}
}
}//while(t)
}//while(1)
}
void function()
{
cout<<"||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||"<<endl;
cout<<"||-----------欢迎使用吉利银行ATM自动取款机-----------||"<<endl;
cout<<" "<<endl;
while(1)
{
cout<<"||~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~||"<<endl;
cout<<"|| 本行及银联用户登陆----1 ||"<<endl;
cout<<"|| 新帐户开户--------2 ||"<<endl;
cout<<"|| 退出ATM 机--------0 ||"<<endl;
cout<<" "<<endl;
cout<<" 请输入您的功能操作选项... "<<endl;
int choice;
cin>>choice;
switch(choice)
{
case 1:
login();
break;
case 2:
creat();
break;
default:
cout<<"谢谢您的使用,再见!"<<endl;
return ;
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论