实验12

时光总嘲笑我的痴心妄想 提交于 2019-12-03 10:20:01

#include <stdio.h>

#include <stdlib.h>

#define OK 1

#define ERROR 0

#define NO 0

#define ADD_SIZE 20

#define INIT_SIZE 10

typedef int Status;

typedef struct

{

    int *top;

    int *base;

    int stacksize;

}SqStack;

Status InitStack(SqStack *S)

    S->base=(int *)malloc(INIT_SIZE*sizeof(int));

    if(!S->base)

        printf("ERROR!\n");

    S->top=S->base;

    S->stacksize=INIT_SIZE;

    return OK;

}

Status Push(SqStack *S,int e)

{

    if(S->top-S->base>=S->stacksize)

    {

        S->base=(int *)malloc((INIT_SIZE+ADD_SIZE)*sizeof(int));

        if(!S->base)

            printf("ERROR!\n");

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

        S->stacksize+=ADD_SIZE;

    }

    *S->top++=e;

    return OK;

}

Status Pop(SqStack *S)

{

    int e;

    e=*--S->top;

    return e;

}

 

int main()

{

    int N;

    int m;

    int out_stack_value;

    SqStack S;

    InitStack(&S);

         printf("输入十进制数:");

    scanf("%d",&N);

    while(N!=0)

    {  

        m=N%8;

        Push(&S,m);

        N=N/8;

    }

    while(S.top-S.base>0)

    {

        out_stack_value=Pop(&S);

        printf("%d",out_stack_value);

    }

    printf("为八进制数\n");

    return 0;

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!