在C中实现检查功能

时间:2016-06-02 19:43:50

标签: c function methods conventions

我刚写了这段代码:

int is_write_mode(char * mode) {
    return !strcmp(mode, "wb");
}

int is_read_mode(char * mode) {
    return !strcmp(mode, "rb");
}

int is_valid_number(int number) {
    return number >= 0 && number <= MAX_LEN_STUDENTS;
}

int is_valid_grade(float grade) {
    return (grade >= MIN_GRADE && grade <= MAX_GRADE) || !grade;
}

更多像这样的函数来缩短long if语句并使代码更具可读性。我现在在想是否应该实现这些功能:

#define WRITE_MODE(x) (!strcmp(x, "wb"))
#define READ_MODE(x) (!strcmp(x, "rb"))
#define VALID_NUM(x) (x >= 0 && x <= MAX_LEN_STUDENTS)
#define VALID_GRADE(x) ((x >= MIN_GRADE && x <= MAX_GRADE) || !x)

您能否告诉我并告知我应该如何实施这类功能。提前谢谢!

0 个答案:

没有答案