如下:
#include
#include
int main()
{
float x, result;
printf("请输入x的值:");
scanf("%f",&x);
if(x <= 0)
{
result = 0;
}
else if(x <= 10)
{
result = sqrt(x);
}
else
{
result = 2 * x + 3;
}
printf("%f",result);
return 0;
}
扩展资料:
c语言计算分段函数值的简单代码:
#include
#include
int main()
{
double x,y;
printf("input x:\n");
scanf("%lf",&x);
if (x< -1.0) y=x*x*x-1;
else if (x >=-1 && x <= 1) y=-3*x+1;
else if (x>1 && x <=10) y=3*exp(2*x-1)+5;
else y = 5*x + 3.0* log10(2*x*x-1) -13;
printf("%.2lf\n",y);
return 0;
}