ATM函数实现

#include <stdio.h>
#include <stdlib.h>
#define kSuccess 1
#define kWrong 0

int password=123456;
float balance = 1000;
//输入密码操作
int inputpassword(void);
//退出程序操作
void shut(void);
//提示用户操作
void show(char *a[],int n);//指针数组 数组中的每一个元素均为指针 这里元素均为字符串 所以用到指针数组
//用户选择
int choice(int max);
//修改密码操作
void revise(void);
//取款操作
void draw(void);
//是否继续
void continuation(void);

int main(int argc, const char * argv[]) {
    // inputpassword();不需要 下面又调用了一次
    int receive = inputpassword();
    if (receive == kWrong) {
        shut();
    }
    
    while(1){
        char *a[4]={"修改密码","取款","查询","退出"};
        show(a, 4);
        int op=choice(4);
        switch (op) {
            case 1:
                revise();
                continuation();
                break;
            case 2:
                draw();
                continuation();
                break;
            case 3:
                printf("您的余额的为:%f\n",balance);
                continuation();
                break;
            case 4:
                shut();
                break;
            default:
                break;
        }
    }
    
    return 0;
}

int inputpassword(void)
{
    int wrongtime = 0;
    int inputpwd = 0;
    while(1){
        printf("请输入密码:");
        scanf("%d",&inputpwd);
        if (inputpwd == password) {
            return kSuccess;
        }else{
            wrongtime++;
            if (wrongtime == 4) {
                printf("抱歉,您的密码输入错误次数过多,您的银行卡将被锁定\n");
                return kWrong;
            }else{
                printf("密码错误 ");
            }
        }
    }
}

void shut(void)
{
    printf("感谢您的使用 再见!");
    exit(EXIT_SUCCESS);
}

void show(char *a[],int n)
{
    printf("****************\n");
    for (int i=0; i<n;i++ ) {
        printf("%d.%s\n",i+1,a[ i ]);
    }
    printf("****************\n");
}

int choice(int max)
{
    int op = 0;
    while(1){
        printf("请按提示选择操作:");
        scanf("%d",&op);
        if (op>0&&op<=max) {
            return op;
        }else{
            printf("输入有误");
        }
    }
}

void revise(void)
{
    int inputpwd = 0;
    while(1){
        printf("请输入原密码:");
        scanf("%d",&inputpwd);
        if (inputpwd ==password) {
            printf("请输入新密码:");
            scanf("%d",&password);
            while(1){
                printf("请再次输入新密码:");
                scanf("%d",&inputpwd);
                if (inputpwd == password) {
                    printf("密码修改成功 !");
                    break;
                }else{
                    printf("您两次输入的密码不一致:");
                }
            }
            break;
        }else{
            printf("密码错误 ");
        }
    }
}

void draw(void)
{
    int money = 0;
    char *a[5]={"100","200","500","1000","其他"};
    show(a, 5);
    int op=choice(5);
    switch (op) {
        case 1:
            money =100;
            break;
            case 2:
            money =200;
            break;
            case 3:
            money = 500;
            break;
            case 4:
            money = 1000;
            break;
            case 5:
            printf("请输入您的取款金额:");
            scanf("%d",&money);
            break;
        default:
            break;
    }
    if (money<balance) {
        balance-=money;
        printf("取款成功");
    }else{
        printf("余额不足!");
    }
}

void continuation(void)
{
    printf("是否继续:‘y'/'n'\n");
    char op;
    getchar();
    op=getchar();
    if (op=='n') {
        shut();
    }
}

 

posted @ 2018-12-21 16:44  kinghyt  阅读(224)  评论(0编辑  收藏  举报