自学c语言自娱自乐的,看到有的练习题上有模拟ATM机流程的练习就试着写了一个。
#include
#include
int userpassword;
int remaind;
void welcome();
void end();
int start();
void password();
void loadin();
void leftcount();
void getmoney();
void savemoney();
void welcome()
{
printf("******************************\n");
printf("* welcome use kirito ATM *\n");
printf("* password 819821 *\n");
printf("* *\n");
printf("* any key to continue... *\n");
printf("******************************\n");
getch();
system("cls");
}
void end()
{
system("cls");
printf("******************************\n");
printf("* kirito ATM *\n");
printf("* *\n");
printf("* *\n");
printf("* any key to end... *\n");
printf("******************************\n");
getch();
exit(0);
}
int start()
{
int z;
do
{
system("cls");
printf("******************************\n");
printf("* please select key: *\n");
printf("* 1. password *\n");
printf("* 2. remaind *\n");
printf("* 3. getmoney *\n");
printf("* 4. savemoney *\n");
printf("* 5. Return *\n");
printf("******************************\n");
scanf("%d",&z);
}
while(z!=1 && z!=2 && z!=3 && z!=4 && z!=5);
return z;
}
void loadin()
{
int i,password;
i = 0;
system("cls");
printf("input password:\n");/*验证密码*/
scanf("%d",&password);
while(password != userpassword)/*验证密码,三次后系统退出*/
{
i++;
system("cls");
if(i==1 || i==2)
printf("Wrong password,input again\n");
if(i == 3)
{
printf("Wrong Password,System Close!");
Sleep(2000);
end();
}
scanf("%d",&password);
}
}
void password()
{ int key;
int password;
int newpassword1,newpassword2;
loadin();/*验证密码*/
do{
system("cls");
printf("******************************\n");
printf("* please select key: *\n");
printf("* 1. change password *\n");
printf("* 2. Return *\n");
printf("* *\n");
printf("******************************\n");
scanf("%d",&key);
}
while(key != 1 && key != 2);
switch(key)
{
case 1 :
{
do
{
system("cls");
printf("input new password:\n");
scanf("%d",&newpassword1);
system("cls");
printf("input new password again:\n");
scanf("%d",&newpassword2);
system("cls");
if(newpassword1 != newpassword2)
{
printf("diffrent password,try again.");
Sleep(2000);
}
}while(newpassword1 != newpassword2);/*循环体内保证两次输入新密码一致*/
userpassword = newpassword1;
printf("new password change success!\n");
Sleep(2000);
};break;
case 2 : break;
}
}
void leftcount()
{
loadin();/*验证密码*/
printf("remaind : %d\n",remaind);
getch();
}
void getmoney()
{ int key;
int t;
loadin();/*验证密码*/
while(1)
{
do
{
system("cls");
printf("******************************\n");
printf("* 1. 100 *\n");
printf("* 2. 500 *\n");
printf("* 3. 1000 *\n");
printf("* 4. return *\n");
printf("******************************\n");
scanf("%d",&key);
}
while(key!=1 && key!=2 && key!=3 && key!=4);
switch(key)
{
case 1 :
{ system("cls");
if(remaind >= 100)
{ printf("take your money.\n");
remaind = remaind - 100;
printf("remaind : %d\n",remaind);
}
else printf("no enough remaind.\n");
};break;
case 2 :
{ system("cls");
if(remaind >= 500)
{ printf("take your money.\n");
remaind = remaind - 500;
printf("remaind : %d\n",remaind);
}
else printf("no enough remaind.\n");
};break;
case 3 :
{ system("cls");
if(remaind >= 1000)
{ printf("take your money.\n");
remaind = remaind - 1000;
printf("remaind : %d\n",remaind);
}
else printf("no enough remaind.\n");
};break;
default : break;
}
printf("1:continue or 2:return\n");
scanf("%d",&t);
if(t!=1) break;
}
}
void savemoney()
{
int key;
int t;
loadin();
while(1)
{
do
{
system("cls");
printf("******************************\n");
printf("* 1. 100 *\n");
printf("* 2. 500 *\n");
printf("* 3. 1000 *\n");
printf("* 4. return *\n");
printf("******************************\n");
scanf("%d",&key);
}
while(key!=1 && key!=2 && key!=3 && key!=4);
switch(key)
{
case 1 :
{ system("cls");
remaind = remaind + 100;
printf("remaind : %d\n",remaind);
};break;
case 2 :
{ system("cls");
remaind = remaind + 500;
printf("remaind : %d\n",remaind);
};break;
case 3 :
{ system("cls");
remaind = remaind + 1000;
printf("remaind : %d\n",remaind);
};break;
default : break;
}
printf("1:continue or 2:return\n");
scanf("%d",&t);
if(t!=1) break;
}
}
int main()
{
int key;
userpassword = 819821;
remaind = 10000;
system("title ATM@kirito");
system("mode con cols=35 lines=10");
welcome();
while(1)
{
key = start();
switch(key)
{
case 1 : password();break;
case 2 : leftcount();break;
case 3 : getmoney();break;
case 4 : savemoney();break;
case 5 : end();
}
}
return 0;
}