[C] char**, swich-case, refactoring, stdbool.h

 

#include <stdio.h>
int main() {
    char *names[] = {"ant", "tiger", "dog", "orange", "apple", "banana"}, **p = names;
    int i;
    for (i = 0; i < 6; ++i, p++) {
        char ch = *(*p);
        switch (ch) {
            case 'a': case 'e': case 'i': case 'o': case 'u': printf("I'm an %s\n", *p); break;
            default: printf("I'm a %s\n", *p);
        }
    }
    return 0;
}

 

运行结果

I'm an ant
I'm a tiger
I'm a dog
I'm an orange
I'm an apple
I'm a banana

 

 

Refactoring

#include <stdio.h>
#include <stdbool.h>

bool BoolTest(char p) {
    bool result;
    switch (p) {
        case 'a': case 'e': case 'i': case 'o': case 'u': result = true; break;
        default: result = false;
    }
    return result;
}

int main() {
    char *names[] = {"ant", "tiger", "dog", "orange", "apple", "banana"}, **p = names;
    int i;
    for (i = 0; i < 6; ++i, p++) {
        printf("I'm %s %s\n", BoolTest(*(*p))?"an":"a", *p);
    }
    return 0;
}

 

posted @ 2020-07-02 10:09  profesor  阅读(173)  评论(0编辑  收藏  举报