大于和小于switch语句C

时间:2014-01-07 12:59:57

标签: c if-statement comparison switch-statement

我正在尝试编写一个有很多比较的代码

Write a program in “QUANT.C” which “quantifies” numbers.  Read an integer “x” and test it, producing the 
following output: 

x greater than or equal to 1000 print “hugely positive” 
x from 999 to 100 (including 100) print “very positive” 
x between 100 and 0 print “positive” 
x exactly 0 print “zero” 
x between 0 and -100 print “negative” 
x from -100 to -999 (including -100) print “very negative” 
x less than or equal to -1000 print “hugely negative” 

Thus -10 would print “negative”, -100 “very negative” and 458 “very positive”.

然后我尝试使用switch解决它,但它不起作用,我是否必须使用if语句解决它,或者有一种方法使用switch解决它?

#include <stdio.h> int main(void) { int a=0; printf("please enter a number : \n"); scanf("%i",&a); switch(a) { case (a>1000): printf("hugely positive"); break; case (a>=100 && a<999): printf("very positive"); break; case (a>=0 && a<100): printf("positive"); break; case 0: printf("zero"); break; case (a>-100 && a<0): printf("negative"); break; case (a<-100 && a>-999): printf("very negative"); break; case (a<=-1000): printf("hugely negative"); break; return 0; }

7 个答案:

答案 0 :(得分:7)

无交换 if-else-less方法:

#include <stdio.h>

int main(void)
{
    int a=0, i;
    struct {
        int value;
        const char *description;
    } list[] = {
        { -999, "hugely negative" },
        { -99, "very negative" },
        { 0, "negative" },
        { 1, "zero" },
        { 100, "positive" },
        { 1000, "very positive" },
        { 1001, "hugely positive" }
    };

    printf("please enter a number : \n");
    scanf("%i",&a);

    for (i=0; i<6 && a>=list[i].value; i++) ;
    printf ("%s\n", list[i].description);

    return 0;
}

for循环不包含任何代码(只有一个空语句;)但它仍然在数组上运行,当输入的值a等于或大于数组中的value元素。此时,i保存要打印的description的索引值。

答案 1 :(得分:6)

没有干净的方法可以通过开关解决这个问题,因为案例需要是整数类型。看看if-else if-else。

答案 2 :(得分:5)

如果您正在使用gcc,那么您将获得“运气”,因为它通过语言扩展支持您想要的内容:

#include <limits.h>
...

switch(a)
{
case 1000 ... INT_MAX: // note: cannot omit the space between 1000 and ...
    printf("hugely positive");
   break;
case 100 ... 999:
    printf("very positive");
   break;
...
}

这是非标准的,其他编译器不会理解您的代码。经常提到你应该只使用标准功能(“可移植性”)来编写你的程序。

因此,请考虑使用“简化的”if-elseif-else构造:

if (a >= 1000)
{
    printf("hugely positive");
}
else if (a >= 100)
{
    printf("very positive");
}
else if ...
...
else // might put a helpful comment here, like "a <= -1000"
{
    printf("hugely negative");
}

答案 3 :(得分:2)

(a>1000)评估为1 [true]或0 [false]。

编译并将收到错误

test_15.c:12: error: case label does not reduce to an integer constant

这意味着,您必须为integer constant标签使用case值。对于这种情况,If-else if-else循环应该可以正常工作。

答案 4 :(得分:1)

这可能有点太晚了,但是:

switch( option(a) ){
    case (0): ...
    case (1): ...
    case (2): ...
    case (n): ...

其中option()函数只是if else的函数。 它可以让你保持开关的清晰外观,逻辑部分在其他地方。

答案 5 :(得分:0)

为什么你喜欢使用开关?

我问,因为这听起来非常像'家庭作业问题'。编译器应该像切换一样有效地处理if / else构造(即使你没有处理范围)。

Switch无法处理您所显示的范围,但您可以通过先对输入进行分类(使用if / else)然后使用switch语句输出答案来找到包含切换的方法。

答案 6 :(得分:-1)

使用类型参数模式以及when子句 例如

switch(a)
    {
        case * when (a>1000):
            printf("hugely positive");
            break;
        case * when (a>=100 && a<999):
            printf("very positive");
            break;
        case * when (a>=0 && a<100):
            printf("positive");
            break; }