自己看,CPP上的代码!看不明白请看CPP第五版中文版407页到408页
CPP第五版英语版509页到510页
最好是这章全看一篇,我是自学了,楼主你问我枚举是什么,我只能告诉你
不懂才问版主以经告诉过你一次了,本掌柜就不多说什么了
https://bbs.bccn.net/thread-455549-1-1.html
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
enum spectrum {red, orange, yellow, green, blue, violet};
const char * colors[] = {"red", "orange", "yellow",
"green", "blue", "violet"};
#define LEN 30
int main(void)
{
char choice[LEN];
enum spectrum color;
bool color_is_found = false;
puts("Enter a color (empty line to quit):");
while (gets(choice) != NULL && choice[0] != '\0')
{
for (color = red; color <= violet; color++)
{
if (strcmp(choice, colors[color]) == 0)
{
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 0;
}