純手工打造 難爲情的是很多借鑑了賀老的參考解答 希望能在擴展中做出自己的風格
代碼如下
main.cpp
#include <iostream>
#include "Bank.h"
using namespace std;
int main()
{
cout<<"+---------------------------------------+"<<endl;
cout<<"+\t歡迎使用neemaby銀行系統 \t+"<<endl;
cout<<"+---------------------------------------+"<<endl<<endl;
Bank b;
if (pass())
{
Bank b;
b.work();
}
return 0;
}
Bank.h
#ifndef BANK_H_INCLUDED
#define BANK_H_INCLUDED
#include <cstring>
#include <iostream>
using namespace std;
const int upNum=2000;//本系統暫設最多支持2000個賬戶
int choosemenu();
int pass();//通過業務員賬號
int inputcode();//使密碼以*號顯示
class user
{
public:
void set_user(int acc, string nam, int pw, double bal,int sta);
void show_name();//顯示用戶信息
void show_balance();//顯示餘額
bool passwordIsRight();//判斷密碼和賬戶是否相符
bool isNormalUser();
friend class Bank;
private:
int ACCOUNT;//用戶賬戶
string NAME;//用戶的名字
int PASSWORD;//用戶密碼
double BALANCE;//用戶餘額
int STATUS;//用戶狀態
};
class Bank
{
public:
Bank();//開機時從文件中讀數據,存在數組中
~Bank();//關機前將數據寫入到文件中
void work();
void openAccount();//開戶
void cancelAccount();//註銷賬戶
void save();//存款
void withdraw();//取款
void showAccount();//查詢賬戶
void transferAccounts();//轉賬
void reportLoss();//掛失
void cancelLoss();//解掛
void updatePassword();//更改密碼
int getUser();//輸入賬號查詢用戶,返回用戶在對象數組中的下標
private:
int N;//實際用戶數目
user users[upNum];
};
#endif // BANK_H_INCLUDED
Bank.cpp
#include "Bank.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
Bank::Bank()//開機時從文件中讀數據,存在數組中
{
ifstream infile("account.dat",ios::in);
if (!infile)
{
cerr<<"open account.dat error!"<<endl;
exit(1);
}
int i=0;
int acc;//賬號
string nam;
int pw;
double bal;//金額
int sta;//狀態
while (infile>>acc>>nam>>pw>>bal>>sta) //讀取成功時
{
users[i].set_user(acc,nam,pw,bal,sta);
i++;
}
N=i;//用靜態數據成員儲存
infile.close();
}
Bank::~Bank()//關機前將數據寫入到文件中
{
ofstream outfile("account.dat",ios::out);
if (!outfile)
{
cerr<<"open account.dat error!"<<endl;
exit(1);
}
int i=1;
for (i=0; i<N; i++)
{
outfile<<users[i].ACCOUNT<<" ";
outfile<<users[i].NAME<<" ";
outfile<<users[i].PASSWORD<<" ";
outfile<<users[i].BALANCE<<" ";
outfile<<users[i].STATUS<<" ";
}
outfile.close();
}
void Bank::work()
{
int iChoice;//選擇
do
{
iChoice = choosemenu();
switch (iChoice)
{
case 1:
openAccount(); //開戶
break;
case 2:
cancelAccount(); //註銷賬戶
break;
case 3:
save(); //存款
break;
case 4:
withdraw(); //取款
break;
case 5:
showAccount(); //查詢餘額
break;
case 6:
transferAccounts(); //轉賬
break;
case 7:
reportLoss(); //掛失
break;
case 8:
cancelLoss(); //解除掛失
break;
case 9:
updatePassword(); //更改密碼
break;
case 0:
cout<<"歡迎您再來. "<<endl;
}
}
while(iChoice);
}
void Bank::openAccount()//開戶
{
if(N==upNum)
{
cout<<"用戶數已經達到上限,無法再開戶";
return;
}
int acc; //賬號
string nam; //姓名
int pw1,pw2; //密碼
double bal; //金額
int sta; //狀態
cout<<"開戶中…"<<endl;
acc=10001+N;
cout<<"賬號:"<<acc<<endl;
cout<<"戶主姓名:";
cin>>nam;
cout<<"密碼:";
pw1=inputcode(); //輸入密碼1
cout<<"確認密碼:";
pw2=inputcode(); //輸入密碼2
if(pw1==pw2)
{
sta=0; //狀態“正常”
cout<<"存款金額:";
cin>>bal;
users[N].set_user(acc, nam, pw1, bal, sta);
N++; //正式用戶數增加1
cout<<"開戶成功!"<<endl;
}
else
{
cout<<"兩次密碼不一致,未成功開戶!"<<endl; //沒有N++,則讀入的值無效
}
}
void Bank::cancelAccount()//註銷賬戶
{
int getU;
getU = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(getU>=0) //說明id賬戶存在
{
users[getU].show_name();
if(users[getU].passwordIsRight())
{
cout<<"餘額:";
users[getU].show_balance(); //顯示金額
cout<<"是否確認銷戶(y/n)?";
if(tolower(getchar())=='y')
{
cout<<"銷戶成功!您的餘額";
users[getU].show_balance();
cout<<"將被全部取出!"<<endl;
users[getU].BALANCE=0; //取款後餘額變0
users[getU].STATUS=2; //狀態變爲註銷
}
else
{
cout<<"你取消了操作,銷戶失敗!"<<endl;
}
fflush(stdin); //清除了getchar()時在鍵盤緩存中的遺留,以免影響後續操作
}
}
}
void Bank::save()//存款
{
int getU;
double money;
getU = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(getU >=0) //說明id賬戶存在
{
if(users[getU].STATUS==0)
{
users[getU].show_name();
cout<<"輸入存款額:";
cin>>money;
users[getU].BALANCE+=money;
cout<<"存款後,您的賬戶將有金額:";
users[getU].show_balance();
}
else if(users[getU].STATUS==1)
{
cout<<"該用戶處於掛失狀態,存款失敗!"<<endl;
}
else
{
cout<<"該用戶已經銷戶,存款失敗!"<<endl;
}
}
return;
}
void Bank::withdraw()//取款
{
int getU ;
double money;
getU = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(getU >=0) //說明id賬戶存在
{
if(users[getU ].isNormalUser())
{
users[getU ].show_name();
if(users[getU ].passwordIsRight())
{
cout<<"輸入取款額:";
cin>>money;
if(money>users[getU ].BALANCE) //親,不玩透支
{
cout<<"餘額不足,取款失敗!"<<endl;
}
else
{
users[getU ].BALANCE-=money;
cout<<"取款後,餘額爲:";
users[getU ].show_balance();
}
}
}
}
return;
}
void Bank::showAccount()//查詢賬戶
{
int getU ;
string sta[3]= {"正常","掛失","已經銷戶"};
getU = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(getU >=0) //說明id賬戶存在
{
users[getU ].show_name();
if(users[getU ].passwordIsRight())
{
cout<<"賬戶餘額爲:";
users[getU ].show_balance();
cout<<"狀態:"<<sta[users[getU ].STATUS]<<endl;
}
}
return;
}
void Bank::transferAccounts()//轉賬
{
int Win,Wout;
double money;
cout<<"轉賬自";
Wout= getUser();
if(Wout>=0)
{
if(users[Wout].isNormalUser())
{
users[Wout].show_name();
if(users[Wout].passwordIsRight())
{
cout<<"輸入轉賬金額:";
cin>>money;
if(money>users[Wout].BALANCE) //防止透支
{
cout<<"餘額不足,轉賬失敗!"<<endl;
}
else
{
cout<<"轉出到";
Win = getUser();
if(Win>=0)
{
if(users[Win].isNormalUser())
{
users[Wout].BALANCE-=money;
users[Win].BALANCE+=money;
cout<<"轉賬後,您的餘額爲:";
users[Wout].show_balance();
}
}
}
}
}
}
}
void Bank::reportLoss()//掛失
{
int getU;
getU = getUser(); //根據賬號查詢用戶,返回用戶的下標
if(getU >=0) //說明id賬戶存在
{
users[getU ].show_name();
if(users[getU ].passwordIsRight())
{
if(users[getU ].STATUS==0)
{
users[getU ].STATUS=1;
cout<<"掛失成功"<<endl;
}
else if(users[getU ].STATUS==1)
{
cout<<"該賬戶已經處於掛失狀態"<<endl;
}
else
{
cout<<"該賬戶已銷戶,不能掛失"<<endl;
}
}
}
return;
}
void Bank::cancelLoss()//解掛
{
int getU;
getU=getUser();
if (getU>=0)
{
users[getU].show_name();
if(users[getU ].passwordIsRight())
{
if(users[getU ].STATUS==1)
{
users[getU ].STATUS=0;
cout<<"解掛成功"<<endl;
}
else if(users[getU ].STATUS==0)
{
cout<<"該賬戶無需解掛"<<endl;
}
else
{
cout<<"該賬戶已銷戶,不能掛失"<<endl;
}
}
}
return;
}
void Bank::updatePassword()//更改密碼
{
int getU,pw1,pw2;
getU=getUser();
if (getU>=0)
{
users[getU].show_name();
if(users[getU ].passwordIsRight())
{
cout<<"請輸入新密碼:";
pw1=inputcode();
cout<<"請重複密碼:";
pw2=inputcode();
if (pw1==pw2)
{
users[getU].PASSWORD=pw1;
cout<<"密碼修改成功!"<<endl;
}
else
{
cout<<"兩次密碼不一致,未成功開戶!"<<endl;
}
}
}
return;
}
int Bank::getUser()//輸入賬號查詢用戶,返回用戶在對象數組中的下標
{
//使用二分查找法查找賬戶
int id,mid;
int ind=-1,high=N-1,low=0;
cout<<"賬號:";
cin>>id;
while(low<=high)
{
mid=(low+high)/2;
if (users[mid].ACCOUNT==id)
{
ind=mid;
break;
}
else if (users[mid].ACCOUNT>id)
high=mid-1;
else
low=mid+1;
}
if(ind<0)
cout<<"該用戶不存在,本次操作失敗!"<<endl;
return ind; //若找到,其值在0~N-1間,否則,保持-1
}
user.cpp
#include "Bank.h"
#include <iostream>
#include <cstring>
#include <fstream>
#include <cstdlib>
using namespace std;
void user::set_user(int acc,string nam,int pw,double bal,int sta)
{
ACCOUNT=acc;
NAME=nam;
PASSWORD=pw;
BALANCE=bal;
}
void user::show_name()
{
cout<<"姓名:"<<NAME<<endl;
}
void user::show_balance()//顯示餘額
{
cout<<BALANCE<<" 元"<<endl;
}
bool user::passwordIsRight()//校驗密碼,正確返回true
{
int pw;
bool right=true;
cout<<"輸入密碼:";
pw=inputcode();
if (pw!=PASSWORD)
{
right=false;
cout<<"密碼錯誤,無法繼續操作!"<<endl;
}
return right;
}
bool user::isNormalUser()//判斷是否處於“正常”,是則返回true,否則返回false
{
bool normal=true;
if(STATUS!=0)
{
normal=false;
cout<<"該賬戶處於"<<(STATUS==1?"掛失":"銷戶")<<"狀態,不能繼續操作!"<<endl;
}
return normal;
}
work.cpp
#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <conio.h> //getch要用
#include <cstring>
#include "Bank.h"
using namespace std;
int choosemenu()
{
int i;
while(1)
{
cout<<endl;;
cout<<"+--------------------------------------+"<<endl;
cout<<"+ 1 開戶\t2 銷戶\t\t3 存款 +"<<endl;
cout<<"+ 4 取款\t5 查詢\t\t6 轉賬 +"<<endl;
cout<<"+ 7 掛失\t8 解掛\t\t9 改密 +"<<endl;
cout<<"+ 0 退出\t\t\t +"<<endl;
cout<<"+--------------------------------------+"<<endl;
cout<<"請輸入操作指令:";
cin>>i;
if(i>=0 && i<=9)
break;
else
cout<<"請重新選擇功能"<<endl;;
}
return i;
}
int pass()
{
char sNameInFile[20]; //由文件中讀出的業務員用戶名
char sPassInFile[20]; //文件中保存的密碼,這一版本中,用字符保存密碼
ifstream infile("password.txt",ios::in);
if(!infile)
{
cout<<"password file cannot open!"<<endl;
exit(1);
}
infile>>sNameInFile>>sPassInFile;
infile.close();
char sName[20]; //業務員登錄時輸入的用戶名
char sPass[20]; //業務員登錄時輸入的密碼
char ch;
int iTry=3; //進入系統時嘗試的次數
int right = 0; //要返回的結果:0-不正確 1-正確
//進入系統,密碼三次不對將退出
do
{
cout<<"請輸入業務員用戶名:";
cin>>sName;
cout<<"請輸入密碼:";
int i=0;
while((ch=getch())!='\r') //getch在接受輸入後,不在屏幕上顯示
{
sPass[i++]=ch;
putchar('*'); //接受任何字符,屏幕上只顯示*
}
sPass[i]='\0';
fflush(stdin);
cout<<endl;;
if(strcmp(sPass,sPassInFile)==0&&strcmp(sName,sNameInFile)==0)
{
right = 1;
break;
}
else
{
iTry--;
if(iTry>0)
cout<<"超過3次將退出,你還可以嘗試"<<iTry<<"次!"<<endl;
else
{
cout<<"對不起,你不能進入系統"<<endl;;
}
}
}
while(iTry);
return right;
}
int inputcode()//使密碼以*號顯示
{
char ch;
int iPass=0;//用於將輸入的字符型數字轉換爲數字型數字
while(1)
{
for(i=0; i<6; i++)
{
ch=getch();
putchar('*'); //輸入後不顯示數字而輸出*
if(isdigit(ch)) //!!isdigit(ch)的使用
iPass=iPass*10+(ch-'0');
else
{
iPass=0;
break; //退出for循環後,再次接受
}
}
fflush(stdin); //!!清除鍵盤緩存區中已經有的輸入,fflush(stdin)的使用
cout<<endl;;
if(iPass==0) //此條件成立可能由兩種情況引起:輸入了非數字字符被直接重置爲0,或6位全0後正常退出for循環
{
cout<<"密碼要求全爲數字,且不能全0!"<<endl;;
cout<<"請重新輸入密碼: ";
}
else
break;
}
return iPass;
}
學習心得
work.cpp中:
1.fflush(stdin)
fflush用於清空緩衝流,雖然一般感覺不到,但是默認printf是緩衝輸出的。 fflush(stdout),使stdout清空,就會立刻輸出所有在緩衝區的內容。 fflush(stdout)這個例子可能不太明顯,但對stdin很明顯。 如下語句: int a,c; scanf("%d",&a); getchar(); 輸入: 12(回車) 那麼 a=12 ,c= '\n' 而: int a,c; scanf("%d",&a); fflush(stdin); getchar(); 輸入: 12(回車) 那麼a=12, c暫時未得到輸入值,還需要再輸入c,因爲getchar也是緩衝輸入,'\n'本還在緩衝區,但是被清空了。 另外fflush不能作用於重定向輸入流。
2.getch()
這個函數是一個不回顯函數,當用戶按下某個字符時,函數自動讀取,無需按回車,有的C語言命令行程序會用到此函數做遊戲,但是這個函數並非標準函數,要注意移植性!
getch():
所在頭文件:conio.h
函數用途:從控制檯讀取一個字符,但不顯示在屏幕上
函數原型:int getch(void)
返回值:讀取的字符
例如:
char ch;或int ch;
_getch();或ch=_getch();
用_getch();會等待你按下任意鍵,再繼續執行下面的語句;
用ch=_getch();會等待你按下任意鍵之後,把該鍵字符所對應的ASCII碼賦給ch,再執行下面的語句。
3.isdight()
isdigit()函數包含在ctype.h頭文件中, 原型: int isdigit(char c); 用法:#include <ctype.h> 功能:判斷字符c是否爲數字 說明:當c爲數字0-9時,返回非零值,否則返回零。 這個函數是判斷某一個字符是否爲數字,可以用一個字符數組接受輸入的字符,然後循環判斷每一個字符是否爲數字,如果其中一個不是數字,那麼返回0,否則返回非0; 如果想要實現負數或者小數的判斷,需要自己在此基礎設計代碼,(判斷小數點的個數和負數符號-) 補充段代碼,可以判斷負數和小數: #include<stdio.h> #include<ctype.h> void main() { char a[10]; int i,j,flag=0; /*flag標記小數點個數,如果超過一個,那麼就不是數字*/ printf("input a number:\n"); scanf("%s",a); for(i=0;a[i]!=0;i++) { if(i==0&&(a[i]=='+'||a[i]=='-')) { i++; continue; } /*如果第一個字符爲+或-,不判斷爲非數字,可能是符號標誌,繼續向下判斷*/ if(a[i]=='.') { flag++; if(flag>1) /*如果小數點個數超過一個,則表示不是數字*/ { break; } else if(flag==1) /*如果小數點個數爲1個,則可能爲小數,繼續判斷*/ { continue; } } j=isdigit(a[i]); if(j==0) { break; } } if(j!=0) printf("%s yes",a); else printf("%s no",a); getch(); }