//给你一个十进制转换为八进制的,其他类似的改一下就是了
#include"stdio.h"
#include"malloc.h"
#include"util.h"
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
#define OVERFLOW -2
#define OK 1
#define TRUE 1
#define FALSE 0
#define ERROR 0
typedef int SElemType;
typedef int Status;
typedef struct{
SElemType *base;
SElemType *top;
int stacksize;
}SqStack;
Status InitStack(SqStack & S)//栈的初始化
{
S.base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base;
S.stacksize = 100;
return OK;
}
/*Status GetTop(SqStack S,SElemType &e)
{
if(S.top==S.base)
return ERROR;
e = *(S.top-1);
printf("获得栈顶元素:");
printf("%3dn",e);
return OK;
}*/
Status Push(SqStack &S,SElemType e)
{
if(S.top-S.base>=S.stacksize)
{
S.base = (SElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base)
exit(OVERFLOW);
S.top = S.base + S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,SElemType &e)
{
//若栈不为空,则删除s的栈顶元素,用e返回其值
if(S.top==S.base)
return ERROR;
--S.top;
e = *S.top;
return OK;
}
bool StackEmpty(SqStack &S)
{
if(S.top=S.base)
return TRUE;
else
return FALSE;
}
void main()
{
SqStack S;
int N,t;
printf("请输入要转换的十进制数:");
scanf("%d",&N);
InitStack(S);
while(N)
{
Push(S,N%8);
N=N/8;
}
printf("转换后的八进制数:");
while(!StackEmpty(S))
{
Pop(S,t);
printf("%2d",t);
}
printf("n");
}