Created
November 5, 2014 03:53
-
-
Save fjc-oai/69664e3d6ca5c2ce2f0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
string convert(string &decimal){ | |
int p=decimal.size(); | |
for(int i=0;i<decimal.size();i++) | |
if(decimal[i]=='.'){ | |
p=i; | |
break; | |
} | |
int n1=atoi(decimal.substr(0,p).c_str()); | |
double n2=atof(decimal.c_str())-n1; | |
string s1,s2; | |
while(n1>0){ | |
s1=(n1%2==0?"0":"1")+s1; | |
n1/=2; | |
} | |
while(n2>0){ | |
if(s2.size()>10) | |
break; | |
n2*=2; | |
if(n2>=1){ | |
s2+="1"; | |
n2=n2-1; | |
}else{ | |
s2+="0"; | |
} | |
} | |
if(s2.empty()) | |
return s1; | |
else | |
return s1+"."+s2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment