十进制正整数化为八进制
#include//2012非负十进制化八进制
#include
#include//含有OVERFLOW的值
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 10//初始分配
#define STACKINCRTMENT 2//增量
typedef int Status;
typedef struct SqStack
{
int *base;
int *top;
int stacksize;
}SqStack;//顺序栈
Status InitStack(SqStack *S)//造空栈
{
(*S).base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status push(SqStack *S,int e)//入栈
{
if((*S).top-(*S).base>=(*S).stacksize)
{
(*S).base=(int *)realloc((*S).base,((*S).stacksize+STACKINCRTMENT)*sizeof(int));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCRTMENT;
}
*((*S).top)++=e;
return 0;
}
Status pop(SqStack S,int *e)//出栈
{
if((*S).base==(*S).top)
return ERROR;
*e=--(*S).top;
return OK;
}
Status StackEmpty(SqStack S)//判空
{
if(S.base==S.top)
return OK;
else
return ERROR;
}
void conversion(int n)//十化八
{
SqStack s;
int e;
InitStack(&s);
while(n)
{
push(&s,n%8);
n=n/8;
}
while(!StackEmpty(s))
{
pop(&s,&e);
printf("%d",e);
}
printf("\n");
}
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
conversion(n);
}
return 0;
}
#include//2012非负十进制化八进制
#include
#include//含有OVERFLOW的值
#define OK 1
#define ERROR 0
#define STACK_INIT_SIZE 10//初始分配
#define STACKINCRTMENT 2//增量
typedef int Status;
typedef struct SqStack
{
int *base;
int *top;
int stacksize;
}SqStack;//顺序栈
Status InitStack(SqStack *S)//造空栈
{
(*S).base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base;
(*S).stacksize=STACK_INIT_SIZE;
return OK;
}
Status push(SqStack *S,int e)//入栈
{
if((*S).top-(*S).base>=(*S).stacksize)
{
(*S).base=(int *)realloc((*S).base,((*S).stacksize+STACKINCRTMENT)*sizeof(int));
if(!(*S).base)
exit(OVERFLOW);
(*S).top=(*S).base+(*S).stacksize;
(*S).stacksize+=STACKINCRTMENT;
}
*((*S).top)++=e;
return 0;
}
Status pop(SqStack S,int *e)//出栈
{
if((*S).base==(*S).top)
return ERROR;
*e=--(*S).top;
return OK;
}
Status StackEmpty(SqStack S)//判空
{
if(S.base==S.top)
return OK;
else
return ERROR;
}
void conversion(int n)//十化八
{
SqStack s;
int e;
InitStack(&s);
while(n)
{
push(&s,n%8);
n=n/8;
}
while(!StackEmpty(s))
{
pop(&s,&e);
printf("%d",e);
}
printf("\n");
}
int main(void)
{
int n;
while(scanf("%d",&n)!=EOF)
{
conversion(n);
}
return 0;
}