Q5.2
Given a (decimal – e.g. 3.72) number that is passed in as a string, print the binary representation.If the number can not be represented accurately in binary, print “ERROR”.
string print_binary(string val) {int pos = val.find('.',0);int intpart = atoi(val.substr(0,pos).c_str());double doublepart = atof(val.substr(pos+1).c_str());string intstr = "";string doublestr = "";while(inpart>0){if(intpart&1)intstr = "1"+intstr;elseintstr = "0"+intstr;intpart>>=1;}while(double>0){if(doublestr.lenght()>32) return "ERROR";doublepart* = 2;if(doublepart>=1){doublestr+="1";doublepart-=1;}else{doublestr +="0";}}return intstr+"."+doublestr; }?
轉載于:https://www.cnblogs.com/xuanxu/p/3255182.html
總結
- 上一篇: Silverlight从客户端上传文件到
- 下一篇: HDU2699+Easy