将10进制数转换成2进制,8进制,16进制输出

#include <iostream>
using namespace std;
#define N 2//8 16

struct Node{
    
int data;
    Node
* next;
    Node(
const int& t=int()):data(t),next(NULL){}
}
;

void dec2bin(int num);
int main()
{
    Node
* head=NULL;
    
int num;
    cin
>>num;
    
int num2=num;
    
while(num!=0){
        Node
* p=new Node(num%N);
        p
->next=head;
        head
=p;
        num
/=N;
    }

    Node 
*q=head;
    
while(q!=NULL){
        
if(q->data<10)
            cout
<<q->data;
        
else
            cout
<<char(q->data-10+'a');
        q
=q->next;
    }

    cout
<<endl;
    
while(head!=NULL){
        q
=head->next;
        delete head;
        head
=q;
    }

    cout
<<"---------------"<<endl;
    dec2bin(num2);
    system(
"pause");
    
return 0;
}

///////////////////////
//10->2进制     位运算方法//
///////////////////////
void dec2bin(int num){
    
int    intbitlen = sizeof(int)*8;
    
for(int i=intbitlen-1;i>=0;i--){
        
if((1<<i)&num)
            cout
<<'1';
        
else 
            cout
<<0;
    }

    cout
<<endl;
}



posted on 2011-06-10 12:41 Hsssssss 阅读(419) 评论(0)  编辑 收藏 引用 所属分类: C++代码


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年5月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿

文章分类

文章档案

收藏夹

搜索

最新评论