#include
#include
#include
#define OK 1
#define ERROR 0
#define OVERFLOW -1
typedef int Status;
typedef int ElemType;
#define S_SIZE 100
#define STACKINCREAMENT 10
typedef struct{
int base;
int *top;
int stacksize;
}SqStack;
Status InitStack(SqStack &S)
{
S.base=(int *)malloc(S_SIZE * sizeof(int));
if(!S.base)exit(OVERFLOW);
S.top=S.base;
S.stacksize=S_SIZE;
return OK;
}
Status GetTop(SqStack S,int &e){
if(S.top==S.base) return ERROR;
e=(S.top-1);
return OK;
}
Status Push(SqStack &S,int e){
if(S.top-S.base>=S.stacksize){
S.base=(int )realloc(S.base,(S.stacksize+STACKINCREAMENT)*sizeof(int));
if(!S.base)exit(OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREAMENT;
}
*S.top++=e;
return OK;
}
Status Pop(SqStack &S,int &e){
if(S.top==S.base)return ERROR;
e=--S.top;
return OK;
}
Status StackEmpty(SqStack S)
{
if(S.top==S.base)
return OK;
else
return ERROR;
}
Status GetTop(SqStack S,int e){
if(S.top){
if(S.top==S.base)return ERROR;
e=*(S.top-1);
printf("%d",e);
return OK;
}
void ba(int N){
InitStack(S);
scanf("%d",N);
while(N){
Push(S,N%8);
N=N/8;
}
while(!StackEmpty(S)){
Pop(S,e);
printf("%d",e);
}
}
void er(int M){
InitStack(S);
scanf("%d",M);
while(M){
Push(S,M%2);
M=M/2;
}
while(!StackEmpty(S)){
Pop(S,e);
printf("%d",e);
}
}
void shiliu(int C){
InitStack(S);
scanf("%d",C);
while(C){
if(C%16<=9)
Push(S,C%16);
else
switch(C%16){
case 10:Push(S,'A');break;
case 11:Push(S,'B');break;
case 12:Push(S,'C');break;
case 13:Push(S,'D');break;
case 14:Push(S,'E');break;
case 15:Push(S,'F');break;
}
C=C/16;
}
}
void main(){
int a;
int b;
InitStack(SqStack &S);
printf("输入需要转换进制的数字:");
scanf("%d",&a);
printf("\n");
printf("输入转后的进制:");
scanf("%d",&b);
switch(b){
case 2: er(a);GetTop(&S,e);break;
case 8: ba(a);GetTop(&S,e);break;
case 16: shiliu(a);GetTop(&S,e);break;
}
}