已解决
C //例4.5 有一函数:...... 编一程序,输入一个x值,要求输出相应的y值。
来自网友在路上 166866提问 提问时间:2023-10-08 16:53:55阅读次数: 66
最佳答案 问答题库668位专家为你答疑解惑
C语言设计 (第四版) 谭浩强 例4.5
有一函数:
y = { − 1 ( x < 0 ) 0 ( x = 0 ) 1 ( x > 0 ) y = \begin{cases}-1\ (x \lt 0)\\0\ \ \ \ (x = 0)\\1\ \ \ \ (x \gt 0)\end{cases} y=⎩ ⎨ ⎧−1 (x<0)0 (x=0)1 (x>0)
编一程序,输入一个x值,要求输出相应的y值。
IDE工具:VS2010
Note: 使用不同的IDE工具可能有部分差异。
代码块
方法1:条件语句
#include <stdio.h>
#include <stdlib.h>#define FORMAT "%d"typedef int Type;int main(){Type x;int y;printf("Enter X Value: ");scanf_s(FORMAT, &x);if(x < 0){y = -1;}else if(x == 0){y = 0;}else{y = 1;}printf("Y Value = ");printf(FORMAT, y);printf("\n");system("pause");return 0;
}
方法2:使用函数的模块化设计
#include <stdio.h>
#include <stdlib.h>#define FORMAT "%d"typedef int Type;void input(Type *x){printf("Enter X Value: ");scanf_s(FORMAT, x);
}void output(Type *x){int y = *x > 0 ? 1 : (*x == 0 ? 0 : -1);printf("Y Value = ");printf(FORMAT, y);printf("\n");
}int main(){Type *x = (Type*)malloc(sizeof(Type));input(x);output(x);free(x);system("pause");return 0;
}
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"C //例4.5 有一函数:...... 编一程序,输入一个x值,要求输出相应的y值。":http://eshow365.cn/6-17367-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 紧固行业内卷严重,张友君的飞沃科技能独善其身吗?
- 下一篇: 一个非常简单的变分量子分类器 (VQC)