利用栈实现数制转换

  • 格式:doc
  • 大小:28.00 KB
  • 文档页数:2

下载文档原格式

  / 5
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*利用栈实现数制转换*/

#include "stdio.h"

#include "malloc.h"

#define ERROR 0

#define OK 1

#define STACK_INIT_SIZE 100

#define STACKINCREMENT 10

typedef int SElemType;

typedef struct stack{

SElemType *top;

SElemType *bottom;

int stacksize;

}SqStack;

int InitStack(SqStack *S)

{

S->bottom=(SElemType*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

if(!S->bottom) return ERROR;

S->top=S->bottom;

S->stacksize=STACK_INIT_SIZE;

return OK;

}

int Push(SqStack *S,SElemType e)

{

if(S->top-S->bottom>=S->stacksize-1){

S->bottom=(SElemType*)realloc(S->bottom,(S->stacksize+STACKINCREMENT)*sizeof(S ElemType));

if(!S->bottom) return ERROR;

S->top=S->bottom+S->stacksize;

}

*S->top++=e;

return OK;

}

int Pop(SqStack *S,SElemType *e)

{

if(S->top==S->bottom)

return ERROR;

*e=*--S->top;

return OK;

}

int StackEmpty(SqStack S)

{

if(S.top==S.bottom) return 1;

else return 0;

}

void main()

{

SqStack myStack;

int N,e;

InitStack(&myStack);

printf("请输入N:");

scanf("%d",&N);

while(N){

Push(&myStack,N%8);

N=N/8;

}

while(!StackEmpty(myStack)){

Pop(&myStack,&e);

printf("%d",e);

}

printf("\n");

}