一道c的编程问题。(要求输入一个数,如果是正数就输出,如果是负数就输出他的绝对值)!

来源:百度知道 编辑:UC知道 时间:2024/04/30 12:40:20
大家帮帮忙,我编的程序到底是那里不对了,
#include<stdio.h>
main()
{
float x;
scanf("%f",&x);
if(x>=0)
printf("%f",x);
else
x=-x;
printf("%f",x);
}
现在c提示说你的程序有3 errors!

求绝对值的话:有小数的(即浮点型)用fabs()函数,整数用abs()
不是你想的那样加个-号是不行的!fabs()函数包含在#include<math.h>中
修改如下:
#include<stdio.h>
#include<math.h>
int main()
{
double x;
scanf("%lf",&x);
if(x>=0)

printf("%lf",x);

else
{
x=fabs(x);
printf("%lf",x);
}
return 0;
}

#include<stdio.h>
main()
{
float x;
scanf("%f",&x);
if(x>=0)
printf("%f",x);
else {
x=-x;
printf("%f",x);
}
}

你用的是什麼编译器阿?
错误信息是什麼

你这个代码里面还有个问题
if(x>=0) //浮点数是不能这样与0必较的

如果判"x == 0.0"
x> -极大值 && x< +极小值
fabs(x) < 极小值(趋近於0)

这个程序完全正确,在DEV C++下编译通过,而且DEV C++是很标准的,我帮你简化下程序
#include<stdio.h>
main()
{
float x;
scanf("%f",&x);
printf("%f",x>=0?x:-x);
}<