用函式来传达你的心意> 0 <

前情提要一下,上次在变数命名的善意那篇中我们把arr换成seats代表一堆位置,n1换成seat代表位置索引,n2换成number代表要放入seats内的值,这次我想带你探索while迴圈内想表达什么,如何才能让别人一眼就看出他想说的事。

int main(){    int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};    int i, j, seat, number;    printf("*seating*\n");    for(int i = 0; i < 10; ++i){        printf("%d ", seats[i]);    }        printf("\n");    printf("***************\n");    printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");    scanf("%d %d", &seat, &number);    while(number != 0){        if(seats [seat - 1] == 0){            seats[seat - 1] = number;            printf("*seating*\n");            for(int i = 0; i < 10; ++i){                printf("%d ", seats[i]);            }            printf("\n");            printf("***************\n");            printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");            scanf("%d %d", &seat, &number);        }        else{            printf("Sorry, seat is taken.\n");            scanf("%d %d", &seat, &number);        }    }        printf("*seating*\n");    bubble_sort(seats, 10);    for(int i = 0; i < 10; ++i){        printf("%d ", seats[i]);    }    printf("\n");    printf("***************\n");        return 0;}

当number != 0时做以下的事,这是我第一眼看见程式码得到最直接的资讯,其实在上几行印出来的讯息中有提到

printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");

所以我可以得知说继续执行whlie做的事是由使用者输入的数值决定的。

while(number != 0){    if(seats [seat - 1] == 0){        seats[seat - 1] = number;        printf("*seating*\n");        for(int i = 0; i < 10; ++i){            printf("%d ", seats[i]);        }        printf("\n");        printf("***************\n");        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");        scanf("%d %d", &seat, &number);    }    else{        printf("Sorry, seat is taken.\n");        scanf("%d %d", &seat, &number);    }}

那往下看if(seats [seat - 1] == 0)里面又做了一堆事,但我们可以先看else的部分(因为感觉内容不多),else里面是告诉你位置被拿走了,然后又重新输入一次seat和number,回头再看if内的判断式,可以得知0这个数字代表的意思是位置是空的,重新再诠释一遍的话就是当位置是空的时候做下面的事否则重新再输入一次位置和数值。

    if(seats [seat - 1] == 0){        seats[seat - 1] = number;        printf("*seating*\n");        for(int i = 0; i < 10; ++i){            printf("%d ", seats[i]);        }        printf("\n");        printf("***************\n");        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");        scanf("%d %d", &seat, &number);    }    else{        printf("Sorry, seat is taken.\n");        scanf("%d %d", &seat, &number);    }

那if内到底在做什么呢? seats[seat - 1] = number这里很直觉是指把输入的值塞到seat-1这个空的座位里,-1的原因是阵列起始索引是从0开始的,接下来到for迴圈里把所有的值印出来,最后再输入一次新的位置和数值。

        seats[seat - 1] = number;        printf("*seating*\n");        for(int i = 0; i < 10; ++i){            printf("%d ", seats[i]);        }        printf("\n");        printf("***************\n");        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");        scanf("%d %d", &seat, &number);

可以请你仔细想一下这三件事情其实是各自独立的,但他们全部连在一起的时候会混淆他们之间的关係,增加阅读的困难,偷偷跟你分享个小技巧,加几个enter会更容易区分他们。

        seats[seat - 1] = number;                printf("*seating*\n");        for(int i = 0; i < 10; ++i){            printf("%d ", seats[i]);        }                printf("\n");        printf("***************\n");        printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");        scanf("%d %d", &seat, &number);

终于把整个while内在做的事搞懂了,再回头看完整的程式码后,不知道你有没有感觉到有些片段好像重複出现过了像是印出阵列内的所有值和请使用者输入seat和number。

    printf("*seating*\n");    for(int i = 0; i < 10; ++i){        printf("%d ", seats[i]);    }
    printf("\n");    printf("***************\n");    printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");    scanf("%d %d", &seat, &number);

当出现这种情形,代表我们可以新增函式来取代重複的事情,新函式的名称我觉得用showSeats()跟getUserInput()这样最直接明白,当然你也可以有自己的想法,但切记要让别人一看就懂,不要用只有自己看得懂的意思,接下来我们来改改看。

void showSeats(int seats[], int length){    printf("*seating*\n");    for(int i = 0; i < length; ++i){        printf("%d ", seats[i]);    }}void getUserInput(int& seat, int& number){    printf("\n");    printf("***************\n");    printf("Please input the seat (0~9) and number(-1 to end game)\n");    scanf("%d %d", &seat, &number);}int main(){    int seats[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};    int i, j, seat, number;    showSeats(seats, 10);        getUserInput(seat, number);        while(number != 0){        if(seats [seat - 1] == 0){            seats[seat - 1] = number;                        showSeats(seats, 10);                        getUserInput(seat, number);        }        else{            printf("Sorry, seat is taken.\n");            scanf("%d %d", &seat, &number);        }    }        bubble_sort(seats, 10);        showSeats(seats, 10);        printf("\n");    printf("***************\n");        return 0;}

http://img2.58codes.com/2024/emoticon24.gif挖~~看看我们改完后的结果多么令人舒畅,while迴圈内是不是一看就知道他想做什么了,虽然还有些地方可以表达得更清楚但已经比一开始看不出个毛来好太多了,而且这样做的好处有很多,除了增加阅读性之外,bug也方便查找,如果之后想要更换显示阵列的方式为从尾巴开始,只需要在showSeats里修改就可以了,最重要的是你帮助别人省去看程式码的时间,因为你已经写在函式上了,相信看你程式码的人会感受到你的心意。http://img2.58codes.com/2024/emoticon47.gif

下一篇我会带你把整个main里可以优化的地方重新组织一下,让整个输入数值到阵列的前置作业更清楚,感谢你陪我到这里,相信这段过程会对你有帮助,我们下次见^ ^


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章