利用堆栈实现的十进制转二进制程序实现

#include #include #define STACK_INIT_SIZE 10#define STACK_INCR_SIZE 2#define OK 1#define ERROR 1#define TURE 1#define FALSE 0typedef int STATUS; typedef int SELemType;typedef struct {SELemType *top;SELemType *base; int stacksize;}SeqStack;//++++++++++++++++++++++++++栈的初始化++++++++++++++++++++++++++++++++int InitStack (SeqStack *S){ S->base = (SELemType *)malloc(STACK_INIT_SIZE * sizeof (SeqStack)); if(!(*S->base)) { printf("The memory allocation is failed !"); return ERROR; } S->top=S->base; S->stacksize = STACK_INIT_SIZE; return OK;}//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++int GetTop(SeqStack S,SELemType *e){ if(S.base==S.top) { printf("The Stack is empty !\n");return ERROR; } *e=*(S.top-1); return OK;}//+++++++++++++++++++++++++入栈push操作+++++++++++++++++++++++++++++++++int Push(SeqStack *S, SELemType e){ if((S->top - S->base )== S->stacksize) { S->base = (SELemType *)malloc((S->stacksize+STACK_INCR_SIZE) * sizeof (SeqStack)); if(!S->base){printf("The memory allocation is failed !\n");return ERROR;} S->top=S->base+S->stacksize; S->stacksize+=STACK_INCR_SIZE; } *S->top++=e; return OK;}//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//++++++++++++++++++++++++++出栈pop操作++++++++++++++++++++++++++++++++int Pop(SeqStack *S,SELemType *e){ if (S->base == S->top) { printf("The stack is empty! Couldn't pop element from stack!\n "); } *e = *(--S->top); return OK;}//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//++++++++++++++++++++++判断堆栈是否为空+++++++++++++++++++++++++++++++++int StackIsEmpty(SeqStack S){ if (S.base==S.top) return TURE; else return FALSE;}//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++//+++++++++++++++++++++数值转换为2进制实现++++++++++++++++++++++++++++++int convert(void){SELemType num; SELemType Outnum; SeqStack S; InitStack(&S); printf("Please input the number you want to conver to Binary:"); scanf("%d",&num); while (num){ Push(&S,num%2); num=num/2; } printf("The output number sequence is:"); while(!StackIsEmpty(S)) { Pop(&S,&Outnum); printf("%d",Outnum); } printf("\n"); return OK;}//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++int main (){ convert(); return OK;}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值