C 中的 Switch 语句

发布于 2024-12-11 10:33:18 字数 643 浏览 0 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

最笨的告白 2024-12-18 10:33:18

你不能——C 中的 switch 语句仅适用于基本类型,不适用于字符串。例如,您可以使用哈希表或搜索树来优化匹配,但对于只有 20 个选项可能不值得这么麻烦。

为了清理代码,您可以做的是设置一个映射表:

struct str2Num {
    char *str;
    int num;
};

const struct str2Num registerMap[] = {
    { "zero", 00000 },
    { "at", 00001 },
    { "v0", 00010 },
    { "v1", 00011 },
    { NULL, 0 }  /* end marker */
};

并像这样进行匹配:

int i;
for (i = 0; registerMap[i].str != NULL; i++) {
    if (strcmp(registerName, registerMap[i].str) == 0) {
        return registerMap[i].num;
    }
}
/* handle no-match case here */

事实上,如果您按字母顺序对表进行排序,您甚至可以使用 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:

struct str2Num {
    char *str;
    int num;
};

const struct str2Num registerMap[] = {
    { "zero", 00000 },
    { "at", 00001 },
    { "v0", 00010 },
    { "v1", 00011 },
    { NULL, 0 }  /* end marker */
};

and do your matching like this:

int i;
for (i = 0; registerMap[i].str != NULL; i++) {
    if (strcmp(registerName, registerMap[i].str) == 0) {
        return registerMap[i].num;
    }
}
/* handle no-match case here */

In fact, if you sorted the table alphabetically, you could even use bsearch() for fast matching.

你在看孤独的风景 2024-12-18 10:33:18

您只能打开整数,因此这不起作用。

如果您所做的只是将字符串转换为 int,请将信息存储在数组中并查看它。

struct {
   const char *name;
   int value;
} fooMapping[] = {
     {"zero",0},
     {"at",1}
      ....
};

int foo2value(const char *name)
{
     size_t i;
     for(i = 0; i < sizeof fooMapping/sizeof fooMapping[0]; i++) {
          if(strcmp(name, fooMapping[i].name) == 0) 
             return fooMapping[i].value;

     }
    return -1;
}

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.

struct {
   const char *name;
   int value;
} fooMapping[] = {
     {"zero",0},
     {"at",1}
      ....
};

int foo2value(const char *name)
{
     size_t i;
     for(i = 0; i < sizeof fooMapping/sizeof fooMapping[0]; i++) {
          if(strcmp(name, fooMapping[i].name) == 0) 
             return fooMapping[i].value;

     }
    return -1;
}
∞梦里开花 2024-12-18 10:33:18

在 switch 中,

switch(number) {

case 1;
case 2;
case 7;
}

您基本上是说,如果 number = 1,则情况 1。如果 number = 7,则情况 7。所以您需要做的是分配每个文本值,在您的情况下为“zero”“at”“v0”和“v1”,您需要将它们放入一个数组中,并且在 switch 语句中,您将切换一个与您拥有的任何文本的索引号相对应的整数,而不是 switch(number) 。因此,如果 array[3] was = "v0",您将向索引号 (3) 分配一个整数,然后再 switch(integer)。希望这有帮助。

In a switch,

switch(number) {

case 1;
case 2;
case 7;
}

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.

ま柒月 2024-12-18 10:33:18

为什么不使用?像这样的运算符:

return
    strcmp(registerName, "zero")? 00000:
    strcmp(registerName, "at")  ? 00001:
    strcmp(registerName, "v0")  ? 00010:
    strcmp(registerName, "v1")  ? 00011:
    ...

Why not use the ? operator like so:

return
    strcmp(registerName, "zero")? 00000:
    strcmp(registerName, "at")  ? 00001:
    strcmp(registerName, "v0")  ? 00010:
    strcmp(registerName, "v1")  ? 00011:
    ...
一枫情书 2024-12-18 10:33:18

由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文