//-----用栈完成十进制与八进制之间的转换-----


#include<iostream>
#define MAXSIZE 100
using namespace std;

typedef struct{
int *base;
int *top;
int stacksize;
}SqStack;

int InitStack(SqStack &S){
S.base=new int[MAXSIZE];
if(!S.base) exit(-1);
S.top=S.base;
S.stacksize=100;
return 1;
}

int Push(SqStack &S, int e){//压栈
if(S.top-S.base==S.stacksize) return 0;
*S.top++=e;
return 1;
}

int Pop(SqStack &S, int &e){
if(S.base==S.top) return 0;
e=*--S.top;
return 1;
}

int StackEmpty(SqStack &S){
if(S.top==S.base)
return 1;
return 0;
}

void conversion(SqStack &S){
InitStack(S);
int N,e;
cout<<"输入十进制数:"<<endl;
cin>>N;
while(N){
Push(S,N%8);
N/=8;
}
cout<<"转换后的八进制为:"<<endl;
while(!StackEmpty(S)){
Pop(S,e);
cout<<e;
}
cout<<endl<<endl;
}

int main(){
while(1){
SqStack S;
conversion(S);
}
}


进制转换 【栈】_ios