/* 用双向动态链表储存数据,没有数据大小限制 */
/* 可以转换负数 */
#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 -> a = x % 2 ;
x = x / 2 ;
while ( 1 ) {
q = (struct num2 * )malloc(sizeof(struct num2));
p -> next = q;
q -> bellow = p;
q -> a = 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->2)
最新推荐文章于 2022-04-27 21:33:33 发布