//stack.h
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack *s); //初始化栈
Status DestroyStack(SqStack* s); //销毁栈
Status ClearStack(SqStack* s); //判空
Status StackEmpty(SqStack* s); //置空
Status StackLen(SqStack* s); //返回栈长度
Status GetTop(SqStack* s,SElemType *e); //返回栈顶元素
Status Push(SqStack *s,SElemType e); //入栈
Status Pop(SqStack *s,SElemType *e); //出栈
Status 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;
}
Status DestroyStack(SqStack* s){
if (s->base != NULL) {
free(s->base);
s->base = NULL;
s->top = NULL;
return OK;
}
else return ERROR;
}
Status ClearStack(SqStack* s){
(*s).top = (*s).base;
return OK;
}
Status StackEmpty(SqStack* s){
if (s->base == s->top) return TRUE;
else return FALSE;
}
Status StackLen(SqStack* s){
return s->top - s->base;
}
Status GetTop(SqStack *s, SElemType *e){
if (StackEmpty(s)) return(ERROR);
*e = *--s->top;
s->top++;
return OK;
}
Status Push(SqStack *s, SElemType e){
if (s->base == NULL) exit(OVERFLOW);
if (StackLen(s) >= s->stacksize) {
s->base = (SElemType*)realloc(s->base,(s->stacksize +STACKINCREMENT) * sizeof(SElemType));
if (!s->base) exit(OVERFLOW);
s->top = s->base + s->stacksize;
s->stacksize += STACKINCREMENT;
}
*s->top = e;
s->top++;
return OK;
}
Status Pop(SqStack *s,SElemType* e) {
if (StackEmpty(s)) return ERROR;
*e=*--s->top;
return OK;
}
#include<stdio.h>
#include"stack.h"
int main(){
SqStack s;
int N,e,n;
InitStack(&s);
printf("请输入一个十进制数");
scanf("%d",&N);
printf("请输入要转换为的进制");
scanf("%d",&n);
while(N){
Push(&s,N%n);
N=N/n;
}
while(!StackEmpty(&s)){
Pop(&s,&e);
printf("%d",e);
}
}