#include<stdio.h>
#include<stdlib.h>
#define InitSize 100 //代表存储空间的初始分配量
//定义栈的结构体
typedef struct stacks
{
int * top;
int * base;
int stacksize;
}SqStack;
//构造一个空栈
void InitStack(SqStack *s)
{
s->base = (int *)malloc(InitSize * sizeof(int));
if(!s->base) printf("分配失败!");
s->top = s->base;
s->stacksize = InitSize;
//printf("建立成功!
");
}
//入栈函数
void Push(SqStack *s, int e)
{
if(s->top-s->base>=s->stacksize)
{
s->base = (int *)realloc(s->base,(s->stacksize+100)*sizeof(int));
s->top = s->base +s->stacksize;
s->stacksize += 100;
}//printf("栈满!");
*s->top = e;
s->top++;
}
//出栈函数
int Pop(SqStack *s, int *p)
{
int i = 0;
if(s->top == s->base) printf("栈为空!");
else
{
while(s->top!=s->base)
{
s->top--;
p[i] = *(s->top);
i++;
}
}
return i;
}
//转化函数
void Change(int n, SqStack *s, int *sp)
{
int i, j;
while(n)
{
Push(s, n%8);
n = n / 8;
}
i = Pop(s, sp);
for(j=0; j<i; j++)
{
printf("%d ", *(sp+j));
}
}
int main()
{
int n,e[InitSize], *sp;
SqStack s;
sp = e;
InitStack(&s);//这里是将结构体变量的地址作为实参进行传递
printf("请输入十进制数:");
scanf("%d", &n);
printf("转化成八进制数为:");
Change(n, &s, sp);
printf("
");
return 0;
}