如何用C++实现如下程序:

来源:百度知道 编辑:UC知道 时间:2024/06/28 07:56:58
根据以下函数关系,对输入的每个x值,求y值.
y=x*(x+2) 2<x<=10
y=2*x -1<x<=2
y=x-1 x<=-1

int fun(int x)
{
int y;
if(x>2&&x<=10){y=x*(x+2);return y;}
else if(x>-1&&x<=2){y=2*x;return y;}
else if(x<=-1){y=x-1;return y;}
else {cout<<"输入非法!"<<endl;return 0;}
}

来个泛型的 呵呵
template <class T>
T func(T x)
{
if(x <= 10)
{
if(x>2)
return x*(x+2);
else if(x>-1)
return 2*x;
return x-1;
}
}