练习30
编写一个程序,读取作为小数扩展开发的浮点值,并执行以下操作:
- 如果它是整数,则显示它是整数
- 否则,将其四舍五入为整数并打印结果。
请注意数据控制
这里是一个没有有关整数类型的消息的新版本。
#include <stdio.h>
#include <math.h>
int main(){
double x; //the argument of f(x)
printf("Program demands x");
printf("\nand writes the rounded value\n");
printf("Author: xXx\n\n");
//loading data
printf("Write x in float type in decimal extension "); // after many tries, program is not rounding the value
if (scanf("%lf",&x)!=1 || getchar()!='\n'){
printf("Wrong data.\n");
printf("\nEnd of program.\n");
return 0;
}
double round( double x );
printf( "Rounded value is = %lf\n", x);
printf("\nEnd of program.\n");
return 0;
}
scanf
,您必须知道正在分配的类型。因此,根据这种逻辑,您已经知道了该类型。如果我理解有误,请纠正我,但我认为您的问题可能是要确定用户输入的数字(来自字符串)是整数还是浮点数。 - jrd1