C语言,已知整型变量a,b(b≠0),设x为实型变量,请用switch结构编程计算以下分段函数的值。

2025-04-03 11:42:33
推荐回答(1个)
回答1:

简单啊
语句:
switch((int)(x+0.5))
{ case 1: y=a+b*x; break;
case 2: y=a–b*x;break;
case 3: y=a*b*x; break;
case 4: y=a/(b*x);break;
default: printf("x error. \n"); }


int temp = (int)(x + 0.5);
if(temp == 1)
y=a+b*x;
else if(temp ==2)
y=a–b*x;
else if(temp == 3)
y=a*b*x;
else if(temp ==4)
y=a/(b*x);
else
printf("x error. \n");