进制转换 【栈】

来源:互联网 发布:arm linux gcc 64下载 编辑:程序博客网 时间:2024/06/01 08:22

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

#include<iostream>#define MAXSIZE 100using 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);    }}


0 0
原创粉丝点击