公 告

欢迎各位网友添加友情链接,在您添加本博客:http://linux0818.blogspot.com/ 做为链接后, E-mail:linux0818@gmail.com给我,我将将您的网址添加到本博客。

2008年10月23日星期四

在linux中用gcc时switch语句的另一种简洁写法

 源代码:
#include <stdio.h>
int main(int argc, char **argv)
{
    int word = 0;
    while (word != 1111) {
        printf("Please input a integer:\n");
        scanf("%d", &word);
        printf("word = %d\n", word);
        switch (word) {
        case 0...20:            //注意格式是:0+空格+...+空格+20
            printf("The word(0-20) is %d\n", word);
            break;
        case 21...40:
            printf("The word(21-40) is %d\n", word);
            break;
        case 41...60:
            printf("The word(41-60) is %d\n", word);
            break;
        case 61...80:
            printf("The word(61-80) is %d\n", word);
            break;
        case 81...100:
            printf("The word(81-100) is %d\n", word);
            break;
        default:
            printf("The word > 100\n");
            break;
        }
    }
    return 0;
}
编译:gcc -Wall switchcase.c
运行:. /a.out
结果:
Please input a integer:
12
word = 12
The word(0-20) is 12
Please input a integer:
34
word = 34
The word(21-40) is 34
Please input a integer:
78
word = 78
The word(61-80) is 78
Please input a integer:
120
word = 120
The word > 100
Please input a integer:
1111
word = 1111
The word > 100



--
/**************************************/
Name: Xiong Feng
E-mail:linux0818@gmail.com
MSN:linux0818@hotmail.com
QQ:23562033
Address: GuangZhou.China
/**************************************/

没有评论:

发表评论