本题要求实现符号函数sign(x)。
函数接口定义:
int sign( int x );
其中x是用户传入的整型参数。符号函数的定义为:若x大于0,sign(x) = 1;若x等于0,sign(x) = 0;否则,sign(x) = −1。
#include <iostream>
using namespace std;
int sign(int x);
int main()
{
int x;
cin >> x;
//scanf("%d",&x);
cout << "sign(" << x << ") = " << sign(x) << endl;
//printf("sign(%d)=%d\n",x,sign(x);
return 0;
}
//if else写法
int sign(int x)
{
int sign;
if (x > 0)
{
sign = 1;
}
else if (x == 0)
{
sign = 0;
}
else
{
sign = -1;
}
return sign;
}
#include <iostream>
using namespace std;
int sign(int x);
int main()
{
int x;
cin >> x;
//scanf("%d",&x);
cout << "sign(" << x << ") = " << sign(x) << endl;
//printf("sign(%d)=%d\n",x,sign(x);
return 0;
}
int sign(int x)
{
if (x > 0)
{
return (1);
}
else if (x == 0)
{
return (0);
}
else
{
return (-1);
}
}