C-ATM实战

2019-08-08  本文已影响0人  小石头呢

目的

通过模拟实现ATM相关功能,达到训练条件语句,循环语句,函数的作用

技术

switch语句,while循环与break语句,函数该如何使用,getchar()的使用

如何使用

1.switch语句

//接收用户的选择
int choice = getChoice();
        
//判断用户的选择
switch(choice){
    case 1:
        //取款
        withdraw();
        break;
    case 2:
        //存款
        deposit();
        break;
    case 3:
        //设置密码
        setPassword();
        break;
    default:
        exitATM(EXIT_SUCCESS);
        break;
}        

上面的例子是根据choice的值进入相应的case中进行相关的操作

2.while循环与break语句

break是循环控制语句之一,用于终止循环,是用来跳出循环语句,条件语句if不可以使用,switch中的用法和这里的不同。

printf("请输入取款金额:");
while (1){       
    scanf_s("%d",&tempMoney);

    if (changeMoneyInATM(tempMoney,ChangeTypeOut)){
        printf("取款成功");
        break;
    }else{
        printf("余额不足,请重新输入:");
    }
}

上面的例子是一个输入取款金额的死循环,只有当输入的取款金额合法的时候才会经过break跳出循环

3.函数的使用

void exitATM(int status){
    printf("*************************\n");
    printf("   感谢你的使用 再见!!!\n");
    printf("*************************\n");
    exit(status);
}

上面的函数是一个没有返回值有一个参数的例子,这个函数的功能是完成退出程序的功能。函数内部调用了系统函数exit(),需要外部传入参数帮助我们调用。

bool isContinue(void){
    fflush(stdin);
    printf("是否继续?(y/n):");

    char ch = getchar();
    if (ch == 'n') {
        return false;
    }else{
        return true;
    }
}

上面的函数是完成是否继续的功能。接受一个缓冲流的字符,比较字符得到一个bool值。我们将我们做完事得到的bool值返回给外部,告诉外部是否继续。

4.getchar()的使用

int getChoice(void){

    //将输入的所有字符串全部从缓冲区里面读取出来
    int ch;
    
    while (1) {
        printf("请选择操作:");
        scanf_s("%d", &ch);
     
        if (ch == 1 || ch == 2 || ch == 3 || ch == 4) {
           return ch;
        }else{
            printf("输入不合法,");
            fflush(stdin);
        }
    }      
}

上面的函数是返回一个合法的输入值给外部,我们调用getchar()接受一个字符。因为有可能多次输出,所以在每次新的比较之前调用fflush(stdin);清空缓冲区。

具体使用

#define ATM

#include <stdio.h>
#include <stdlib.h>

//定义全局变量
int orgPassword = 123;
int totalMoney = 1000;

//函数声明
bool loginATM(void);
void welcome(void);
void exitATM(int status);
void showMenu(void);
int getChoice(void);
void withdraw(void);//取款
bool isContinue(void);
void deposit(void);//存款
void setPassword(void);

#endif

#include "ATM.h"

int main(int argc, const char * argv[]) {
    //欢迎界面
    welcome();
    
    //登录
    bool result = loginATM();
    if (result == false) {
        //密码错误次数过多
        exitATM(EXIT_FAILURE);
    }
    
    //主要操作
    while (1) {
        //提示操作
        showMenu();
        
        //接收用户的选择
        int choice = getChoice();
        
        //判断用户的选择
        switch(choice){
            case 1:
                //取款
                withdraw();
                break;
            case 2:
                //存款
                deposit();
                break;
            case 3:
                //设置密码
                setPassword();
                break;
            default:
                exitATM(EXIT_SUCCESS);
                break;
        }

        system("CLS");
        
    }
    
    return 0;
}

bool loginATM(void){
    int password = 0;
    int wrongTime = 0;
    
    while(1){
        printf("请输入密码:");
        scanf_s("%d", &password);
        
        if (password == orgPassword) {
            return true;
        }else{
            wrongTime++;
            if (wrongTime == 4) {
                return false;
            }else{
                printf("密码错误,");
            }
        }
    }
}

void welcome(void){
    printf("***************\n");
    printf("   欢迎使用ATM\n");
    printf("***************\n");
}

void exitATM(int status){
    printf("*************************\n");
    printf("   感谢你的使用 再见!!!\n");
    printf("*************************\n");
    exit(status);
}

void showMenu(void){
    printf("**************\n");
    printf("1.取款\n");
    printf("2.存款\n");
    printf("3.设置密码\n");
    printf("4.退出\n");
    printf("**************\n");
}

int getChoice(void){
    //将输入的所有字符串全部从缓冲区里面读取出来

    int ch;
    
    while (1) {
        printf("请选择操作:");
        scanf_s("%d", &ch);
     
        if (ch == 1 || ch == 2 || ch == 3 || ch == 4) {
           return ch;
        }else{
            printf("输入不合法,");
            fflush(stdin);
        }
    }
        
}

//取款
void withdraw(void){
    int outMoney = 0;
    
    while (1) {
        printf("请输入取款金额:");
        scanf_s("%d", &outMoney);
        
        if(outMoney > 0 && outMoney <= totalMoney){
            totalMoney = totalMoney - outMoney;
            
            printf("取款成功 余额为:%d\n", totalMoney);
            //提示是否继续
            bool ch = isContinue();
            if (ch == false) {
                return; //break;
            }
        }else{
            printf("余额不足,");
        }
    }
}

bool isContinue(void){
    fflush(stdin);
    printf("是否继续?(y/n):");

    char ch = getchar();
    if (ch == 'n') {
        return false;
    }else{
        return true;
    }
}

void deposit(void){
    int intputMoney = 0;
    while (1) {
        printf("请输入存款金额:");
        scanf_s("%d", &intputMoney);
        
        totalMoney = totalMoney + intputMoney;
        printf("存款成功,余额为:%d\n", totalMoney);
        bool ch = isContinue();
        if (ch == false) {
            return; //break;
        }
    }
}


void setPassword(void){
    int newPwd1 = 0;
    int newPwd2 = 0;
    
    while (1) {
        //输入原始密码
        bool result = loginATM();
        if (result == true) {
            while (1) {
                printf("请输入新密码:");
                scanf_s("%d", &newPwd1);
                
                printf("请确认密码:");
                scanf_s("%d", &newPwd2);
                
                if (newPwd1 == newPwd2) {
                    printf("设置密码成功!\n");
                    fflush(stdin);
                    getchar();
                    return;
                } else{
                    printf("两次密码不一致,");
                }
            }
           
        }
    }
}

运行结果:

上一篇 下一篇

猜你喜欢

热点阅读