资源描述
/*利用栈实现数制转换*/
#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(SElemType));
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");
}
展开阅读全文