分段函数求值 c语言

Description 已知:y是x的函数,
当x<-2时,y=7-2x;
当x>=-2,且x<3时,y=5-|3x+2|;
当x>=3时,y=3x+4

Input 任意输入一个整数
Output 输出其对应的函数值
Sample Input -4
2
5

Sample Output 15
-3
19
求写程序....

第1个回答  2012-11-24
# include <stdio.h>
# include <math.h>
void main()
{
int x;
int y;
while(1)
{
printf("input x:\n");
scanf("%d",&x);
if(x<-2)
{
y=7-2*x;
}
else if(x>=-1 && x<3)
{
y=5-abs(3*x+2);
}
else
{
y=3*x+4;
}
printf("y is %d\n\n",y);
}
}
这样就可以了,望采纳!本回答被提问者和网友采纳
第2个回答  2012-11-24
#include "stdio.h"
#include "math.h"
int main(){
int x,y;
scanf("%d",&x);
if(x<-2)
y=7-2x;
else if(x>-2 &&x<3)
y=5-abs(3x 2);
else
y=3x 4;
printf("%d",y);
}
第3个回答  2012-11-25
#include<stdio.h>
#include<stdlib.h>
main{}{
int x,ans;
while(scanf("%d",&x)!=EOF){
if(x<-2)ans=7-2*x;
else if(x>=-2&&x<3)ans=5-abs(3*x+2);
else ans=3*x+4;
printf("%d\n",ans);
}
}
相似回答