C language programming (third edition) He Qinming exercises 5-1

C language programming (third edition) He Qinming exercises 5-1

List of exercises
1. C language programming (third edition) He Qinming exercises 2-1
2. C language programming (third edition) He Qinming exercises 2-2
3. C language programming (third edition) He Qinming exercises 2-3
4. C language programming (third edition) He Qinming exercises 2-4
5. C language programming (third edition) He Qinming exercises 2-5
6. C language programming (third edition) He Qinming exercises 2-6
7. C language programming (third edition) He Qinming exercises 3-1
8. C language programming (third edition) He Qinming exercises 3-2
9. C language programming (third edition) He Qinming exercises 3-3
10. C language programming (third edition) He Qinming exercises 3-4
11. C language programming (third edition) He Qinming exercises 3-5
12. C language programming (third edition) He Qinming exercises 4-1
13. C language programming (third edition) He Qinming exercises 4-2
14. C language programming (third edition) He Qinming exercises 4-3
15. C language programming (third edition) He Qinming exercises 4-4
16. C Language Programming (Third Edition) He Qinming with Exercises 4-5
17. C language programming (third edition) He Qinming exercises 4-6
18. C language programming (third edition) He Qinming exercises 4-7
19. C language programming (third edition) He Qinming exercises 4-8
20. C language programming (third edition) He Qinming exercises 4-9
21. C language programming (third edition) He Qinming exercises 4-10
22. C language programming (third edition) He Qinming exercises 4-11


topic

Use the function to calculate the value of the piecewise function: input x, calculate and output the value of the following piecewise function f(x).
It is required to define and call the function sign (x) to realize this piecewise function. Try to write the corresponding program.
Insert picture description here


Analysis process

enter

Condition: Enter x

Output

Condition: output the value of the piecewise function f(x)

Code

#include <stdio.h>
#include <math.h>
int sign(int x);/*函数声明*/

int main () {
    
    
	/*定义变量*/
	int x;                                                                      /*定义变量,存储输入的n*/
	/*赋值*/
	printf("请输入x:\n");                                				        /*输入提示*/
	scanf("%d \n", &x);                                            				/*输入并赋给变量*/
    printf("sign(%d)=%d \n", x, sign(x));                                       /*调用函数,输出计算结果*/
    
	return 0;
}

int sign(int x){
    
    
   if(x<0) return -1;/*x小于0时,返回-1*/
   else if(x==0) return 0;/*x等于0时,返回0*/
   else return 1;/*x大于0时,返回1*/
}

operation result

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43228814/article/details/112506451