当前位置: 代码迷 >> 综合 >> sdut 1959 简单枚举类型——植物与颜色
  详细解决方案

sdut 1959 简单枚举类型——植物与颜色

热度:32   发布时间:2023-11-22 14:36:31.0

题目链接
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.
Example Input
blue
yellow
purple
Example Output
Bluebells are blue.
Sunflower are yellow.
I don’t know about the color purple.
使用枚举类型的时候要用强制类型转换

#include<iostream>
using namespace std;
typedef enum color{
    red, orange, yellow, green, blue, violet
}color;
int main(){
    string s;while(cin>>s){
    int co;if(s=="red")co = 0;else if(s=="orange")co = 1;else if(s=="yellow")co = 2;else if(s=="green")co = 3;else if(s=="blue")co = 4;else if(s=="violet")co = 5;elseco = 6;switch((color) co){
    //强制类型转换为color类型case 0:cout<<"Rose are red."<<endl;break;case 1:cout<<"Poppies are orange."<<endl;break;case 2:cout<<"Sunflower are yellow."<<endl;break;case 3:cout<<"Grass are green."<<endl;break;case 4:cout<<"Bluebells are blue."<<endl;break;case 5:cout<<"Violets are violet."<<endl;break;default:cout<<"I don't know about the color "<<s<<"."<<endl;break;}}return 0;
}