编写一段程序,要求输入x的值,输出y的值,分别用不嵌套if语句,嵌套语句,if else语句,switch语句,编写

x(-5<x<0)y= x-1(x=0) x+1(o<x<1o)
2024-11-24 04:51:55
推荐回答(2个)
回答1:

#include

float f1(float x){ //不用嵌套if
float t;
if(-5 if(x==0) t=x-1;
if(0 return t;
}

float f2(float x){ //嵌套if
float t;
if (-5 t=x+1;
if(-5 t=x-1;
if(-5 t=x;
}// 第三层
}// 第二层
}// 第一层
return t;
}

float f3(float x){ //if~else语句
float t;
if (-5 else if (x==0) t=x-1;
else if(0 return t;
}

float f4(float x){ //switch语句
float t;
int flag = (x<0 ? -1 : x==0 ? 0 : 1);
switch (flag) {
case (-1): t=x; break;
case (0): t=x-1; break;
case (1): t=x+1; break;
}
return t;
}

int main() {
float x, y;
scanf("%f", &x);
y=f1(x);
printf("%f\n", y);
y=f2(x);
printf("%f\n", y);
y=f3(x);
printf("%f\n", y);
y=f4(x);
printf("%f\n", y);
return 0;
}

回答2:

#include
void main()
{
int x;
int y;
printf("请输入X的值,-5scanf("%d",x);
if(x=0)
y=x-1;
printf("%d",y);
else
y=x+1;
printf("%d",y);
}

#include
void main()
{
int x;
int y;
printf("请输入X的值,-5scanf("%d",x);
switch(y)
{
case x=0:y=x-1;
break;
case x>0&&x<10:y=x+1;
break;
}
printf("%d",y);
}