欢迎来到贱贱的博客

扩大
缩小

枚举练习

 1 /* enum.c -- uses enumerated values */
 2 #include <stdio.h>
 3 #include <string.h>    // for strcmp()
 4 #include <stdbool.h>   // C99 feature
 5 
 6 enum spectrum {red, orange, yellow, green, blue, violet};
 7 const char * colors[] = {"red", "orange", "yellow",
 8                          "green", "blue", "violet"};
 9 #define LEN 30
10 
11 int main(void)
12 {
13     char choice[LEN];
14     enum spectrum color;
15     bool color_is_found = false;
16 
17     puts("Enter a color (empty line to quit):");
18     while (gets(choice) != NULL && choice[0] != '\0')
19     {
20         for (color = red; color <= violet; color++)
21         {
22             if (strcmp(choice, colors[color]) == 0)
23             {
24                 color_is_found = true;
25                 break;
26             }
27         }
28         if (color_is_found)
29             switch(color)
30             {
31                 case red    : puts("Roses are red.");
32                               break;
33                 case orange : puts("Poppies are orange.");
34                               break;
35                 case yellow : puts("Sunflowers are yellow.");
36                               break;
37                 case green  : puts("Grass is green.");
38                               break;
39                 case blue   : puts("Bluebells are blue.");
40                               break;
41                 case violet : puts("Violets are violet.");
42                               break;
43             }
44         else
45             printf("I don't know about the color %s.\n", choice);
46         color_is_found = false;
47         puts("Next color, please (empty line to quit):");
48     }
49     puts("Goodbye!");
50     
51     return 0;
52 }

 

posted on 2017-03-01 23:24  L的存在  阅读(239)  评论(0编辑  收藏  举报

导航