如何在 C 中创建 !default 情况

发布于 2024-12-28 14:16:57 字数 502 浏览 0 评论 0原文

在微控制器程序上,我有一些指令,我想对开关中除默认值之外的每种情况执行这些指令。但是,我不想为每种情况编写函数调用或使用宏。

因为这是针对以 3-7Mhz 运行的微控制器,所以速度和代码空间非常重要。 例如:

switch(letter)
{
case a:
    ShowApple();
    printf("You entered an english letter.");
    break;
case b:
    ShowBananna();
    printf("You entered an english letter.");
    break;
...
case z:
    ShowZebra();
    printf("You entered an english letter.");
    break;
default:
    printf("You did not enter an english letter. Silly human!");
    break;
}

On a micro-controller program I have a few instructions that I would like to execute for every case in a switch except the default. I do not, however, want to write a function call or use a macro for every case.

Because this is for a micro-controller running at 3-7Mhz, speed and code space are important.
For example:

switch(letter)
{
case a:
    ShowApple();
    printf("You entered an english letter.");
    break;
case b:
    ShowBananna();
    printf("You entered an english letter.");
    break;
...
case z:
    ShowZebra();
    printf("You entered an english letter.");
    break;
default:
    printf("You did not enter an english letter. Silly human!");
    break;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

随风而去 2025-01-04 14:16:57
int was_default_picked;

was_default_picked = 0;
switch (letter)
{
// ...

default:
    was_default_picked = 1;
}

if (!was_default_picked)
{
    // Your logic goes here
}
int was_default_picked;

was_default_picked = 0;
switch (letter)
{
// ...

default:
    was_default_picked = 1;
}

if (!was_default_picked)
{
    // Your logic goes here
}
月棠 2025-01-04 14:16:57

我会为此下地狱...

switch (foo) {
default:
    /* code for default case */
    break;
    if (0) { case 'a':
        /* ... */
    } if (0) { case 'b':
        /* ... */
    } if (0) { case 'c':
        /* ... */
    }
    /* common code for non-default cases */
}

I'm going to go to hell for this...

switch (foo) {
default:
    /* code for default case */
    break;
    if (0) { case 'a':
        /* ... */
    } if (0) { case 'b':
        /* ... */
    } if (0) { case 'c':
        /* ... */
    }
    /* common code for non-default cases */
}
毁我热情 2025-01-04 14:16:57

为什么不使用由字母索引的函数指针数组而不是 switch?这将更加节省空间和速度。而且,在我看来,更具可读性。

static void (*fn_table['z' - 'a' + 1])(void) = {
    &ShowApple,
    &ShowBananna,

    ...,

    &ShowZebra,
};

if (letter < 'a' || 'z' < letter)
{
    printf("You did not enter an english letter. Silly human!");
}
else
{
    (*fn_table[letter - 'a'])();
    printf("You entered an english letter.");
}

Why don't you use an array of function pointers indexed by a letter instead of switch? That would be both more space and speed efficient. And, IMO, more readable.

static void (*fn_table['z' - 'a' + 1])(void) = {
    &ShowApple,
    &ShowBananna,

    ...,

    &ShowZebra,
};

if (letter < 'a' || 'z' < letter)
{
    printf("You did not enter an english letter. Silly human!");
}
else
{
    (*fn_table[letter - 'a'])();
    printf("You entered an english letter.");
}
我要还你自由 2025-01-04 14:16:57

如果它确实是每种情况下的最后一条语句,您可以简单地使用 if 语句随后执行它:

int wasEnglish = 1;
switch(letter)
{
case a:
    ShowApple();
    break;
case b:
    ShowBananna();
    break;
...
case z:
    ShowZebra();
    break;
default:
    wasEnglish = 0;
    break;
}

if (wasEnglish)
{
    printf("You entered an english letter.");
}
else
{
    printf("You did not enter an english letter. Silly human!");
}

If it's really the last statement in each case, you could simply execute it afterward using an if statement:

int wasEnglish = 1;
switch(letter)
{
case a:
    ShowApple();
    break;
case b:
    ShowBananna();
    break;
...
case z:
    ShowZebra();
    break;
default:
    wasEnglish = 0;
    break;
}

if (wasEnglish)
{
    printf("You entered an english letter.");
}
else
{
    printf("You did not enter an english letter. Silly human!");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文