//main.c
#include<stdio.h>
#include<stdlib.h>
#include"head.h"
int main(int argc,const char *argv[])
{LinkList L = create();if(NULL == L){printf("create失败\n");return -1;}int num;printf("请输入一个整数:");scanf(" %d",&num);transform(L,num);show(L);free(L);L=NULL;return 0;
}
// head.h
#ifndef __HEAD_H__
#define __HEAD_H__
#define MAX 50
typedef struct
{int data[MAX];int top;
}SeqList,*LinkList;LinkList create();
int transform(LinkList L,int num);
void show(LinkList L);#endif
//head.c
#include<stdio.h>
#include<stdlib.h>
#include"head.h"LinkList create()
{LinkList L = (LinkList)malloc(sizeof(SeqList));if(L == NULL){printf("create失败\n");return NULL;}L->top = -1;return L;
}int transform(LinkList L,int num)
{if(NULL == L || num < 0){printf("transform失败\n");return -1;}while(num >= 2){L->top++;L->data[L->top] = num % 2; num /= 2;}L->top++;L->data[L->top] = num % 2;return 0;
}void show(LinkList L)
{if(NULL == L || L->top < 0){printf("show失败\n");return;}printf("转换结果:");for(int i = L->top; i >= 0; i--){printf("%d",L->data[i]);}putchar(10);
}