替代功能

发布于 2025-01-24 08:50:27 字数 446 浏览 0 评论 0原文

#include <stdio.h>

void main(void)
{

    int price;

    scanf("%d", &price);
    
    switch (price)
    {
    case 1000: // what i want - case pay >= 1000
               // code (no break - intentional)

    case 500: // ....... - case pay >= 500
              // code
    
    default:
               break;
    }
}

我是新手。在没有中断的情况下,是否有任何替代方案 ,并且还可以在switch中使用比较而不是常数?

#include <stdio.h>

void main(void)
{

    int price;

    scanf("%d", &price);
    
    switch (price)
    {
    case 1000: // what i want - case pay >= 1000
               // code (no break - intentional)

    case 500: // ....... - case pay >= 500
              // code
    
    default:
               break;
    }
}

I'm new to this. Is there any alternative to switch without break and also able to use comparisons, not constant, in a switch?

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

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

发布评论

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

评论(3

黑寡妇 2025-01-31 08:50:27

只需系列如果语句,则:

if (price >= 1000) {
    // do something
}
if (price >= 500) {
    // do some more stuff
}
  • 您也可能需要使用#define对这些魔术数字(1000,500 ...)。或使用枚举枚举它们。
  • 尽管switch()看起来像一个函数,但它是一个语句块。

Just series of if statements then :

if (price >= 1000) {
    // do something
}
if (price >= 500) {
    // do some more stuff
}
  • You may also want to use #define for those magic numbers (1000, 500 ...). Or Enumerate them using enum.
  • Although switch() looks like a function, it's a statement block.
千紇 2025-01-31 08:50:27

正如其他人所说,如果块比switch> switch> switch语句(其中 故意 fall through是在C编程世界中,许多人皱着眉头)。

但是,如果要将一个数字分为固定大小的块范围(在您的情况下,大小为500),则可以将数字除以该块,并将结果用作switch 变量。另外,请注意,默认案例不必是 final 案例 - 它甚至可以在任何地方,甚至首先。

在下面的示例中,由于秋天的秋季(即break;在任何一个case块中),所以案例0:代码将用于任何输入;对于少于500的输入(即 Integer disement将导致0),该情况将执行。对于500范围内999的数字(将给出1),案例1 案例0代码将运行;而且,对于数字&gt; = 1000,默认案例将运行,然后是其他两个块。 (此代码需要正面 Price!!)

#include <stdio.h>

int main(void)
{
    int price = 0;
    printf("Price: ");
    int s = scanf("%d", &price);
    if (s != 1 || price < 0) {
        printf("Invalid price!\n");
    }
    else {
        switch (price / 500) {
        default:
            printf("Price is >= 1000!\n");
        case 1:
            printf("Price is >= 500\n");
        case 0:
            printf("Price is anything\n");
        }
    }
    return 0;
}

正如我所说,我通常不建议在情况下使用switch ) 像这样;但是,尽管如此,但实际上,添加额外的块/条件(例如1000-1499范围内的价格代码)实际上非常容易代码。此外,当“范围”的数量变得足够大(例如,超过3)时,使用这种switch> switch> switch而不是链条,则可以说更清晰如果语句/块。


注意:为了增加清晰度 - 如果您的编译器支持它(Clang和GCC会这样做,但我不知道MSVC的同等用途) - 那么您可以在任何地方添加__属性__((fallthrough));语句依靠任何(隐含的)跌落 - 尽管从一个案例块到另一个案例块。在上面显示的代码中,将在案例1:案例0:标签之前立即添加此类语句。

As others have said, this problem is better suited to consecutive if blocks than to a switch statement (where the use of deliberate fall-through is frowned-upon by many in the C programming world).

However, if you want to divide a number into ranges of fixed-size blocks (in your case, that size is 500), then you can divide your number by that block-size and use the result as the switch variable. Also, note that the default case doesn't have to be the final case – it can be anywhere, even first.

In the below example, because of the fall-through (i.e. no break; statements in any of the case blocks), the case 0: code will be run for any input; for inputs less than 500 (i.e. the integer division will result in 0), only that case will execute. For numbers in the range 500 thru 999 (division will give 1), the case 1 and case 0 code will run; and, for numbers >= 1000, the default case will run, followed by the other two blocks. (This code requires a positive value for price!)

#include <stdio.h>

int main(void)
{
    int price = 0;
    printf("Price: ");
    int s = scanf("%d", &price);
    if (s != 1 || price < 0) {
        printf("Invalid price!\n");
    }
    else {
        switch (price / 500) {
        default:
            printf("Price is >= 1000!\n");
        case 1:
            printf("Price is >= 500\n");
        case 0:
            printf("Price is anything\n");
        }
    }
    return 0;
}

As I have said, I would generally not recommend using a switch in cases (poor pun) like this; but, that advice notwithstanding, it is then actually quite easy to add extra blocks/conditions, such as code for prices in the 1000 - 1499 range, where you could just insert a case 2: block to the code. Furthermore, when the number of 'ranges' becomes sufficiently large (say, more than 3), then it does arguably become clearer to use such a switch, rather than a chain of if statements/blocks.


Note: For added clarity – if your compiler supports it (clang and gcc do but I'm not aware of an equivalent for MSVC) – then you can add __attribute__((fallthrough)); statements wherever you are relying on any (implied) fall-though from one case block to the next. In the code shown above, such statements would be added immediately before the case 1: and case 0: labels.

浅听莫相离 2025-01-31 08:50:27

使用链式如果…else语句:

if (1000 <= price)
{
    // Things for price 1000 or more.
}
else if (500 <= price)
{
    // Things for price 500 or more.
}
else
{
    // Things for other cases.
}

Use chained if … else statements:

if (1000 <= price)
{
    // Things for price 1000 or more.
}
else if (500 <= price)
{
    // Things for price 500 or more.
}
else
{
    // Things for other cases.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文