求一个简单的函数

有一个函数:

[latex display=”true”] y = \begin{cases} -1&(x < 0) \\ 0&(x = 0) \\ 1&(x > 0) \end{cases} [/latex]

编写一个程序,输入一个x的值,要求输出相应的y的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <stdio.h>
int main() {
    int x, y;
    printf("Please enter x: \n");
    scanf("%d", &x);
    if(x < 0) {
        y = -1;
    } else {
        if(x == 0) {
            y = 0;
        } else{
            y = 1;
        }
    }
    printf("y=%d", y);
    return 0;
}