按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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值