有了前面的基础,这个就变得比较简单了,这里就不做细说
#include <iostream>
using namespace std;#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef int ElemType;
typedef struct
{
ElemType *base;
ElemType *top;
int stacksize;
}SqStack;
bool InitStack(SqStack &L)
{
L.base=(ElemType *)malloc(STACK_INIT_SIZE*sizeof(ElemType));
if(!L.base) return 0;
L.top=L.base;
L.stacksize=STACK_INIT_SIZE;
return 1;
}
bool IsEmpty(SqStack &L)
{
if(L.top==L.base) return 1;
else return 0;
}
bool Push(SqStack &L,ElemType e)
{
if(L.top-L.base>=L.stacksize)
{
L.base=(ElemType *)realloc(L.base,STACKINCREMENT*sizeof(ElemType));
if(!L.base) return 0;
L.top=L.base+L.stacksize;
L.stacksize+=STACKINCREMENT;
}
*L.top++=e;
return 1;
}
bool Pop(SqStack &L,ElemType &e)
{
if(L.top==L.base) return 0;
e=*(--L.top);
return 1;
}
void Conversion()
{
int n;
ElemType elem;
SqStack Stack;
InitStack(Stack);
cout<<"Please input the number that you want to change:"<<endl;
cin>>n;
while(n)
{
Push(Stack,n%8);
n=n/8;
}
while(!IsEmpty(Stack))
{
Pop(Stack,elem);
cout<<elem;
}
cout<<endl;
}
int main()
{
Conversion();
return 0;
}