用栈实现10进制转换为8进制

#include 
#define STACK_INIT_SIZE 100
#define OVERFLOW -2
#define OK 1
typedef int SElemType;
typedef struct {
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;



int InitStack (SqStack *S) {
	S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
	if(!S->base) exit(OVERFLOW);
	S->top = S->base;
	S->stacksize = STACK_INIT_SIZE;
	return OK;
	
}
int main() {
	SqStack S;
	int a,e;
	InitStack(&S);
	int N;
	int count = 0;
	scanf("%d",&N);
	while(N) {	
	*(S.base+count) = N%8;
	N=N/8;
	++count;
	S.top++;
	}
	while(S.top != S.base) {
		printf("%d",*(S.top - 1));
		S.top--;
	}
	printf("\n");
}

 

你可能感兴趣的:(数据结构)