进制转化(10->2)

 

/*  用双向动态链表储存数据,没有数据大小限制  */
/*  可以转换负数  */  
#include 
< stdio.h >
#include 
< malloc.h >
int  main( void )
{
    struct num2 {
        
int  a;
        struct num2 
* next;
        struct num2 
* bellow;
    };
    struct num2 
* p,  * q,  * head,  * end;
    
int  num;
    unsigned x;
    scanf(
" %d " & num);
    x 
=  (unsigned)num;  /*  将有符号变量转化成无符号变量,这样就能将负数转变成补码形式  */  
    p 
=  (struct num2  * )malloc(sizeof(struct num2));
    head 
=  p;
    p
-> bellow  =  NULL;
    p
-> =  x  %   2 ;
    x 
=  x  / 2 ;
    
while  ( 1 ) {
        q 
=  (struct num2  * )malloc(sizeof(struct num2));
        p 
->  next  =  q;
        q 
->  bellow  =  p;
        q
-> =   x  %   2 ;
        x 
=  x  / 2 ;
        p 
=  q;
        
if  (x  ==   0 ) {
            p
-> next  =  NULL;
            end 
=  q;
            
break ;
        }
    }
    p 
=  end;
    
if  (p  !=  NULL)
        
do {
            printf(
" %d " , p -> a);
            p 
=  p -> bellow;
        }
while  (p  !=  NULL); 
    getchar();
    getchar();
    
return   0 ;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值