Day2简易自动取款机

简介

1.复习C语言知识点
2.写一个模拟ATM取款机的简易小demo

Part 1

1.%02d

    printf("%02d",1);
>>>02//需要输出0002这类特殊格式时只需要 0 + 长度量 + d 即可

2.声明与定义的区别
声明
对于函数来说,只是告诉编译器所写函数的名称,参数,返回值等基本信息,相当于告诉编译器有这个东西,至于里面有什么东西,是不清楚的

定义
对于函数来说,需要将之前所声明的函数的内容给补充完全。明白函数内容是什么,该执行什么操作
Example

int function(void);//函数声明
int main(void)
{
    return 0;
}
//函数定义
int function(void){
      //函数体
}

但是对于变量来说,情况就变得有些复杂了
Example

extern int variables;//此处仅仅是**声明**了一个整形变量
int variable;//此处**声明并且定义**了一个整形变量

但是网上的博客中对变量的定义和声明解释不够清楚并且说法也不一致,在群里这个问题时,说法也是不一致。于是我抱着疑惑开始寻找资料

C Primer Plus( 6th中文版 ):
P24上方如下写道:
int num;
这行代码叫做声明,在该例中,声明完成两件事。其一,在函数中有一个名为num的变量。其二,int 表明num是一个整数。编译器使用这些信息为num变量在内存中分配存储空间
P26如下写道:
num = 1;
声明时,编译器在计算机内存中为变量num预留了空间,然后在执行该语句时,把值存储在之前的预留的位置

抱着中文翻译可能会有把信息扭曲的可能,我翻了翻英文版的内容
内容摘要如下:
C Primer Plus, Sixth Edition
Declarations
int num;

This line from the program is termed a declaration statement . The declaration statement is one of C’s most important features. This particular example declares two things. First, somewhere in the function, you have a variable called num . Second, the int proclaims num as an integer—that is, a number without a decimal point or fractional part. ( int is an example of a data type .) The compiler uses this information to arrange for suitable storage space in memory for the num variable.

Assignment
num = 1;

The next program line is an assignment statement , one of the basic operations in C. This particular example means “assign the value 1 to the variable num .” The earlier int num; line set aside space in computer memory for the variable num , and the assignment line stores a value in that location.

本着实践是检验真理的唯一标准原则
让我们打开VS2017查看是否正确
结果如下:

image.png

在本例中分别对两个整形变量做了一个对比,整形变量再与extern 修饰的整形变量作为对比。添加断点并且对这三个变量都添加监视
我们发现i和test都可以被监视到,并且已经被分配了一块内存。只有extern是无法被监视的,但我们对extern int number赋以初值时结果又是如何呢?


image.png

此时 number可以被监视到并且被赋予了一块内存,这便说明

extern int variables;//此处仅仅是**声明**了一个整形变量,但是没有分配内存
int variable;//此处**声明并且定义**了一个整形变量,而且分配了内存

Part 2

简易ATM机功能
自顶向下的思想分析 可将ATM机的功能模块分为四块
1.输入密码进入账户
2.用户界面显示选项
3.选择选项进行操作

三个模块在再一次进行细分
part 1:输入密码具有一定次数,超过一定次数便会冻结账户
part 2:在控制台中,我们只能用键盘读到信息来选择相应功能,但是键盘中的字符数>>功能数量,因此需要对错误操作具有容错性,需要对键入的错误信息提供一个重新键入的功能
part 3:对于某个具体功能内仍有可能出现误操作,要对此有一个提示信息(例如在银行账户中取款金额大于账户金额,就会提示取款失败,并且给予一个提示
part 4:给予用户适当的提示信息,引导用户进行正确操作操作。当我们书写程序时往往会带有些上帝视角,知道为什么会出现这个东西,不需要进行提示。但是对于用户来说,出现错误很可能不知所措,需要提示信息进行引导。所以我们要给予一定的提示信息进行引导

Code

Part 1:

bool PassWord(int password) {
    int input;
    for (int i = 0; i < 4; i++) {
        scanf_s("%d", &input);
        if (input == password)
            return true;
        else
            printf("Please rewrite your password \n");
    }
    return false;
}

此处让用户最多输入四次密码,如果超过便会冻结账户

Part 2:

void print_option(void) {
//提供四个选项给用户
    printf("You can choose the 4 options,please put you number\n");
    printf("1. Take your money\n");
    printf("2. Put your money\n"); 
    printf("3. Reset your password\n"); 
    printf("4. Exit\n");
}

Part 3:

while( 1 )
    {
        scanf_s("%d", &options);

        switch ( options )
        {
        case 1: {
            printf(" Please put your money\n");
            scanf_s("%d", &money);
            total += money;
            printf(" Your account have %d RMB", total);
            break; 
        }
            case 2:
            printf("Please take your money\n");
            scanf_s("%d", &money);
            if (money > total)
                printf(" False, your money is %d", total);
            else
                total -= money;
            break;
        case 3:
            printf(" Please write your new password \n");
            scanf_s("%d", &new_password);
            if (password == new_password) {
                printf(" Please write your new password \n");
                scanf_s("%d", &password);
            }
            else {
                printf(" Sorry, your password isn't true.Please write again \n");
                scanf_s("%d", &new_password);
            }
            break;
        case 4:
            printf("exit\n");
            return 0;
        default: 
            printf("Error! Please rewrite new number\n");
            break;
        }
    }

用户有可能误操作,所以应该给予提示,并且操作次数不能是有限的

All

#include
void Welcome (void);
void print_option(void);
bool PassWord(int);
int main(void)
{
    int options = -1;
    int password = 123456;
    int total = 0;
    int money = 0;
    int new_password;
    //welcome interface
    Welcome();

    //put your password
    if ( !PassWord(password)) {
        printf("Frozen your account!");
        return 0;
    }

    //option interface
    print_option();
    while( 1 )
    {
        scanf_s("%d", &options);

        switch ( options )
        {
        case 1: {
            printf(" Please put your money\n");
            scanf_s("%d", &money);
            total += money;
            printf(" Your account have %d RMB", total);
            break; 
        }
            case 2:
            printf("Please take your money\n");
            scanf_s("%d", &money);
            if (money > total)
                printf(" False, your money is %d", total);
            else
                total -= money;
            break;
        case 3:
            printf(" Please write your new password \n");
            scanf_s("%d", &new_password);
            if (password == new_password) {
                printf(" Please write your new password \n");
                scanf_s("%d", &password);
            }
            else {
                printf(" Sorry, your password isn't true.Please write again \n");
                scanf_s("%d", &new_password);
            }
            break;
        case 4:
            printf("exit\n");
            return 0;
        default: 
            printf("Error! Please rewrite new number\n");
            break;
        }
    }
    return 0;
}
void Welcome(void) {
    //for (int i = 0; i < 14; i++)
    printf("*************\n");
    printf("   Welcome!   \n");
    printf("*************\n");
}
void print_option(void) {
    printf("You can choose the 4 options,please put you number\n");
    printf("1. Take your money\n");
    printf("2. Put your money\n"); 
    printf("3. Reset your password\n"); 
    printf("4. Exit\n");
}
bool PassWord(int password) {
    int input;
    for (int i = 0; i < 4; i++) {
        scanf_s("%d", &input);
        if (input == password)
            return true;
        else
            printf("Please rewrite your password \n");
    }
    return false;
}

对于这个非常简单的demo还有细节性的地方没有实现,例如输入密码的是希望取卡的操作。但是写这个程序的目的是感受编程思维,学会自顶向下的方法。先分析一个工程的功能需求,分成几块独立功能块。再分别对这些功能进行分析,实现一个功能需要哪几个模块,再分别对模块再次分析。直至可以直接写出被分好的小模块。

你可能感兴趣的:(Day2简易自动取款机)