/*-----------------------------
enum.c -- 使用枚举类型的值
-----------------------------*/ #include <stdio.h>
#include <string.h>
//#include <stdbool.h> //C99特性 #define LEN 30 char* s_gets(char *st, int n); enum spectrum {red, orange, yellow, green, blue, violet};
const char *colors[] = {"red", "orange", "yellow", "green", "blue", "violet"}; int main()
{
char choice[LEN];
int color;
bool color_is_found = false; puts("Enter a color (empty line to quit):"); while (s_gets(choice, LEN) != NULL && choice[] != '\0')
{
for (color = red; color != violet; ++color)
{
if (strcmp(choice, colors[color]) == )
{
color_is_found = true;
break;
}
} if (color_is_found)
switch (color)
{
case red:
puts("Roses are red.");
break;
case orange:
puts("Poppies are orange.");
break;
case yellow:
puts("Sunflowers are yellow.");
break;
case green:
puts("Grass is green");
break;
case blue:
puts("Bluebells are blue");
break;
case violet:
puts("Violets are violet");
break;
}
else
printf("I don't know about the color %s.\n", choice); color_is_found = false; puts("Next color, please (empty line to quit):");
} puts("Goodbye"); return ;
} char* s_gets(char *st, int n)
{
char *ret_val, *find; if (ret_val = fgets(st, n, stdin))
{
if (find = strchr(st, '\n'))
*find = '\0';
else
while (fgetc(stdin) != '\n') continue;
} return ret_val;
}
enum.c