switch语句 - ANSI-C [英] switch statement - ANSI-C

查看:89
本文介绍了switch语句 - ANSI-C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




一个简单的问题:在ANSI-C中,如何为switch语句指定一个字符串作为表达式




例如


switch(mystr){

case" ABC" :bleh ......

休息;

case" DEF" :bleh

休息''

默认值:bleh;

休息;

}


AFAIK,表达式必须产生一个整数值。在

字符串的情况下,解决方法是什么?


Aaron

Hi,

A simple question: In ANSI-C, how to specify a string as the expression
for switch statement.

eg

switch (mystr) {
case "ABC" : bleh...
break;
case "DEF" : bleh
break''
default: bleh;
break;
}

AFAIK, the expression must produce an integer value. In the case of a
string, what is the work-around?

Aaron

推荐答案

问:如何打开字符串?


简单的答案是:

a)不要使用开关。使用if / else和strcmp

b)创建一个可能的字符串数组并使用查找函数

返回字符串的索引(或-1)并打开。


我面对这类事情的最常见的时间是解析配置文件

for the token。我采用的方法是增加上面的简单答案

b),使用由与字符串相关联的索引枚举的结构组成的结构。枚举可以方便地添加新的

键,而不必担心索引号而不是

案例1:,案例2:更新。这里有几个字符串的示例:


typedef enum

{

eKEY_HOST = 0,

eKEY_PORT,/ *隐含= 1 ...等* /

eKEY_PROTO,

eNUM_KEYS,/ *有效密钥数量* /

eKEY_INVALID

} cfg_key_names;


/ *将枚举(数值)值映射到字符串的结构数组* /

struct {

cfg_key_names id;

const char * str;

} cfg_keys [] = {

{eKEY_HOST," host" },

{eKEY_PORT," port" },

{eKEY_PROTO," proto" }

};


/ *自动获取上述数组中的条目数* /

int n_cfg_keys = sizeof( cfg_keys)/ sizeof(cfg_keys [0]);


/ *查找函数* /

int parse_cfg_key(const char * str)

{

int i;

for(i = 0; i< n_cfg_keys; i ++)

if(strcmp(str, cfg_keys [i] .str)== 0)

返回cfg_keys [i] .id;


返回eKEY_INVALID;

}


开关(parse_cfg_key(some_str))

{

案例eKEY_HOST:

/ * ... * /

休息;

默认:

案例eKEY_INVALID:

/ *没有为你服从! * /

休息;

}


希望有所帮助。

Q: How do I switch on strings?

The simple answers are:
a) don''t use switch. use if/else with strcmp
b) make an array of possible strings and use a lookup function to
return the index of your string (or -1) and switch on that.

The most common time I face this sort of thing is parsing config files
for tokens. The approach I take is an enhancement to the simple answer
b) above, using a structure consisting of an enumeration of index
values associated with the string. The enum makes it handy to add new
keys without having to worry about index numbers as opposed to having
case 1:, case 2: to go update. Here''s an example for a few strings:

typedef enum
{
eKEY_HOST = 0,
eKEY_PORT, /* implicitly = 1 ... etc */
eKEY_PROTO,
eNUM_KEYS, /* number of valid keys */
eKEY_INVALID
} cfg_key_names;

/* array of structures to map the enum (numerical) values to strings */
struct {
cfg_key_names id;
const char *str;
} cfg_keys[] = {
{ eKEY_HOST, "host" },
{ eKEY_PORT, "port" },
{ eKEY_PROTO, "proto" }
};

/* get the number of entries in the above array automatically */
int n_cfg_keys = sizeof(cfg_keys) / sizeof(cfg_keys[0]);

/* lookup function */
int parse_cfg_key(const char *str)
{
int i;
for (i = 0; i < n_cfg_keys; i++)
if (strcmp(str, cfg_keys[i].str) == 0)
return cfg_keys[i].id;

return eKEY_INVALID;
}

switch (parse_cfg_key(some_str))
{
case eKEY_HOST:
/* ... */
break;
default:
case eKEY_INVALID:
/* NO STRING FOR YOU! */
break;
}

Hope that helps.

< br>

aa******@yahoo.com.au 说:




一个简单的问题:在ANSI-C中,如何指定字符串作为表达式

表示switch语句。
Hi,

A simple question: In ANSI-C, how to specify a string as the expression
for switch statement.



你不能,但继续阅读,因为有办法解决这个问题。

You can''t, but keep reading, because there is a way around this.


例如


switch(mystr){

case" ABC" :bleh ......

休息;

case" DEF" :bleh

休息''

默认值:bleh;

休息;

}


AFAIK,表达式必须产生一个整数值。在

字符串的情况下,解决方法是什么?
eg

switch (mystr) {
case "ABC" : bleh...
break;
case "DEF" : bleh
break''
default: bleh;
break;
}

AFAIK, the expression must produce an integer value. In the case of a
string, what is the work-around?



将每个字符串与一个整数(这里的结构很好)相关联,最好是某种排序容器中的
(排序数组,或BST,或哈希表,或

类似的东西),以使检索更快。查找字符串,找到

相关的数字,然后打开它。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh位于上述域名, - www。

Associate each string with an integer (a struct works well here), preferably
in a sorted container of some kind (sorted array, or BST, or hash table, or
something of the kind), to make retrieval quicker. Look up the string, find
the associated number, and switch on that instead.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


" typedef" < ty ************ @ gmail.comwrote:
"typedef" <ty************@gmail.comwrote:

问:如何打开字符串?


简单的答案是:

a)不要使用开关。使用if / else和strcmp

b)创建一个可能的字符串数组并使用查找函数

返回字符串的索引(或-1)并打开。
Q: How do I switch on strings?

The simple answers are:
a) don''t use switch. use if/else with strcmp
b) make an array of possible strings and use a lookup function to
return the index of your string (or -1) and switch on that.



c)使用哈希值。


Richard

c) Use a hash.

Richard


这篇关于switch语句 - ANSI-C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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