#include<stdlib.h>
#include<string.h>
#include<conio.h>
#define BUFFERSIZE 1024
#define MAXACCOUNT 1000
typedef struct BankAccount
{
int account
int key
char name[32]
float balance
}BANKACCOUNT
BANKACCOUNT accountCollection[MAXACCOUNT]
int curAccount = 0
void InsertAccount(FILE *fp)
{
BANKACCOUNT newaccount
printf("please input the account information\n")
printf(">>account num:")
scanf("%d",&(newaccount.account))
printf(">>key:"笑敬芦)
scanf("%d",&(newaccount.key))
printf(">>name:")
scanf("%s",newaccount.name)
printf(">>balance:")
scanf("%f",&(newaccount.balance))
fseek(fp,0L,SEEK_END)
fprintf(fp,"%d %d %s %.2f\n",newaccount.account,newaccount.key,newaccount.name,newaccount.balance)
}
void GetAccount(FILE *fp)
{
int accountnum
int key
char name[32]
float balance
int i =0,j
char buffer[BUFFERSIZE]
int len
curAccount = 0
fseek(fp,0,SEEK_SET)
while(!feof(fp)) /* 因为feof()最后会读2遍,所以最后curAccount多加了碰带1 */
{
fscanf(fp,"%d %d %s %f",&accountnum,&key,name,&balance)
accountCollection[curAccount].account = accountnum
accountCollection[curAccount].key = key
strcpy(accountCollection[curAccount].name ,name)
accountCollection[curAccount].balance = balance
curAccount++
}
}
void ListAccount(FILE *fp)
{
int i =0
printf("There is %d accounts at all:\n",curAccount-1)/* curAccount减去多加的1 */
for(i = 0i<curAccount-1i++)
{
printf("ACCOUNT[%d]:\n",i+1)
printf("account:%d\n",accountCollection[i].account)
printf("name:%s\n",accountCollection[i].name)
printf("balance:%.2f\n"稿历,accountCollection[i].balance)
}
}
int SearchAccount(FILE *fp,int accountnum)
{
int i =0
for(i = 0i<curAccount-1i++)
{
if(accountCollection[i].account == accountnum)
{
printf("ACCOUNT[%d]:\n",i+1)
printf("account:%d\n",accountCollection[i].account)
printf("name:%s\n",accountCollection[i].name)
printf("balance:%.2f\n",accountCollection[i].balance)
return 1
}
}
return 0
}
void DelAccount(FILE *fp,int accountnum)
{
int i
if(SearchAccount(fp,accountnum)==0)
printf("Can't find the account\n")
else
{
for(i = 0i<curAccount-1i++)
{
if(accountCollection[i].account != accountnum)
fprintf(fp,"%d %d %s %.2f\n",accountCollection[i].account,accountCollection[i].key,accountCollection[i].name,accountCollection[i].balance)
}
printf("delete successfully!\n")
}
}
int main()
{
FILE *fp
int accountnum
int i
do{
system("cls")//清屏
puts("********************************************")
puts("*You can choose : *")
puts("*1 : Insert a new Account *")
puts("*2 : List all Accounts*")
puts("*3 : Find a Account *")
puts("*4 : Delete a Account *")
puts("*5 : quit *")
puts("********************************************")
printf("Please input your choice:")
scanf("%d",&i)
system("cls")//清屏
switch(i)
{
case 1:
if(!(fp = fopen("account.txt","a+")))
{
printf("can't open the file account.txt\n")
exit(0)
}
InsertAccount( fp)
printf("press any key to continue.....\n")
getch()
fclose(fp)
break
case 2:
if(!(fp = fopen("account.txt","r")))
{
printf("can't open the file account.txt\n")
exit(0)
}
GetAccount(fp)
ListAccount(fp)
fclose(fp)
printf("press any key to continue.....\n")
getch()
break
case 3:
printf("please input the account num:\n")
scanf("%d",&accountnum)
if(!(fp = fopen("account.txt","r")))
{
printf("can't open the file account.txt\n")
exit(0)
}
GetAccount(fp)
if(!SearchAccount(fp,accountnum))
printf("There is not the account:%d\n",accountnum)
fclose(fp)
printf("press any key to continue.....\n")
getch()
break
case 4:
printf("please input the account num:\n")
scanf("%d",&accountnum)
if(!(fp = fopen("account.txt","r")))
{
printf("can't open the file account.txt\n")
exit(0)
}
GetAccount(fp)
fclose(fp)
if(!(fp = fopen("account.txt","w+")))
{
printf("can't open the file account.txt\n")
exit(0)
}
DelAccount(fp,accountnum)
fclose(fp)
printf("press any key to continue.....\n")
getch()
break
default:
break
}
}while(i != 5)
return 0
}
这里账户数据文件名已经设定为account.txt,这个文件要和上面这个程序放在同一个文件夹下面,不然就得用绝对路径(比如"d:\\book\\account.txt"),account内容可以用记事本打开自己改动,然后运行程序后就可以在程序中添加或删除。account.txt一个参考内容如下:
1 10023 zhangsl 100.50
2 32001 sunq 5000.00
3 20010 wangxi 2500.00 4
能传附件吗?不知道怎么传。我就贴给你吧。#include<iostream>
#include<string>
#include<fstream>
using namespace std
class account //atm账户类
{
private:
string CardNum,Psw//卡号、密码
float Balance
public:
friend class ATM//atm为友元
/*void get_cardnum()
void get_psw()
void get_balance()*/
account(){}//<---------------------here
account(string num,string passwd,float balance)//构造函数
void Write()
}
class ATM //atm机类
{
private:
int times
account *ac
public:
ATM()void load( account *act )//装入账户信息
void welcome()//初始欢迎信息
bool check_passwd( string pwd )//验证当前账户信息的密码
void change_psw()//修改密码
void get_money()//取钱塌毁毕
float check_balance()//查账
void tran()//转账
void exit()//退卡
void function_show()//显示功能菜单
void lock()//锁定账户
}
/*
* account::account( string num, string passwd, float balance )
* account类构造函数
* 用途: 使用给定的账号密码等信团芹息初始化一个account对象
* 参数: string num 账号
* string passwd 密码
* float balance
*/
account::account( string num, string passwd, float balance )
{
CardNum = num
Psw = passwd
Balance = balance
}
//atm类构造函数
/*account::get_cardnum()
{
return CardNum
}
account::get_psw()
{
return Psw
}
account::get_balance()
{
return Balance
}*/
void account::Write()
{
ofstream outfile("atm.txt",ios::binary)//<-----------------here
outfile.write((char *)(this),sizeof(account))//<-------------here
outfile.close()
}
ATM::ATM()
{
}
/*
* void ATM::load( account *act )
* ATM类装入账户信息函数
* 用途: 载入指定的account对象,模拟atm插卡过程
* 参数: account *act 要载入的account对象指针
*/
void ATM::load( account *act )
{
ac = act
}
/*
* void ATM::welcome()
* ATM类显示初始欢迎信息函数
* 用途: 显示欢迎信息,提示密码输入并验证
* 参数: 无
*
*/
void ATM::welcome()
{
times = 0//记录密码输入错误次数
cout <<"Welcome to use the China Bank ATM!" <<endl
string pwd//这一个语句应该上移的,一般来说数据的定义和初始化一块写在开头,下面才是各种 *** 余野作的语句
while( times <3 )
{
cout <<"Please enter the password: " <<endl
cin >>pwd
if( !check_passwd( pwd ) )
{
cout <<"The password you entered is wrong, please enter again" <<endl
times++
}
else
{
function_show()
break
}
}
if( times >= 3 )
lock()//输入密码错误次数超过(等于)3次,锁定账户
}
bool ATM::check_passwd( string pwd )
{
if( pwd == ac->Psw )
return true
else
return false
}
void ATM::function_show()
{
int n
cout <<"(1) Change Password" <<endl
cout <<"(2) Get Money" <<endl
cout <<"(3) Check Balance" <<endl
cout <<"(4) Transfer accounts" <<endl
cout <<"(5) Exit" <<endl
cin >>n
while(n != 1 &&n != 2 &&n != 3 &&n != 4 &&n != 5) //这样就可以完全限制用户的输入
{
cout <<"Please enter the right number" <<endl
cin >>n
}
switch( n )
{
case 1:
change_psw()
break
case 2:
get_money()
break
case 3:
cout <<check_balance() <<endl
break
case 4:
tran()
break
case 5:
exit()
break
}
}
void ATM::lock()
{
cout <<"Sorry! Your card has been confiscated!" <<endl
exit()
}
void ATM::change_psw()
{
string old_psw, new_psw1, new_psw2
int t = 0
while( t <3 )
{
cout <<"Please enter the old password: "
cin >>old_psw
if( !check_passwd( old_psw ) )
{
cout <<"The password you enter is wrong, please enter again" <<endl
t++
}
else
{
cout <<"Please enter the new password: "
cin >>new_psw1
cout <<"Please enter the new password again: "
cin >>new_psw2
if( new_psw1 == new_psw2 )
{
ac ->Psw = new_psw2
cout <<"You have change your password successfully!" <<endl
break
}
else
cout <<"Sorry, entered passwords do not match! " <<endl
}
}
//}//<----------------------here
if( t >= 3 )
{
cout <<"Sorry, you have inputed the wrong password for three times and more! " <<endl
}
}
void ATM::get_money()
{
float money
cout <<"Please enter the amount of money you want to get: " <<endl
cin >>money
while( money >ac ->Balance)
{
cout <<"Your balance is not enough, please enter again" <<endl
cin >>money
}
ac ->Balance = ac ->Balance - money
}
float ATM::check_balance()
{
return ac ->Balance
}
void ATM::tran()
{
account a[5]
string cn
float m
cout<<"please enter the cardnum of the account you want to transfer money to"<<endl
cin>>cn
ifstream infile("atm.txt",ios::binary)
infile.seekg(0,ios::beg)
for(int i=0i<5i++)
{
infile.read((char *)(&a[i]),sizeof(a[i]))
}
for(int j=0j<5j++)
{
if(cn==a[i].CardNum)
{
cout<<"please enter the amount of money"<<endl
cin>>m
while(m>ac->Balance)
{
cout<<"there is no enough money in your account,please enter again"<<endl
cin>>m
}
ac->Balance=ac->Balance-m
a[i].Balance=a[i].Balance+m
ofstream outfile("atm.txt",ios::binary)
outfile.seekp(i*sizeof(a[0]),ios::beg)
outfile.write((char *) &a[i],sizeof(a[i]))
}
}
if(j>=5)
{
cout<<"the account doesn't exit"<<endl
}
}
void ATM::exit()
{
cout <<"Please take your card!" <<endl
}
int main()
{
account a[5]={account("10001","1111",5000.0f),account("10002","2222",10000.0f),account("10003","3333",15000.0f),
account("10004","4444",20000.0f),account("10005","5555",25000.0f)}
account temp( "10001", "1111", 5000.0f )
ATM atm
atm.load( &temp )
atm.welcome()
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)