看板 ESOE-94 關於我們 聯絡資訊
#include<iostream> #include<string.h> using namespace std; class Account { private: bool state; int error; double amount; char nameaa[20]; int password; public: Account() //內鍵一個帳戶 { char nameaab[20]="admin"; strcpy(nameaa,nameaab); password=123; amount=0; error=0; state=true; } Account(char* inameaa,int ipsw,double imoney,bool istate, int ierror) //USER建立帳戶 { strcpy(nameaa,inameaa); password=ipsw; amount=imoney; error=ierror; state=istate; } void withdraw(double imoney) //領錢 { if(check()) //確定帳戶內的錢夠領 { if(amount>=imoney) { amount-=imoney; cout<<"Withdraw "<<imoney<<endl; } else { cout<<"You don't have enough money to withdraw."<<endl; //不夠領 } } } void deposit(double imoney) //提款 { if(check()) { amount+=imoney; //amount=amount+money cout<<"Deposit "<<imoney<<endl; } } void query() //查餘額 { if(check()) { cout<<"Your money left: "<<amount<<endl;//顯示餘額 } } void changepassword() //改密碼 { if(check()) { int ipsw; cout<<"Please enter your new password: ";//輸入新密碼 cin>>ipsw; //存入新密碼 password=ipsw; cout<<"Password has been changed!"<<endl; //密碼已被更改 } } int check() //這大段是檢驗密碼程式 { if(state==true) //計數器仍容許錯誤(還可輸入密碼) { int ipsw; for(;state==true;) { cout<<"Please enter your password: "; cin>>ipsw; if(ipsw!=password)//若輸入的密碼錯誤, { error++; //將計數器+1 if(error>=3) //若計數器的值大於等於3, { cout<<"You have failed 3 times, the account is locked."<<endl; state=false;//將帳戶鎖住 } else { cout<<"Incorrect!!"<<endl;//USER嘗試錯誤 } return 0; //密碼檢查失敗 } error=0; //將對錯值歸零 return 1; //密碼檢查成功 } } else { cout<<"The account has been locked!!"<<endl; return 0; //因為 STATE=FALSE 所以表示帳號已鎖 連檢查都不用檢查 就直接檢查失敗 } } }; void main() { char name[20]; int psw; double amount; int error; cout<<"Please set your account name: "; cin>>name; cout<<"Please set your password: "; cin>>psw; cout<<"How much money do you want to put in your account? "; cin>>amount; cout<<"Please set times of error permitted: "; cin>>error; Account ac1; Account ac2(name,psw,amount,true,error); /* Account ac1; Account ac2(name,psw,amount,true,error); 改成 Account ac2; 這樣你前面不管 什麼 後面操作的都是ADMIN帳號 */ int y; double m; for(;;) //無限次重複使用選單 { cout<<"What do you want to do:(1.withdraw/2.deposit/3.query/ 4.change password)"; cin>>y; switch(y) { case 1: cout<<"Amount you want to withdraw = ? "; cin>>m; ac2.withdraw(m); break; case 2: cout<<" Amount you want to deposit = ? "; cin>>m; ac2.deposit(m); break; case 3: ac2.query(); break; case 4: ac2.changepassword(); break; } } } -- \||/ ii \/ \/ \/ GUNDAM [‵′] [‵′] [‵′] i[‵′]i _丄[′‵]丄_ SEED [] ╟[同] ![]! ╟[] [] DESTINY _||_ \_||_/ _||_── _||_ _||_ -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.167.19.220 ※ 編輯: loshihyen 來自: 218.167.19.220 (06/20 23:48)
mikearice:雖然我還是不會,不過謝謝你ㄌ 06/21 00:04