#include
#include
#define ElemType int
#define MAXNUM 50
typedef struct
{ElemType stack[MAXNUM];
int top;
}Qstack;
void InitStack(Qstack *p)
{if(!p) printf("Wrong!");
p->top=-1;}
void Push(Qstack *p,int s)
{if(p->top
{p->top++;
p->stack[p->top]=s;}
else
printf("OverFlow!");
}
void Pop(Qstack *p)
{int i;
ElemType s;
if(p->top!=0)
{for(i=p->top;i>=0;i--)
{s=p->stack[i];
if(s>=10)
{s+='a'-10;
printf("%c",s);}
else
printf("%d",s);
}
printf("\n");}
}
void main()
{ElemType m;
int n,yu;
Qstack *q;
q=(Qstack *)malloc(sizeof(Qstack));
printf("请输入要进行进制转换的十进制数:\n");
scanf("%d",&m);
printf("\n 1---二进制数 \n");
printf("\n 2---八进制数 \n");
printf("\n 3---十六进制数 \n");
printf("请选择要转换成的进制形式(1,2,3):\n");
scanf("%d",&n);
InitStack(q);
printf("\n");
switch(n)
{case 1:if(m<2)
printf("%d\n",m);
else
do
{yu=m%2;
Push(q,yu);
m=m/2;}while(m!=0);
Pop(q);break;
case 2:if(m<8)
printf("%d\n",m);
else
do
{yu=m%8;
Push(q,yu);
m=m/8;}while(m!=0);
Pop(q); break;
case 3:if(m<16)
{if(m>=10)
{m+='a'-10;
printf("%c\n",m);}
else
printf("%d\n",m);}
else
do
{yu=m%16;
Push(q,yu);
m=m/16;}while(m!=0);
Pop(q);break;
}
}
这个是用C来写的,上面那个是用C++的知识来写的.