从文件读入数据将其转化为2进制输出

来源:互联网 发布:吴文化网络课答案 编辑:程序博客网 时间:2024/06/11 00:04
/**从文件读入数据将其转化为2进制输出*/#include <iostream>#include <fstream>#include <cmath>using namespace std;long convert(long a){    long sum = 0;    int i = 0;    while (a != 0)    {        sum = sum + (a % 2) * pow(10,i);        a /= 2;        i++;    }    return sum; }int main(int argc, char const *argv[]){    ifstream in("input.txt", ios_base::in);    if(!in)    {        cerr << "can't open the file!" << endl;    }    else    {        long num;        while (in >> num)        {            cout << convert(num) << endl;        }    }    return 0;}

// by AutuanLiu

源码下载

0 0
原创粉丝点击