变数命名的善意

这是一小段泡沫排序法的程式码,可是在说什么呢?你可以试着阅读他,但千万不要勉强自己。

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

http://img2.58codes.com/2024/emoticon06.gif如果你跟我一样,看到n1、n2那边就受不了的,我能了解你的感受,但是目前的资讯量还不够给这两个变数一个合适的命名,往下看也许可以找到答案。

    int arr[10] = {99, 0, 10, 31, 0, 42, 70, 67, 0, 0};    int i, j, n1, n2;    printf("*seating*\n");    for(int i = 0; i < 10; ++i){        printf("%d ", arr[i]);    }

这段程式码有排序前的阵列,四个之后会用到的变数i、j、n1、n2以及印出arr阵列内所有的值,可能一开始只是想展示arr有什么东西吧。


    printf("\n");    printf("***************\n");    printf("Please input the seat (1~10) and 2-digit number(0 to end game)\n");    scanf("%d %d", &n1, &n2);

喔~这边就明白多了,提示使用者输入seat和number,代表可能会在阵列中尝试放进一个值,这里告诉我们n1代表插入阵列的位置,n2代表插入的值,我们可以用seat代表n1,number代表n2。

    int arr[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 ", arr[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);

修改过后总算有点理解他想表达的事情了,首先先印出排序前阵列的所有值,再来提示使用者输入插入的位置和数值,但还有一个地方我不太满意的就是arr这个阵列的命名,既然都用seat当作位置的索引了,我想用seats(一堆位置)来取代arr可以更明确表达意图,也许你有其他想法也可以提供,但目前我想先用seats来重新修改一下全部的程式码。


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;}

http://img2.58codes.com/2024/emoticon34.gif这样舒服多了,不用再去想n1代表索引、n2代表数字,程式码直接表达出他的意图,让我们可以更轻鬆阅读他,小细节的修改带来的便利性是会积累的,也能让阅读的人感受到你的善意,下一篇我们会往while迴圈内探索(老实说一开始我没看懂while内在做什么XD),感谢你陪我到这里,我们下次见^ ^

下一篇传送门


关于作者: 网站小编

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

热门文章