#include<stdio.h>
#include<stdlib.h>
#define STACKSIZE 100
typedef struct node{
int data;
}NODE ;
typedef struct stack{
NODE *top;
NODE *base;
int StackSize ;//决定了是顺序栈 下文也没有更新StackSize大小
}Mystack;
int InitStack(Mystack *p)
{
p->top = p->base =(NODE *)malloc(sizeof(NODE)*STACKSIZE);
if(p->top == NULL)
{
printf("malloc error \n");
return -1;
}
p->StackSize = STACKSIZE ;
return 0;
}
int IsEmptyStack(Mystack *p)
{
if(p->top == p->base)
return 1;
else return 0;
}
int pop(Mystack *p)
{
NODE temp;
if(IsEmptyStack(p)){
printf("The stack is empty \n");
return 0;
}
temp=*(p->top-1);//通过temp读取top下的第一个元素 node1
// top - node1 -node2 -node3 base
p->top--;//top1占据到node1位置
return temp.data;
}
int push(Mystack *p,int t)
{
NODE element;
if(p->top - p->base == STACKSIZE){
printf("The stack is full \n");
return -1;
}
element.data = t ;
*(p->top) = element ;
p->top++;
}
int ExchangeBin(int x)
{
Mystack ty ;
int tag = 0 ;
int temp = abs(x ) ;
if(x< 0) tag = 1;
InitStack(&ty);
while(temp)
{
push(&ty,temp%2);
temp /=2 ;
//基于辗转相除
}
if(tag) printf(" -");
while(IsEmptyStack(&ty) != 1)
printf("%2d",pop(&ty));
printf("\n\n");
return 0;
}
int main(void)
{
int x;
printf("Please input the \nX :");
scanf("%d",&x);
ExchangeBin(x);
return 0;
}
最后修改:2023 年 12 月 27 日
© 允许规范转载