在C++中用栈实现进制之间的转换

众所周知,用栈可以消除递归,见下例:
 
  
 
  
 
  
#include
using namespace std;
#define STACK_INIT_SIZE 100
struct Stack{
int*base;
int*top;
int stacksize;
};
void conversion();
int InitStack(Stack*S);
int Push(Stack*S,int e);
int Pop(Stack*S,int*e);
int StackEmpty(Stack*S);
int main(){
conversion();
return 0;
}
void conversion(){
//对于一个十进制,打印输出与其等值的八进制
Stack S;
    int e;
InitStack(&S);
int n,m;
cout<<"请输入一个整数"<>n;
cout<<"输入此整数所要转换的进制数"<>m;
while(n){
   Push(&S,n%m);
   n=n/m;
}
while(!StackEmpty(&S)){
   Pop(&S,&e);
        cout<base=new int[STACK_INIT_SIZE]; 
S->top=S->base;
S->stacksize=STACK_INIT_SIZE;
return 0;
}
int Push(Stack*S,int e){
*S->top=e;
    S->top++;
return 0;
}
int Pop(Stack*S,int*e){
S->top--;
*e=*S->top;
    return 0;
}
int StackEmpty(Stack*S){
if(S->top==S->base) return 1;
else return 0;
}


 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(c++)