将字符串输入与枚举数据类型进行比较 [英] Comparing string input to enum data type

查看:77
本文介绍了将字符串输入与枚举数据类型进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候。


我正在完成一项任务,似乎无法获得正确的概念

我想要的东西使用枚举数据类型。


我在代码中定义了以下内容:


枚举颜色{红色,蓝色,绿色,黄色,黑色,紫色,粉红色};


我现在正在做的是从文本文件中读取一个字符串(char *)。我想要比较读入数组的值,并确定枚举数据类型中的颜色是什么。如何比较像

" red"这样的字符串?查看它是否存在于

枚举数据类型''color''的一部分中?我知道我需要写一些

比较函数,但是我无法弄清楚

应该如何工作。

David

解决方案

文章< 11 ************** ********@t69g2000cwt.googlegroups .com> ;,
dt ******* @ gmail.com < dt ******* @ gmail.comwrote:


>我如何比较像
"红色"查看它是否存在于
枚举数据类型'颜色的一部分的值中?



你不是。当你运行程序时,枚举值分配给名称的名称不再具有意义;在这一点上,

C纯粹用它们的数值工作。


在你的代码中,你可以准备一个名字

并与之比较。


可能有一种方法可以使用宏来命名枚举

常量和字符串化 ;并将名称存放在同一个

时间,但如果有,那么它现在就逃脱了。

-

所有是虚荣。 - 传道书


2月27日晚上8:45,dtscho ... @ gmail.com < dtscho ... @ gmail.comwrote:


问候。


我正在完成作业并且可以'对于我正在尝试使用枚举数据类型的东西,似乎得到了正确的概念




我在代码中定义了以下内容:


枚举颜色{红色,蓝色,绿色,黄色,黑色,紫色,粉红色};


我现在正在做的是阅读从文本文件中获取字符串(char *)。我想要比较读入数组的值,并确定枚举数据类型中的颜色是什么。如何比较像

" red"这样的字符串?查看它是否存在于

枚举数据类型''color''的一部分中?我知道我需要写一些

比较函数,但是我无法弄清楚

应该如何工作。


如果你把字符串变成a,你要设置c:


if(0 == strncmp(" red" ,a,sizeofred))

c =红色;

如果......

-

Bill Pursell


2月27日下午1:16,Bill Pursell < bill.purs ... @ gmail.comwrote:


>

如果你把字符串变成a,并且你想设置c:


if(0 == strncmp(" red",a,sizeof" red"))

c = red ;

否则如果......


-

Bill Pursell



好​​的,所以这会带来别的东西,你所拥有的部分


sizeof" red"


通常我有用它作为sizeof(name)和()'s。这种格式

与"一起使用枚举数据类型吗?我不熟悉使用该运算符的这种

方式。


返回什么,基于

在列表中的位置?


David


Greetings.

I am working on an assignment and can''t seem to get the right concept
for something I''m attempting to do with enum data types.

I have defined the following in my code:

enum color {red, blue, green, yellow, black, purple, pink};

What I am doing now is reading from a text file a string (char *). I
wish to compare the value read into the array and determine which
color it is from the enum data type. How can I compare a string like
"red" to see if it exists in the values that are part of the
enumerated data type ''color''? I know I need to write some kind of
compare function, but I''m not getting anywhere figuring out how that
should work.

David

解决方案

In article <11**********************@t69g2000cwt.googlegroups .com>,
dt*******@gmail.com <dt*******@gmail.comwrote:

>How can I compare a string like
"red" to see if it exists in the values that are part of the
enumerated data type ''color''?

You don''t. By the time you run the program, the names assigned
to the enumerated values no longer have meaning; at that point,
C is working purely with their numeric values.

In your code, you can prepare a list of strings with the names
and compare against that.

There just might be a way to use a macro to name the enumeration
constant and to "stringify" and store away the name at the same
time, but if there is then it escapes me at the moment.
--
All is vanity. -- Ecclesiastes


On Feb 27, 8:45 pm, "dtscho...@gmail.com" <dtscho...@gmail.comwrote:

Greetings.

I am working on an assignment and can''t seem to get the right concept
for something I''m attempting to do with enum data types.

I have defined the following in my code:

enum color {red, blue, green, yellow, black, purple, pink};

What I am doing now is reading from a text file a string (char *). I
wish to compare the value read into the array and determine which
color it is from the enum data type. How can I compare a string like
"red" to see if it exists in the values that are part of the
enumerated data type ''color''? I know I need to write some kind of
compare function, but I''m not getting anywhere figuring out how that
should work.

If you''ve red the string into a, and you want to set c:

if( 0 == strncmp("red", a, sizeof "red") )
c = red;
else if ...
--
Bill Pursell


On Feb 27, 1:16 pm, "Bill Pursell" <bill.purs...@gmail.comwrote:

>
If you''ve red the string into a, and you want to set c:

if( 0 == strncmp("red", a, sizeof "red") )
c = red;
else if ...

--
Bill Pursell

OK, so this brings up something else, the part where you have

sizeof "red"

Normally I have used it as sizeof(name) with the ()''s. Is this format
with ""''s used with enumerated data types? I am not familiar with this
way of using that operator.

What does this return, the int value of the enum type based on the
position in the list?

David


这篇关于将字符串输入与枚举数据类型进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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