• 第 14 章 结构和其他数据形式(enum枚举)


     1 /*-----------------------------
     2     enum.c -- 使用枚举类型的值
     3 -----------------------------*/
     4 
     5 #include <stdio.h>
     6 #include <string.h>
     7 //#include <stdbool.h>    //C99特性
     8 
     9 #define LEN 30
    10 
    11 char* s_gets(char *st, int n);
    12 
    13 enum spectrum {red, orange, yellow, green, blue, violet};
    14 const char *colors[] = {"red", "orange", "yellow", "green", "blue", "violet"};
    15 
    16 int main()
    17 {
    18     char choice[LEN];
    19     int color;
    20     bool color_is_found = false;
    21 
    22     puts("Enter a color (empty line to quit):");
    23 
    24     while (s_gets(choice, LEN) != NULL && choice[0] != '')
    25     {
    26         for (color = red; color != violet; ++color)
    27         {
    28             if (strcmp(choice, colors[color]) == 0)
    29             {
    30                 color_is_found = true;
    31                 break;
    32             }
    33         }
    34 
    35         if (color_is_found)
    36             switch (color)
    37             {
    38             case red:
    39                 puts("Roses are red.");
    40                 break;
    41             case orange:
    42                 puts("Poppies are orange.");
    43                 break;
    44             case yellow:
    45                 puts("Sunflowers are yellow.");
    46                 break;
    47             case green:
    48                 puts("Grass is green");
    49                 break;
    50             case blue:
    51                 puts("Bluebells are blue");
    52                 break;
    53             case violet:
    54                 puts("Violets are violet");
    55                 break;
    56             }
    57         else
    58             printf("I don't know about the color %s.
    ", choice);
    59 
    60         color_is_found = false;
    61         
    62         puts("Next color, please (empty line to quit):");
    63     }
    64 
    65     puts("Goodbye");
    66 
    67     return 0;
    68 }
    69 
    70 char* s_gets(char *st, int n)
    71 {
    72     char *ret_val, *find;
    73 
    74     if (ret_val = fgets(st, n, stdin))
    75     {
    76         if (find = strchr(st, '
    '))
    77             *find = '';
    78         else
    79             while (fgetc(stdin) != '
    ') continue;
    80     }
    81 
    82     return ret_val;
    83 }
    enum.c

  • 相关阅读:
    Marshal's Confusion III(快速幂)
    两种筛素数的方法
    B
    HDU 1563 【Find your present!】
    HDU 2044【一只小蜜蜂】
    HDU 2153 仙人球的残影
    NYOJ 49 【开心的小明】
    最小的回文数
    Google Code Jam 2014资格赛【Problem A. Magic Trick】
    携程编程大赛 (预赛第二场)第一题【剪刀石头布】
  • 原文地址:https://www.cnblogs.com/web1013/p/9185235.html
Copyright © 2020-2023  润新知