6
回答
新手求解答关于c语言代码问题

我的代码哪错了?显示的是:ex1.c:7:10: error: switch quantity not an integer
ex1.c:9:2: error: case label does not reduce to an integer constant
ex1.c:12:2: error: case label does not reduce to an integer constant
ex1.c:15:2: error: case label does not reduce to an integer constant


#include <stdio.h>
main()
{
float x,y;
  prntf("plese input a count for x:");
scanf("%f",&x);
switch (x)
{
case x<1:
y = x;
break;
case x>=1||x<10:
y = 2*x - 1;
break;
case x>=10:
y = 3*x - 11;
}
printf("the result 'y' is :%f\n",y);


return 0;
}

<无标签>
举报
青鸾之旅
发帖于6年前 6回/569阅
共有6个答案 最后回答: 6年前

switch quantity not an integer (case 后面就整型和字符型变量,不要用 float型)

The expression of each case label shall be an integer constant expression and no two of
the case constant expressions in the same switch statement shall have the same value
after conversion.

c99标准。

#include <stdio.h>

main()
{
	float x,y;
	int a[] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
 
  	printf("plese input a count for x:");
	scanf("%f",&x);

	int c = a[x < 10 ? (int)x:10];;

	switch (c)
	{
		case 0:
			y = x;
			break;
		case 1:
			y = 2*x - 1;
			break;
		case 2:
			y = 3*x - 11;
	}

	printf("the result 'y' is :%f\n", y);

	return 0;
}
#include <stdio.h>

void fun0(float x)
{
	printf("the result 'y' is :%f \n", x);
}

void fun1(float x)
{
	printf("the result 'y' is :%f \n", 2*x - 1);
}

void fun2(float x)
{
	printf("the result 'y' is :%f \n", 3*x - 11);
}

typedef void FUN(float x); 

main()
{
	float x,y;
	int a[11] = { 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2};
 
 	FUN *fun[3] = { fun0, fun1, fun2 };
 
  	printf("plese input a count for x:");
	scanf("%f",&x);

	int c = a[x < 10 ? (int)x:10];
	
	(*fun[c])(x);

	return 0;
}
顶部