下面模仿下ATM取款機,有錢真好!!!
輸入密碼正確後可以:取款,存款,退出
[cpp]
#include<stdio.h>
/*
模仿ATM取款程序
*/
int strCompare(char *,char *);
int accountVerify();
void accountOperate();
void simulateOfATM()
{
if(accountVerify())
accountOperate();
printf("GOODBYE\n");
}
//不使用自帶的strcmp,自己寫個玩玩
int strCompare(char *str1,char *str2)
{
while(*str1&&*str2&&(*str1==*str2))
//while(*str1!='\0'&&*str2!='\0'&&(*str1==*str2))//這個效率低點,不過容易理解
{
str1++;
str2++;
}
return *str1-*str2;
}
//通行驗證 www.2cto.com
int accountVerify()
{
char password[50];//我就不信你們的密碼超過50, 30倒是見過
char pwd[10]={"abc"};
int pwdCount=0;//記錄密碼輸入次數,不能超過3次
do{
printf("please input your password or press ENTER to break: \n");
gets(password);
if(!strCompare(password,"\0")){
break;
}
if(strCompare(password,pwd)){//密碼錯誤
printf("PASSWORD ERROR !!!\n");
pwdCount++;
}
else
{
return 1;
}
}while(pwdCount<3);
if(pwdCount>=3)
printf("input count has outnumber,good bye!!\n");//輸入超過限制
else printf("GOODBYE\n");
return 0;
}
//賬戶操作
void accountOperate()
{
int operate,initMoney=100;//初始賬戶值:100
int oprateMoney = 0;//操作金額數
do{
printf("the amount of your card is: %d\n",initMoney);
printf("*********** ACCOUNT OPERATE***************\n");
printf("* 1: get money *\n");
printf("* 2: deposite *\n");
printf("* 3: exit *\n");
printf("******************************************\n");
printf("please select a operate: ");
scanf("%1d",&operate);
switch(operate)
{
case 1:
printf("how much whould you like to get: ");
scanf("%d",&oprateMoney);
if(oprateMoney<=0||oprateMoney>initMoney)//如果輸入金額小於等於0或者大於賬戶余額
{
printf("輸入錯誤或者余額不足!!\n");
continue;
}
initMoney -= oprateMoney;
printf("¥%d 正在吐鈔,請笑納………\n",oprateMoney);
continue;
case 2:
printf("how much whould you like to put: ");
scanf("%d",&oprateMoney);
initMoney += oprateMoney;
printf("press any key to go back");
continue;
case 3:
default:
return;
}
}while(1);
}