• 1207--ATM自动取款机的实现


    #include <stdio.h>

    #include <stdlib.h>

    #include <stdbool.h>

    //提示用户操作

    void alert(char *content);

    /*

     检查输入是否合法

     maxNum: 最大的范围  5 (1-5)

     */

    int getValidOperation(int maxNum);

    //查询余额

    void query();

    //退出程序

    void quit(int status);

    //是否继续操作

    void isContinue();

    //输入密码

    bool inputPassword();

    //取款

    void takeMoney();

    //定义全局变量

    int totalMoney = 1000;//总金额

    int password = 12345;//密码

    bool isLogined = false; //记录登陆的状态

    int main(int argc, const char * argv[]) {

        int operation = 0; //记录最终选择

        

        while (1) {

            //提示用户操作

            alert("1.输入密码 2.取款 3.查询 4.退出 ");

            

            //判断是否合法

            operation = getValidOperation(4);

            

            bool result ;

            //对用户的操作进行处理

            switch (operation) {

                case 1:

                    //输入密码

                    result =  inputPassword();

                    if (result == false) {

                        //密码错误 退出

                        quit(EXIT_SUCCESS);

                    } else {

                        //记录状态

                        isLogined = true;

                    }

                    

                    //询问是否继续操作

                    isContinue();

                    break;

                case 2:

                    //取款

                    takeMoney();

                    isContinue();

                    break;

                case 3:

                    //查询

                    query();

                    isContinue();

                    break;

                case 4:

                    //退出

                    quit(EXIT_SUCCESS);

                    break;

                default:

                    break;

            }

            

        }

        return 0;

    }

    void alert(char *content){

        printf("***************** ");

        printf("%s", content);

        printf("***************** ");

    }

    int getValidOperation(int maxNum){

        int operation ;//记录用户输入的操作

        

        //提示用户选择操作

        printf("请选择操作:");

        scanf("%d", &operation);

        

        //判断是否合法

        while (operation < 1 || operation > maxNum) {

            printf("输入错误,请重新输入:");

            scanf("%d", &operation);

        }

        

        //已经得到一个合法的了

        return operation;

    }

    void quit(int status){

        //显示一点内容给用户

        alert("欢迎下次使用 ");

        

        exit(status);

    }

    void query(){

        printf("***************** ");

        printf("余额为$%d ", totalMoney);

        printf("***************** ");

    }

    void isContinue(){

        char choice;

        

        printf("是否继续?(y/n):");

        

        //抛掉上一次输入的' '

        getchar();

        

        //获取这一次的输入

        scanf("%c", &choice);

        

        while (choice != 'y' && choice != 'n') {

            printf("输入错误(y/n):");

            

            getchar();

            scanf("%c", &choice);

        }

        

        if (choice == 'n') {

            quit(EXIT_SUCCESS);

        }

    }

    bool inputPassword(){

        int inputedPassword = INT32_MAX;

        int totalWrongTime = 3;

        

        do {

            printf("%s:", inputedPassword == INT32_MAX ? "请输入密码" : "密码错误 请重新输入");

            scanf("%d", &inputedPassword);

            

            //消耗一次机会

            totalWrongTime--;

        } while ((inputedPassword != password) && totalWrongTime > 0);

        

        //判断密码是否输入正确

        if (inputedPassword != password){

            return false;

        } else{

            return true;

        }

    }

    void takeMoney(){

        //判断是否输入过密码了

        if (isLogined == false) {

            //没有登陆

            bool result =  inputPassword();

            if (result == false) {

                //密码错误 退出

                quit(EXIT_SUCCESS);

            } else {

                //记录状态

                isLogined = true;

            }

        } else {

            //1.提示额度

            alert("1.$100 2.$200 3.$500 4.返回 ");

            

            //判断输入

            int operation = getValidOperation(4);

            

            int outMoney = 0;

            switch (operation) {

                case 1:

                    //100

                    outMoney = 100;

                    break;

                case 2:

                    //200;

                    outMoney = 200;

                    break;

                case 3:

                    //500

                    outMoney = 500;

                    break;

                case 4:

                    //返回

                    break;

                default:

                    break;

            }

            

            //显示取款金额

            if (totalMoney < outMoney){

                alert("余额不足 ");

            } else{

                printf("*************** ");

                printf("取款%d 余额%d ", outMoney, totalMoney -= outMoney);

                printf("*************** ");

            }

        }

    }

  • 相关阅读:
    如何在for循环中使用多线程
    解决ios10以上H5页面手势、双击缩放问题
    select标签默认选项
    vue三级联动
    手动安装composer详细教学
    密码校验:长度6位以上,至少包含一个数字,一个大写字母,一个小写字母,不包含空格
    tp5生成6位不重复验证码
    css漂亮的阴影边框
    圆形进度条css3样式
    jQuery倒计时组件(jquery.downCount.js)
  • 原文地址:https://www.cnblogs.com/damonWq/p/5027111.html
Copyright © 2020-2023  润新知