ATM_Fuction

#include 
#include 
#include 

int password = 123456;
int balance = 10000;
bool isLogined = false;

void alert(char *content);
int getValidOperation(int maxNum);
void quit(int status);
void query();
void isContinue();
bool inputPassword();
void takeMoney();
void changePsw();


int main(int argc, const char * argv[]) {
    
    bool result;
    
    while (1) {
        alert("1.输入密码\n2.取款\n3.查询余额\n4.更改密码\n5.退出");
        int operation = INT32_MAX;
        operation = getValidOperation(5);
        switch (operation) {
                //1.输入密码
            case 1:
                result = inputPassword();
                if (result == false) {
                    quit(EXIT_SUCCESS);
                }else{
                    isLogined = true;
                }
                break;
                //2.取款
            case 2:
                takeMoney();
                isContinue();
                break;
                
                //3.查询余额
            case 3:
                query();
                isContinue();
                getchar();
                break;
                //4.更改密码
            case 4:
                changePsw();
                break;
                //5.退出
            case 5:
                quit(EXIT_SUCCESS);
            default:
                break;
        }
        printf("\n");
        
    }
    return 0;
}

void alert(char *content){
    printf("****************\n");
    printf("%s\n", content);
    printf("****************\n");
}

int getValidOperation(int maxNum){
    int operation;
    
    printf("选一个吧:");
    scanf("%d", &operation);
    
    while (operation < 1 || operation > maxNum) {
        printf("别搞笑,好好输:");
        getchar();
        scanf("%d", &operation);
    }
    return operation;
}

void quit(int status){
    alert("拔卡!拔卡!拔卡!");
    exit(status);
}


void query(){
    if (isLogined == false) {
        bool result = inputPassword();
        if (result == false) {
            quit(EXIT_SUCCESS);
        }else {
            isLogined = true;
        }
    }else{
    printf("****************\n");
    printf("你还有¥%d\n", balance);
    printf("****************\n");
    }
    query();
}

void isContinue(){
    char choose;
    
    alert("继续不?(y/n):");
    getchar();
    scanf("%c", &choose);
    
    while (choose != 'y' && choose != 'n') {
        printf("好好输!(y/n)\n");
        getchar();
        scanf("%c", &choose);
    }
    if (choose == 'n') {
        quit(EXIT_SUCCESS);
    }
}


bool inputPassword(){
    int inputedpassword = INT32_MAX;
    int totalWrongTime = 3;
    
    do {
        printf("%s", inputedpassword == INT32_MAX ? "密码是啥:": "别骗我!密码是啥:");
        getchar();
        scanf("%d", &inputedpassword);
        
        totalWrongTime--;
    } while (inputedpassword != password && totalWrongTime > 0);
    
    if (inputedpassword != password) {
        return false;
    }else{
        return true;
    }
}


void takeMoney(){
    if (isLogined == true) {
        int totalMoney;
        int num2_1;
        alert("1.¥100\n2.¥300\n3.¥500\n4.其他金额");
        num2_1 = getValidOperation(4);
        switch (num2_1) {
            case 1:
                totalMoney = 100;
                break;
            case 2:
                totalMoney = 300;
                break;
            case 3:
                totalMoney = 500;
                break;
            case 4:
                printf("取多少:");
                scanf("%d", &totalMoney);
                break;
            default:
                break;
        }
        if (totalMoney <= balance) {
            printf("\n您的余额为:¥%d\n \n", balance -= totalMoney);
        }else {
            printf("\n穷B,钱不够!\n \n");
        }
        
    }else{
        bool result =  inputPassword();
        if (result == false) {
            quit(EXIT_SUCCESS);
        }else {
            isLogined = true;
        }
        
    }
    takeMoney();
}



void changePsw(){
    if (isLogined == false) {
        bool result = inputPassword();
        if (result == false) {
            quit(EXIT_SUCCESS);
        }else {
            isLogined = true;
        }
    }else{
        printf("输入新密码:\n");
        scanf("%d", &password);
    }
    changePsw();
}

你可能感兴趣的:(ATM_Fuction)