可视化枚举值 [英] Visualize enum value

查看:90
本文介绍了可视化枚举值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可视化枚举类型的食物类型?

How can i visualize the types of food with type enum?

typedef struct cat{
 int code;
 int age;
 float weight;
 enum {kibbles,canned_food,tuna_fish}food;
} cats;

int n,i;

printf("Insert a number: ");
scanf("%d",&n);

cats *cat_arr = calloc(n, sizeof(cats));

for(i = 0;i<n;i++){
    printf("Code: ");
    scanf("%d",&cat_arr[i].code);
    printf("Age: ");
    scanf("%d",&cat_arr[i].age);
    printf("weight: ");
    scanf("%f",&cat_arr[i].weight);
    printf("Food: ");
    scanf("%d",&cat_arr[i].food);
}


for(i=0;i<n;i++){
    if(cat_arr[i].age < 4 && cat_arr[i].weight > avg){
        printf("%d %s",cat_arr[i].code,cat_arr[i].food);   <---- HERE
    }
}

我想要插入类型带有数字的食物,并显示我用枚举分配的名称。

I want insert type of food with numbers and display the name that i assigned with enum.

推荐答案

没有自动方法来获取与枚举常量。您需要创建枚举到它们的字符串等效项的映射,然后引用它。

There's no automatic way to get the string equivalent of an enum constant. You'll need to create a mapping of enums to their string equivalents and reference that.

const char *food_names[] = { "kibbles" , "canned_food" ,"tuna_fish" };

...

printf("%d %s", cat_arr[i].code, food_names[cat_arr[i].food]); 

这篇关于可视化枚举值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆