设为首页 加入收藏

TOP

十进制转二进制-快速算法
2015-07-24 05:54:03 来源: 作者: 【 】 浏览:5
Tags:十进制 二进制 快速 算法
#include
  
   
#include
   
  
#include
  
   
using namespace std;
int main(int agrc, char *agrv[])
{
	int iInPut = 0;
	while (cin >> iInPut)
	{

		string sBinary;//转换后的二进制存储为字符串,调用了默认构造函数初试化为空串
		int temp = abs(iInPut);
		if (temp == 0)
		{	
			//cout.width(11);//以11位的宽度右对齐输出
			cout << "          0-->0\n";
			continue;
		}

		while (temp)
		{
			if (temp & 0x01)
			{
				sBinary += '1';
			}
			else
			{
				sBinary += '0';
			}
			temp >>= 1;//对正数右移,高位补0
		}
		reverse(sBinary.begin(), sBinary.end());
		const char *cOutPut = sBinary.c_str();
		cout.width(11);
		cout << iInPut << (iInPut > 0 ? "-->" : "-->-") << cOutPut << endl;
	}
	return 0;
}
  

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++语言笔记系列之十――静态成员 下一篇bnu 34895 Elegant String(矩阵快..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: