用c++编程序求分段函数,从键盘上输入x的值求y值,编写一完整程序,实现上述函数功能

x (x<0) y= 3x-2 (10<=x<50) 4x-1 (50<=x<=100) 5x (x>=100)
2025-05-12 09:24:24
推荐回答(3个)
回答(1):

#include
#include
int sub_function(int x)
{
if(x<0)
return x;
else
if(10<=x&&x<50)
return 3*x-2;
else
if(50<=x&&x<=100)
return 4*x-1;
else
if(x>100)
return 5*x;
else
cout<<"未定义X在此区域!!"<}
void main(void)
{
while(1)
{
int x,y;
cout<<"请输入x(整数):";
cin>>x;
y=sub_function(x);
cout<<"Y的值为:"< getch();
}
}

回答(2):

#include
fun( double x){
if( x<0) return x;
if(10<=x&&x<=50) return 3*x-2;
if(50<=x&&x<=100) return 4*x-1;
if(x>=100) return 5*x;
return 0;
}
int main(){
double x;
cin>>x;
cout<return 0;
}

回答(3):

# include
int main()
{
int x;
scanf("%d",&x);
if(x>=10&&x<50)
printf("y=%d\n",3*x-2);
if(x>=50&&x<=100)
printf("y=%d\n",4*x-1)
if(x>=100)
printf("y=%d\n",5*x)

return 0;
}