C语言 C switch 语句中的大于和小于
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20972297/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Larger than and less than in C switch statement
提问by Salahuddin
I'm trying to write a code that has a lot of comparison
我正在尝试编写一个有很多比较的代码
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”.
在“QUANT.C”中编写一个“量化”数字的程序。读取一个整数“x”并测试它,产生以下输出:
x 大于或等于 1000 打印“非常正”
x 从 999 到 100(包括 100)打印“非常正”
x 100 和 0 之间打印“正”
x 正好 0 打印“零”
x 0 和 -100 之间打印“负”
x 从 -100 到 -999(包括 -100)打印“非常负”
x 小于或等于 -1000 打印“非常负”因此 -10 将打印“否定”,-100“非常否定”和 458“非常肯定”。
Then I tried to solve it using a switch statement, but it didn't work. Do I have to solve it using an ifstatement or there is a method to solve it using a switch statement?
然后我尝试使用 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;
}
采纳答案by Eric Fortin
There is no clean way to solve this with switch, as cases need to be integral types. Have a look at if-else if-else.
没有干净的方法可以用 switch 解决这个问题,因为 case 需要是整数类型。看看 if-else if-else。
回答by usr2564301
A switch-less andif-else-less method:
无开关和无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;
}
The for-loop contains no code (there is just an empty statement ;) but it still runs over the array with values and exits when the entered value ais equal to or larger than the valueelement in the array. At that point, iholds the index value for the descriptionto print.
for 循环不包含任何代码(只有一个空语句;),但它仍会遍历包含值的数组,并在输入的值a等于或大于value数组中的元素时退出。此时,i保存description要打印的索引值。
回答by anatolyg
If you are using gcc, you have "luck" because it supports exactly what you want by using a language extension:
如果您使用的是 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;
...
}
This is non-standard though, and other compilers will not understand your code. It's often mentioned that you should write your programs only using standard features ("portability").
但是,这是非标准的,其他编译器将无法理解您的代码。人们经常提到你应该只使用标准特性(“可移植性”)来编写你的程序。
So consider using the "streamlined" if-elseif-elseconstruct:
因此,请考虑使用“流线型”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");
}
回答by Souza
Use:
用:
switch (option(a)) {
case (0): ...
case (1): ...
case (2): ...
case (n): ...
Where the option()function is simply a function with if else.
其中option()函数只是一个带有if else.
It lets you keep the clean look of a switch and the logic part is elsewhere.
它可以让您保持开关的简洁外观,而逻辑部分则在别处。
回答by Sourav Ghosh
(a>1000)evaluates to either 1 [true] or 0 [false].
(a>1000)计算结果为 1 [真] 或 0 [假]。
Compile and you will get the error:
编译,你会得到错误:
test_15.c:12: error: case label does not reduce to an integer constant
This means, you have to use an integer constantvalue for the caselabels. An If-else if-elseloop should work just fine for this case.
这意味着,您必须integer constant为case标签使用一个值。在If-else if-else这种情况下,循环应该可以正常工作。
回答by rolinger
Why do you have a preference to use switch?
为什么您更喜欢使用 switch?
I'm asking because this sounds awfully like a 'homework question'. A compiler should deal with if/else construct just as efficiently as a switch (even if you weren't dealing with ranges).
我问是因为这听起来非常像一个“家庭作业问题”。编译器应该像处理 switch 一样有效地处理 if/else 构造(即使您没有处理范围)。
Switch can't handle ranges as you have shown, but you could find a way to include switch by categorising the input first (using if/else) then using a switch statement to output the answer.
Switch 无法处理您所展示的范围,但您可以通过首先对输入进行分类(使用 if/else)然后使用 switch 语句输出答案来找到一种包含 switch 的方法。

