设为首页 加入收藏

TOP

第 14 章 结构和其他数据形式(enum枚举)
2018-10-21 14:13:58 】 浏览:45
Tags:结构 其他 数据 形式 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] != '\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.\n", 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, '\n'))
77             *find = '\0';
78         else
79             while (fgetc(stdin) != '\n') continue;
80     }
81 
82     return ret_val;
83 }
enum.c

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇ESP32 学习笔记 - Ubuntu安装 下一篇第 14 章 结构和其他数据形式(函..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目