标签:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
char operation;
int totalMoney = 10000;
int password = 123456;
while (1) {
//提示用户相应操作
printf("************************\n");
printf("1.输入密码\n2.存款\n3.取款\n4.查询\n5.退出\n");
printf("************************\n");
//接受用户输入
printf("请输入操作序号:");
scanf("%c", &operation);
//判断用户输入是否正确
while ( operation < ‘1‘ || operation > ‘5‘) {
getchar();
printf("输入错误,请重新输入:");
scanf("%c", &operation);
}
//根据用户输入去完成相应地事情
int inputPassword;
int worngTime = 1;//密码错误次数
int totalTime = 3;//总的输入次数
int putMoney=0,getMoney=0;
switch (operation) {
case ‘1‘:
printf("请输入密码:");
scanf("%d", &inputPassword);
while (inputPassword != password && worngTime < totalTime) {
printf("密码错误 剩余%d次机会 请重新输入密码:", totalTime - worngTime );
scanf("%d", &inputPassword);
worngTime ++;
}
//两种情况退出
if (inputPassword == password) {
printf("***************\n");
printf("密码输入正确!\n");
printf("***************\n");
}else{
printf("***************\n");
printf("输入次数过多,强制退出!\n");
printf("***************\n");
exit(EXIT_SUCCESS);
}
getchar();
break;
case ‘2‘:
while (1) {
printf("***************\n");
printf("1.$100\n2.$200\n3.$500\n4.其他\n");
printf("***************\n");
printf("请选择操作序号:");
getchar();
scanf("%c", &operation);
//判断用户输入是否正确
while ( operation < ‘1‘ || operation > ‘4‘) {
getchar();
printf("输入错误,请重新输入:");
scanf("%c", &operation);
}
switch (operation) {
case ‘1‘:
putMoney = 100;
break;
case ‘2‘:
putMoney = 200;
break;
case ‘3‘:
putMoney = 500;
break;
case ‘4‘:
printf("请输入存款金额:");
scanf("%d", &putMoney);
default:
exit(EXIT_SUCCESS);
break;
}
totalMoney += putMoney;
printf("存款成功!您的账户余额为$%d\n",totalMoney);
//返回上一步操作之前 提示用户是否继续操作
char choice;
printf("是否继续?(y/n):\n");
//消去上一次操作留下的‘\n‘
getchar();
scanf("%c", &choice);
if (choice == ‘y‘) {
//继续取款
continue;
}else{
//返回
break;
}
}
getchar();
break;
case ‘3‘:
//取款
while (1) {
printf("***************\n");
printf("1.$100\n2.$200\n3.$500\n4.其他\n");
printf("***************\n");
printf("请选择操作序号:");
getchar();
scanf("%c", &operation);
//判断用户输入是否正确
while ( operation < ‘1‘ || operation > ‘4‘) {
printf("输入错误,请重新输入:");
getchar();
scanf("%c", &operation);
}
switch (operation) {
case ‘1‘:
getMoney = 100;
break;
case ‘2‘:
getMoney = 200;
break;
case ‘3‘:
getMoney = 500;
break;
case ‘4‘:
printf("请输入取款金额:");
getchar();
scanf("%d", &getMoney);
default:
break;
}
//余额不足
if (getMoney > totalMoney) {
printf("对不起,您的余额不足!\n");
}
//从总金额里扣除取款金额
else {
totalMoney -= getMoney;
printf("取款成功!您的账户余额为$%d\n",totalMoney);
}
//返回上一步操作之前 提示用户是否继续操作
char choice;
printf("是否继续?(y/n):\n");
//消去上一次操作留下的‘\n‘
getchar();
scanf("%c", &choice);
if (choice == ‘y‘) {
//继续取款
continue;
}else{
//返回
break;
}
}
getchar();
break;
case ‘4‘:
printf("***************\n");
printf(" 您的余额为 $%d\n", totalMoney);
printf("***************\n");
getchar();
break;
case ‘5‘:
printf("***************\n");
printf("感谢您的使用 再见!\n");
printf("***************\n");
exit(EXIT_SUCCESS);
break;
default:
break;
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/hmzxwky/p/5025831.html