結構體-簡單列舉型別——植物與顏色

HowieLee59發表於2019-03-24

Problem Description

 請定義具有red, orange, yellow, green, blue, violet六種顏色的列舉型別color,根據輸入的顏色名稱,輸出以下六種植物花朵的顏色:

Rose(red), Poppies(orange), Sunflower(yellow), Grass(green), Bluebells(blue), Violets(violet)。如果輸入的顏色名稱不在列舉型別color中,例如輸入purple,請輸出I don't know about the color purple.

 

Input

 輸入資料有多行,每行有一個字串代表顏色名稱,顏色名稱最多30個字元,直到檔案結束為止。

Output

 輸出對應顏色的植物名稱,例如:Bluebells are blue. 如果輸入的顏色名稱不在列舉型別color中,例如purple, 請輸出I don't know about the color purple.

 

Sample Input

blue
yellow
purple

Sample Output

Bluebells are blue.
Sunflower are yellow.
I don't know about the color purple.

Hint

 請用列舉型別實現。

#include <stdio.h>
#include <string.h>
int main()
{
    char str[20];
    enum color{red,orange,yellow,green,blue,violet};
    while (gets(str))
    {
        if(strcmp(str,"red")==0)
            printf("Rose are red.\n");
        else if(strcmp(str,"orange")==0)
            printf("Poppies are orange.\n");
        else if(strcmp(str,"yellow")==0)
            printf("Sunflower are yellow.\n");
        else if(strcmp(str,"green")==0)
            printf("Grass are green.\n");
        else if(strcmp(str,"blue")==0)
            printf("Bluebells are blue.\n");
        else if(strcmp(str,"violet")==0)
            printf("Violets are violet.\n");
        else
            printf("I don't know about the color %s.\n",str);

    }
    return 0;
}

 

相關文章