static_cast对double转换的问题
我是个C++新手,在做练习的时候遇到问题:用turbo C++或者DEV-C++和用MS的VC编译运行结果不一样。源码如下:
#include<iostream.h>
#include<math.h>
#include <stdlib.h>
int main()
{
cout<<"please input a number"<<endl;
long int num,mun=0,n=5,temp1,temp2,temp3;
cin>>num;
for(int i=0;i<=5;i++)
{
temp1=static_cast<long int>(pow(10,(5-i)));
temp2=static_cast<long int>(pow(10,i));
temp3=static_cast<long int>(pow(10,(5-i)));
mun+=(num/temp1*temp2);
//cout<<mun<<endl;//下面给出的运行结果是在这一行没被注释的情况下
if(mun!=0) num=num%temp3;
else n--;//n是用来记录输入的是几位数
}
cout<<n<<endl;
cout<<mun<<endl;
system("pause");
return 0;
}
功能是输入一个《=5位的整数,输出这个数的位数,并反转输出
比如输入1234,则输出4 4321
算法:(以输入1234为例)
1234除以10000 得0 余1234
1234除以1000 得1 余234
234除以100 得2 余34
34除以10 得3 余4
4除以1 得4 余0
将
得
数0*10^0+1*10^1+2*10^2+3*10^3+4*10^4
得到得结果就是反转的4321
在VC6.0中编译运行结果是:
please input a number
1234
0
0
100
2100
32100
432100
3
432100
在dev-C++中编译运行结果是:
please input a number
1234
0
0
99
2099
32096
632096
3
632096
请教问题的原因和解决办法。