设计一个算法 将十进制数转化成二进制数

大神在线求帮请问下,设计一个算法 将十进制数转化成二进制数
最新回答
你与北诗

2024-09-30 03:38:51

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct SqStack
{
int *base;
int *top;
int stacksize;
}
SqStack;
void InitStack(SqStack *S)
{
S->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int));
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
}
void Push(SqStack *S,int e)
{
if(S->top-S->base>=S->stacksize)
{
S->base=(int*)realloc(S->base,
(S->stacksize+STACKINCREMENT)*sizeof(int));
S->top=S->base+S->stacksize;
S->stacksize+=STACKINCREMENT;
}
*(S->top)=e;
S->top++;
}
int Pop(SqStack *S)
{
S->top --;
return *S->top;

}
int StackEmpty(SqStack *S)
{
if(S->top == S->base )
return 1;
else
return 0;
}
void conversion(int a)
{
SqStack S;int e;
InitStack(&S);
while(a)
{
Push(&S,a%2);
a=a/2;
}
while(!StackEmpty(&S))
{
e=Pop(&S);
printf("%d",e);
}

}
void main()
{
int a;
printf("输入个十进制数\n");
scanf("%d",&a);
printf("二进制为:\n");
conversion(a);
}
这样的,有问题联系