7.30 集训第三天【C语言函数入门】

2019-08-01  本文已影响0人  草莓灵啾啾

今日目标

学会简单实用C语言中的函数, 将函数知识运用到前一天写的代码中

学习内容

函数

实际操作

\small银行取款机(不用函数)

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

int main(int argc, const char * argv[]) {
    int password = 123;
    int inputPwd = 0;
    int totalTime = 4;
    int totalMoney = 1000;
    printf("**************\n");
    printf("   欢迎使用\n");
    printf("**************\n");
    printf("请输入密码:");
    while (1) {
        scanf("%d", &inputPwd);
        if (inputPwd == password) {
            break;
        }else{
            totalTime--;
            if (totalTime == 0) {
                printf("此卡已冻结 请联系客服!\n");
                exit(EXIT_FAILURE);
            }else{
                printf("密码不正确 请重新输入:");
            }
        }
    }
    char choice;
    while(1){
        printf("**************\n");
        printf("1.取款\n");
        printf("2.存款\n");
        printf("3.设置密码\n");
        printf("4.退出\n");
        printf("**************\n");

        char ch[20] = {};
        while (1) {
            printf("请选择操作:");
            int count = scanf("%s", ch);
            
            if (count != 1) {
                printf("输入不合法 ");
            } else{
                choice = ch[0];
                
                if (choice == '1' || choice == '2' || choice == '3' || choice == '4') {
                    break;
                } else{
                    printf("输入不合法 ");
                }
            }
        }
        
        char choice2;
        int newPassword1 = 0;
        int newPassword2 = 0;
        switch (choice) {
            case '1':
                while(1){
                    int outMoney = 0;
                    printf("请输入取款金额:");
                    scanf("%d", &outMoney);
                    
                    if (outMoney > totalMoney) {
                        printf("余额不足是否继续(y/n):");
                        getchar();
                        choice2 = getchar();
                        if (choice2 == 'n') {
                            break;
                        }
                    }else{
                        totalMoney -= outMoney;
                        printf("取款成功 余额为:%d\n", totalMoney);
                        break;
                    }
                    
                }
                break;
            case '2':
                printf("请输入存款金额:");
                int inputMoney = 0;
                scanf("%d", &inputMoney);
                totalMoney += inputMoney;
                printf("存款成功,余额为:%d\n", totalMoney);
                break;
            case '3':
                
                while (1) {
                    printf("请输入新密码:");
                    scanf("%d", &newPassword1);
                    
                    printf("请确认密码:");
                    scanf("%d", &newPassword2);
                    
                    if (newPassword1 == newPassword2){
                        password = newPassword1;
                        printf("更改密码成功\n");
                        break;
                    }else{
                        printf("两次密码不一致 ");
                    }
                }
                break;
            default:
                printf("感谢您的使用\n");
                exit(1);
                break;
        }
    }
    return 0;
}

\small银行取款机(用函数)

#include<stdio.h>
#include<stdlib.h>
 _Bool loginATM(void);
void welcome(void);
void exitATM(int status);
void showMenu(void);
char getChoice(void);
void withdraw(void);//取款
_Bool isContinue(void);
void deposit(void);//存款
void setPassword(void);

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

int main(int argc, const char * argv[]) {
    
    welcome();
    
    _Bool result = loginATM();
    if (result == 0) {
        exitATM(1);
    }
    
   
    while (1) {
        showMenu();
        char choice = getChoice();
        switch(choice){
            case '1':
                //取款
                withdraw();
                break;
            case '2':
                //存款
                deposit();
                break;
            case '3':
                //设置密码
                setPassword();
                break;
            default:
                exitATM(1);
                break;
        }
    }
    
    return 0;
}

_Bool loginATM(void){
    int password = 0;
    int wrongTime = 0;
    
    while(1){
        printf("请输入密码:");
        scanf("%d", &password);
        
        if (password == orgPassword) {
            return 1;
        }else{
            wrongTime++;
            if (wrongTime == 4) {
                return 0;
            }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");
}

char getChoice(void){
    //将输入的所有字符串全部从缓存里面读取出来
    char temp[20] = {};
    
    while (1) {
        printf("请选择操作:");
        int count = scanf("%s", temp);
        
        //确保值输入一个字符
        if (count != 1) {
            printf("输入不合法,");
        } else{
            //获取输入的字符
            char ch = temp[0];
            
            if (ch == '1' || ch == '2' || ch == '3' || ch == '4') {
                return ch;
            }else{
                printf("输入不合法,");
            }
        }
        
    }
}

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

_Bool isContinue(void){
    printf("是否继续?(y/n):");
    
    getchar();
    char ch = getchar();
    if (ch == 'n') {
        return 0;
    }else{
        return 1;
    }
}

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


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

心得体会

在一个简单编写银行取款机代码的基础上,老师给我们扩展了很多知识,运用函数比不运用函数能实现的功能多,虽然函数对于我来说确实是一个难点,可是越难的东西就越要去面对,希望能通过大量练习,对函数这一块知识有一些新的体会。

上一篇下一篇

猜你喜欢

热点阅读