[cpp] view plaincopyprint? 01.#include<sstream> 02.#include<stdlib.h> 03.#include<algorithm> 04.#include<iostream> 05.using namespace std; 06.string::iterator it; 07.string Trans(long Integer) 08.{ 09. if(Integer == 0) return "0"; 10. string temp=""; 11. while(Integer) 12. { 13. temp = char(Integer%2+48)+temp; 14. Integer/=2; 15. } 16. return temp; 17.} 18.string Trans(double Decimal) 19.{ 20. string temp="."; 21. int n = 20; 22. while(Decimal&&n) 23. { 24. Decimal*=2; 25. temp = temp + char(int(Decimal)+48); 26. Decimal = Decimal - int(Decimal); 27. n--; 28. } 29. return temp; 30.} 31.int main() 32.{ 33. int i,j; 34. double x; 35. while(cin>>x) 36. { 37. long Integer = long(x); 38. double Decimal = x-Integer; 39. double ans; 40. string Ans = Trans(Integer) + Trans(Decimal); 41. //cout<<Ans<<endl; 42. 43. /*根据题目进行的格式控制、、*/ 44. int n = Ans.length(); 45. while(--n) 46. { 47. it = Ans.end()-1; 48. if(Ans[n] == '0') 49. Ans.erase(it); 50. else if(Ans[n] == '.') 51. { 52. Ans.erase(it); 53. break; 54. } 55. else 56. break; 57. } 58. cout<<Ans<<endl; 59. /* 60. stringstream ss; 61. ss<<Ans; 62. ss>>ans; 63. ss.clear(); 64. cout<<ans<<endl; 65. */ 66. } 67.} 上一页 [1] [2]
|