您好,欢迎访问一九零五行业门户网

技巧:巧用数组减少if语句

/* * filename: odd_or_even.cpp * author: antigloss at http://stdcpp.cn * lastmodifieddate: 2005-7-22 22:30 * purpose: tell if a given number is odd or even */ #include cstdlib // for exit_success #include iostream #include limits // for nu
 /*
 * filename:          odd_or_even.cpp
 * author:            antigloss at http://stdcpp.cn
 * lastmodifieddate:  2005-7-22 22:30
 * purpose:           tell if a given number is odd or even
 */
#include     // for exit_success
#include
#include      // for numeric_limits
// flush the input buffer
inline void flush_stdin()
{
    std::cin.clear(); // clear error state of the stream
    // clear data left at the input buffer
    std::cin.ignore( std::numeric_limits::max(), '\n' );
} // end of flush_stdin
int main()
{
    long num;
    const char *msg[] = { even, odd };
    for (;;) {
        std::cout
        if ( std::cin >> num ) {
            std::cout         } else {
            std::cin.clear(); // clear error state before reading from the input stream
            if ( std::cin.get() == 'q' ) {
                flush_stdin();
                break;
            }
            std::cerr         }
        flush_stdin();
    }
    std::cout     std::cin.get();
    return exit_success;
}
其它类似信息

推荐信息