C 中的 Switch 语句
我有以下 if 语句:
if (strcmp(registerName, "zero"))
return 00000;
else if (strcmp(registerName, "at"))
return 00001;
else if (strcmp(registerName, "v0"))
return 00010;
else if (strcmp(registerName, "v1"))
return 00011;
它实际上很长 - 大约 20 个 if 语句。因此,我想使用开关。当每个语句都有不同的条件时,如何将其转换为 switch ?
我尝试了以下代码,但它不起作用:
int registerAddress;
switch(registerAddress) {
case 1 (strcmp(registerName, "zero")):
regsiterAddress = 00000;
break;
case 2 (strcmp(registerName, "at")):
registerAddress = 00001;
break;
}
I have the following if-statements:
if (strcmp(registerName, "zero"))
return 00000;
else if (strcmp(registerName, "at"))
return 00001;
else if (strcmp(registerName, "v0"))
return 00010;
else if (strcmp(registerName, "v1"))
return 00011;
It's actually really long - about 20 if-statements. Therefore, I would like to use a switch. How would I convert that to switch when each statement has a different condition?
I tried something as the code below, but it does not work:
int registerAddress;
switch(registerAddress) {
case 1 (strcmp(registerName, "zero")):
regsiterAddress = 00000;
break;
case 2 (strcmp(registerName, "at")):
registerAddress = 00001;
break;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
你不能——C 中的
switch
语句仅适用于基本类型,不适用于字符串。例如,您可以使用哈希表或搜索树来优化匹配,但对于只有 20 个选项可能不值得这么麻烦。为了清理代码,您可以做的是设置一个映射表:
并像这样进行匹配:
事实上,如果您按字母顺序对表进行排序,您甚至可以使用
bsearch()
用于快速匹配。You can't —
switch
statements in C only work on primitive types, not on strings. You could use, say, a hash table or a search tree to optimize the matching, but for only 20 options that may not be worth the trouble.What you could do, to clean up the code, is set up a mapping table:
and do your matching like this:
In fact, if you sorted the table alphabetically, you could even use
bsearch()
for fast matching.您只能打开整数,因此这不起作用。
如果您所做的只是将字符串转换为 int,请将信息存储在数组中并查看它。
You can only switch on integers, so this will not work.
If all you're doing is converting a string to an int, store the info in an array and look through it.
在 switch 中,
您基本上是说,如果 number = 1,则情况 1。如果 number = 7,则情况 7。所以您需要做的是分配每个文本值,在您的情况下为“zero”“at”“v0”和“v1”,您需要将它们放入一个数组中,并且在 switch 语句中,您将切换一个与您拥有的任何文本的索引号相对应的整数,而不是 switch(number) 。因此,如果 array[3] was = "v0",您将向索引号 (3) 分配一个整数,然后再 switch(integer)。希望这有帮助。
In a switch,
you are basically saying, if number = 1, then case 1. If number = 7, case 7. So what you need to do is assign each text value, in your case "zero""at""v0" and "v1", you would need to put these into an array, and in the switch statement, instead of switch(number) you would switch an integer that would correspond with the index number of whichever text you had. So if array[3] was = "v0", you would assign an integer to the index number (3) and then switch(integer). Hope this helped.
为什么不使用?像这样的运算符:
Why not use the ? operator like so:
由于 switch-case 仅适用于数字或单个字符,因此我会使用 GNU 的 gperf 之类的工具创建一个 完美哈希 并打开该值(后跟 strcmp()以确保完全匹配)。这应该会给您带来所需的性能改进。
Since switch-case only works with numbers or single chars, I would use a tool like GNU's gperf to create a perfect hash and switch on that value (followed by a strcmp() to be certain of an exact match). That ought to give you the desired performance improvement.