#include <iostream>
using namespace std;
class Whole_to_decimal{
private:
int x;
public:
Whole_to_decimal(int x_):x(x_){}
float transfloat(){
int flag=0;
float t = (float)x;
if(t<0)
{
t=-t;
flag=1;
}
while(t>1)
{
t/=10;
}
if(flag)
t=-t;
return t;
}
};
class Decimal_to_whole{
private:
float x;
public:
Decimal_to_whole(float x_):x(x_){}
int getWhole()
{
return (int)x;
}
float getdecimal()
{ float x_ = x;
int flag=0;
if(x_<0)
{
x_=-x_;
flag=1;
}
float decimal = (x_-(int)x_);
/*
当我用111.111-111后的结果是0.11100000697...末尾带有几个数
*/
if(flag)
decimal=-decimal;
return decimal;
}
int getdecimal2()
{
float x_=getdecimal();
int flag=0;
if(x_<0.0)
{
flag=1;
x_=-x_;
}
int t = 0;
while(x_>0.0)
{
x_*=10;
t+=(int)x_;
x_=(x_-(int)x_);
if(x_<=0.0001)
/*
如上,末尾带有几个无关的数,所以变不了0,只能取一个相对应的精度
*/
break;
t*=10;
}
if(flag)
t=-t;
return t;
}
};
int main()
{
Whole_to_decimal Test(-1111);
cout<<Test.transfloat()<<endl;
Decimal_to_whole Test2(-111.111);
cout<<Test2.getdecimal()<<endl;
cout<<Test2.getdecimal2()<<endl;
cout<<Test2.getWhole()<<endl;
}
结果: