用C语言的swich语句写函数

来源:百度知道 编辑:UC知道 时间:2024/06/22 02:10:27
x(-5<x<0)
y={ x-1(x=0)
x+1(0<x<10)
上面显示的不对,是这个
y=x (-5<x<0)
y=x-1 (x=0)
y=x+1 (0<x<10)

我知道很麻烦,但题目就这么要求。。。。。是if else好写,但题目就要switch

#include<stdio.h>
void main()
{
double x,y;
int flag;
scanf("%lf",&x);
if(x>-5&&x<0)flag=0;
else if(x==0)flag=1;
else if(x>0&&x<10)flag=2;
switch(flag)
{
case 0:y=x;break;
case 1:y=x-1;break;
case 2:y=x+1;break;
default:printf("wrong input\n");
}
if(flag>=0&&flag<=2)printf("y=%g\n",y);
}

意思我懂,可是这个用swich比较麻烦,还是用if,else比较方便吧

x是什么啊
0.5