九天雁翎的博客
如果你想在软件业获得成功,就使用你知道的最强大的语言,用它解决你知道的最难的问题,并且等待竞争对手的经理做出自甘平庸的选择。 -- Paul Graham

按weskercn的思路写的程序,问题(2)解答:把键盘输入的16,10,8进制数转换为2进制输出

//因为不能回复,老是提示校检码错误,所以单独发出来。

#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <bitset>
#include <iomanip>
#include <cassert>
#include <limits>

using namespace std;

typedef enum _transmode
{
    HEX_MODE = 1,
    DEC_MODE,
    OCT_MODE
} transmode;

int main()
{
    string instr;           //这就是输入,string类型
    long outlong;           //转换成进制的长整数
    int n_Numformat;
    cout << "Please chose the input number format(1-Hex,2-Dec,3-Oct)" << endl;
    cin >> n_Numformat;
    cout << "Please input the number to be transformed:";
    cin >> instr;

    switch (n_Numformat)   //这里你的风格好于我
    {
    case HEX_MODE:
        n_Numformat = 16;
        break;
    case DEC_MODE:
        n_Numformat = 10;
        break;
    case OCT_MODE:
        n_Numformat = 8;
        break;
    default:
        cout << "Error input number,exit" << endl;
        exit(1);
    }

    outlong = strtol(instr.c_str(), NULL, n_Numformat);
    assert(outlong != 0);  //假如转换失败,断言错误,这是你的方法的最好的地方
    bitset<numeric_limits<long>::digits> abit(outlong);
    cout << "The binary number is" << abit << endl; //没有管格式了
    return 0;
}

分类:  C++ 
标签:  C++ 

Posted By 九天雁翎 at 九天雁翎的博客 on 2007年06月09日

前一篇: 关于C++的学习再思考(2) 后一篇: 学习C++的再思考(3)