|  
 
 
C++奋斗小蜗牛在请客源代码 思路:没啥说的,本想用java写,没找到double进制转换的对应函数。。就该用c++,本想偷懒下,直接用string,然后找个string转化double的函数,最后还是失败了 用sstream,格式会变。。发现其实最简单的方法就是直接用数组模拟。。一直想偷懒下,结果绕了一个圈。。。 
先提供个double型化二进制的模版,这里不考虑格式。。 
 [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.}   #include<sstream> #include<stdlib.h> #include<algorithm> #include<iostream> using namespace std; string::iterator it; string Trans(long Integer) {     if(Integer == 0) return "0";     string temp="";     while(Integer)     {         temp = char(Integer%2+48)+temp;         Integer/=2;     }     return temp; } string Trans(double Decimal) {     string temp=".";     int n = 20;     while(Decimal&&n)     {         Decimal*=2;         temp = temp + char(int(Decimal)+48);         Decimal = Decimal - int(Decimal);         n--;     }     return temp; } int main() {     int i,j;     double x;     while(cin>>x)     {         long Integer = long(x);         double Decimal = x-Integer;         double ans;         string Ans = Trans(Integer) + Trans(Decimal);         cout<<Ans<<endl;     } } 
本题AC代码: [1] [2] 下一页  
 |