[求助]下面这个程序应该怎样编?(已解决)
题目:
分别用条件运算符(?:)、if语句和fabs函数编程计算a加b的绝对值的值。
解决方案:
方法1:(用条件运算符(?:))
#include <stdio.h>
main()
{
float a,b,x,y;
printf("Enter Two Numbers:")
scanf("%f%f",&a,&b);
x=(b>=0)?b:-b;
y=a+x;
printf("The Answer is %f\n",y);
}
方法2:(用if语句)
#include <stdio.h>
main()
{
float a,b,y;
printf("Enter Two Numbers:")
scanf("%f%f",&a,&b);
if(b>=0)
y=a+b;
else
y=a-b;
printf("The Answer is %f\n",y);
}
方法3:(用fabs函数)
#include <stdio.h>
#include <math.h>
main()
{
float a,b,c;
printf("Enter Two Numbers:")
scanf("%f%f",&a,&b);
c=a+fabs(b);
printf("The Answer is %f\n",c);
}
结语:谢谢大家指点哦。我的编程水平又得到了提高!
[此贴子已经被作者于2007-11-21 12:33:19编辑过]