分段函数
f(x)=-1;x<0
0;x=0
2x;x>0
表现在代码里
int f;
if(x<0){
f=-1;
}else if(x==0){
f=0;
}else{
f=2*x;
}
printf("%d",f);
也可以写成
if(x<0){
printf("%d",-1);
}else if(x==0){
printf("%d",0);
}else{
printf("%d",2*x);
}
但是更推荐第一种,第一种方法叫做单一出口,对f的用法更灵活