C语言静态顺序栈实现进制转换

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX_STACK_SIZE 100 //栈的大小
#define ERROR -1
#define OK 1
typedef struct sqstack
{
    int stack_array[MAX_STACK_SIZE];
    int top;
    int bottom;
}SqStack;
///栈的初始化
int Init_Stack(SqStack *S)
{
    S->bottom=S->top=0;
}
///进栈
int PushStack(SqStack *S, int e)
{
    //使数据元素e进栈成为新的栈顶元素
    if(S->top==MAX_STACK_SIZE-1)
    {
        printf("栈满,返回错误标志");
        return ERROR;
    }
    S->top=S->top+1;
    S->stack_array[S->top]=e;
    return OK;
}
///出栈
int PopStack(SqStack *S, int e)
{
    if(S->top==0)
    {
        printf("栈空,无法出栈元素");
        return ERROR;
    }
    e=S->stack_array[S->top];
    S->top--;
    return OK;
}
///遍历栈
int StackTravel(SqStack *S)
{
    int e;
    int ptr;
    ptr=S->top;
    while(ptr>S->bottom)
    {
        e=S->stack_array[ptr];
        ptr=ptr-1;
        printf("%d",e);
    }
    return OK;
}




int main()
{
    SqStack stack2;
    Init_Stack(&stack2);
    int k;
    int e;
    int n;
    int d;
    n=1348;
    d=8;
    while(n>0)
    {
        k=n%d;
        PushStack(&stack2,k);
        n=n/d;
    }
    printf("将%d转换为%d进制为:",n,d);
    StackTravel(&stack2);

}



猜你喜欢

转载自blog.csdn.net/qq_20406597/article/details/81019184