#include
#include
#include
#define STACK_INIT_SIZE 100 // 存储空间的初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
struct SqStack
{
int *base;
int *top;
int stacksize; //当前已分配的存储空间
};
/*构造一个空栈*/
void InitStack(struct SqStack *S)
{
S->base = (int *)malloc(STACK_INIT_SIZE * sizeof (int));
if(!(S->base)) //存储分配失败
{
exit(0);
printf("内存分配错误!\n");
}
S->top = S->base ;
S->stacksize = STACKINCREMENT ;
}
/*入栈*/
void Push(struct SqStack *S, int e)
{
if(S->top - S->base >= S->stacksize) //栈满溢出
{
S->base = (int *)realloc(S->base,(STACKINCREMENT+STACK_INIT_SIZE)* sizeof (int));
//重新分配存储空间
if(!(S->base))
{
exit(0);
printf("存储失败\n");
}
S->top = S->base + S->stacksize ;
S->stacksize = S->stacksize + STACKINCREMENT ;
}
* (S->top++) = e;
}
/*出栈*/
int Pop(struct SqStack *S, int e)
{
if(S->top == S->base) //空栈
printf("此栈为空栈\n");
e = *( -- S->top);
return(e);
}
/*判断栈是否为空*/
int StackEmpty(struct SqStack *S)
{
if(S->top == S->base)
return 1;
else
return 0;
}
/*主函数*/
int main(int argc, char *argv[])
{
int n = 0;
int e = 1;
struct SqStack *Change;
Change = (struct SqStack *)malloc(sizeof(sturuct SqStack));
InitStack(Change);
printf("Please input the Primitive num (十进制数):");
scanf("%d",&n);
while(n)
{
Push(Change , n%8 );
n = n/8;
}
printf("The result is ( 八 进 制 数 ) : ");
while(!StackEmpty(Change))
{
e = Pop(Change,e);
printf("%d",e);
}
printf("\n");
if(e/8 == 0 )
printf("此栈已经为空了~~~\n");
system("PAUSE");
return 0;
}
阅读(1279) | 评论(0) | 转发(0) |