#include<stdio.h> #include<malloc.h> #define ERROR 0 #define TRUE 1 #define FALSE 0 #define OK 1 #define EQUAL 1 #define OVERFLOW -1 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int SElemType; typedef int Status; struct STACK { SElemType num; SElemType *base; SElemType *top; int stacksize; }; typedef struct STACK SqStack; typedef struct STACK *pSqstack; Status InitStack(SqStack **S); Status DestroyStack(SqStack *S); Status ClearStack(SqStack *S); Status StackEmpty(SqStack S); int StackLength(SqStack S); Status GetTop(SqStack S,SElemType *e); Status Push(SqStack *S,SElemType e); Status Pop(SqStack *S,SElemType *e); Status StackTraverse(SqStack S,Status (*visit)()); Status InitStack(SqStack **S) { (*S)=(SqStack *)malloc(sizeof(SqStack)); (*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) { free(S->base); free(S); } Status ClearStack(SqStack *S) { S->top=S->base; } Status StackEmpty(SqStack S) { if(S.top==S.base) return TRUE; else return FALSE; } int StackLength(SqStack S) { int i; SElemType *p; i=0; p=S.top; while(p!=S.base) {p++; i++; } } Status GetTop(SqStack S,SElemType *e) { if(S.top==S.base) return ERROR; *e=*(S.top-1); return OK; } Status Push(SqStack *S,SElemType e) { /* if(S->top - S->base>=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; return OK; } Status Pop(SqStack *S,SElemType *e) { if(S->top==S->base) return ERROR; *e=*--S->top; return OK; } Status StackPrintElem(SElemType * e) { printf("%d\n",e); } Status StackTraverse(SqStack S,Status (*visit)()) { while(S.top!=S.base) visit(--S.top); } void Conversion(){ pSqstack S; SElemType e; int n; InitStack(&S); printf("Input a number to convert to OCT:\n"); scanf("%d",&n); if(n<0) { printf("\nThe number must be over 0."); return; } if(!n) Push(S,0); /*Èç¹ûnΪ0ʱ,Ó¦Êä³ö0*/ while(n){ Push(S,n%8); n=n/8; } printf("the result is: "); while(!StackEmpty(*S)){ Pop(S,&e); printf("%d",e); } } int main() { SElemType e; SqStack *Sa; Conversion(Sa); getch(); return 0; }</malloc.h></stdio.h>
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)