同一个程序,同样的输入,输出为什么不同?

水落 2014-12-18 09:51:49
为什么我的程序两次都是输入5,得出的结果却不一样? 一次是5,一次是5.0


下面是我的代码,很简单。
#include<iostream>
#include<iomanip>

using namespace std;

int main()
{
int num;
double x,y;
cin>>num;
while(num--)
{
cin>>x;
if(x<1) y=x;
else if(x>=10) y=3*x-11;
else y=2*x-1;
if(y==(int)y) cout<<y<<endl;
else cout<<fixed<<setprecision(1)<<y<<endl;
}
}
/*
5
5
5.2
5
*/
...全文
615 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
rr2114 2014-12-20
  • 打赏
  • 举报
回复
帮顶
若谷. 2014-12-20
  • 打赏
  • 举报
回复
你写的程序好难看
水落 2014-12-19
  • 打赏
  • 举报
回复
引用 3 楼 clremix 的回复:
编译器优化问题,double类型在用到小数前依旧是以整形的形式存在,你中间输入了1.1,导致y已经确确实实成为了double浮点类型,输出时的 y == (int)y 是始终成立的,不会通过下面的格式化输出
  按你的说法 y 在输入1.1的时候,y已经变成了确确实实double 型的。   那么if(y==(int)y) 应该是不成立的。  所以才会输出时都带一位小数。  但是我把 double x,y;定义在while循环里面时, 输入还是和之前相同。   按你的说法应该是上一次循环的y变成了确确实实的double型,但是 当前循环的y还是新定义的。 
水落 2014-12-19
  • 打赏
  • 举报
回复
引用 2 楼 jacksonfan 的回复:
setprecision SMANIP( int ) setprecision( int np ); #include <iomanip.h> Parameter np An integer that indicates the number of significant digits or significant decimal digits to be used for floating-point display. Remarks This parameterized manipulator sets the stream’s internal floating-point precision variable to np. The default precision is six digits. If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change. 看看这个函数的说明吧 输入1.1的时候,调用了该函数,设置了y的精度,1位小数,所以后面输出小数点后都会有1位。
但是我把double x,y;写在while循环里面结果也是一样的;下在while循环里面的话,每循环一次就相当于重新定义了一次x,y; 那所设置的y和下次循环的y应该不一样啊,毕竟是重新在内存开了一个double 型的 空间出来。 这个怎么解释呢?  
Coding_Chan 2014-12-19
  • 打赏
  • 举报
回复
编译器优化问题,double类型在用到小数前依旧是以整形的形式存在,你中间输入了1.1,导致y已经确确实实成为了double浮点类型,输出时的 y == (int)y 是始终成立的,不会通过下面的格式化输出
jacksonfan 2014-12-19
  • 打赏
  • 举报
回复
setprecision SMANIP( int ) setprecision( int np ); #include <iomanip.h> Parameter np An integer that indicates the number of significant digits or significant decimal digits to be used for floating-point display. Remarks This parameterized manipulator sets the stream’s internal floating-point precision variable to np. The default precision is six digits. If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change. 看看这个函数的说明吧 输入1.1的时候,调用了该函数,设置了y的精度,1位小数,所以后面输出小数点后都会有1位。
jacksonfan 2014-12-19
  • 打赏
  • 举报
回复
引用 8 楼 clremix 的回复:
错了,我试了下还真会出现9.0
还是看看MSDN Remarks This parameterized manipulator sets the stream’s internal floating-point precision variable to np. The default precision is six digits. If the display format is scientific or fixed, then the precision indicates the number of digits after the decimal point. If the format is automatic (neither floating point nor fixed), then the precision indicates the total number of significant digits. This setting remains in effect until the next change.
赵4老师 2014-12-19
  • 打赏
  • 举报
回复
相由心生。 http://bbs.csdn.net/topics/390676437 电脑内存或文件内容只是一个一维二进制字节数组及其对应的二进制地址; 人脑才将电脑内存或文件内容中的这个一维二进制字节数组及其对应的二进制地址的某些部分看成是整数、有符号数/无符号数、浮点数、复数、英文字母、阿拉伯数字、中文/韩文/法文……字符/字符串、汇编指令、函数、函数参数、堆、栈、数组、指针、数组指针、指针数组、数组的数组、指针的指针、二维数组、字符点阵、字符笔画的坐标、黑白二值图片、灰度图片、彩色图片、录音、视频、指纹信息、身份证信息……
Coding_Chan 2014-12-19
  • 打赏
  • 举报
回复
引用 5 楼 u013961718 的回复:
[quote=引用 3 楼 clremix 的回复:] 编译器优化问题,double类型在用到小数前依旧是以整形的形式存在,你中间输入了1.1,导致y已经确确实实成为了double浮点类型,输出时的 y == (int)y 是始终成立的,不会通过下面的格式化输出
  按你的说法 y 在输入1.1的时候,y已经变成了确确实实double 型的。   那么if(y==(int)y) 应该是不成立的。  所以才会输出时都带一位小数。  但是我把 double x,y;定义在while循环里面时, 输入还是和之前相同。   按你的说法应该是上一次循环的y变成了确确实实的double型,但是 当前循环的y还是新定义的。 [/quote] 错了,我试了下还真会出现9.0
Coding_Chan 2014-12-19
  • 打赏
  • 举报
回复
引用 5 楼 u013961718 的回复:
[quote=引用 3 楼 clremix 的回复:] 编译器优化问题,double类型在用到小数前依旧是以整形的形式存在,你中间输入了1.1,导致y已经确确实实成为了double浮点类型,输出时的 y == (int)y 是始终成立的,不会通过下面的格式化输出
  按你的说法 y 在输入1.1的时候,y已经变成了确确实实double 型的。   那么if(y==(int)y) 应该是不成立的。  所以才会输出时都带一位小数。  但是我把 double x,y;定义在while循环里面时, 输入还是和之前相同。   按你的说法应该是上一次循环的y变成了确确实实的double型,但是 当前循环的y还是新定义的。 [/quote] if ( y == (int)y )是成立的,及时你强制转换成int,但是比较的时候会全部变成9.0,不信你可以自己跟一下代码,如果你把double拿到里面,那么结果应该不会出现9.0,我试了下也确实不会出现9.0
jacksonfan 2014-12-19
  • 打赏
  • 举报
回复
引用 4 楼 u013961718 的回复:
但是我把double x,y;写在while循环里面结果也是一样的;下在while循环里面的话,每循环一次就相当于重新定义了一次x,y; 那所设置的y和下次循环的y应该不一样啊,毕竟是重新在内存开了一个double 型的 空间出来。 这个怎么解释呢?  
删掉fixed fixed, to insert floating-point values in fixed-point format (with no exponent field). 自己跟踪看看

64,650

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧