#include #include #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define NULL 0 #define OVERFLOW 0 #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 typedef int Status; typedef int SElemType;
typedef struct {
SElemType *base;
SElemType *top;
int stacksize; }SqStack; Status InitStack (SqStack &S) {
S.base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType));
if(!S.base) exit (OVERFLOW);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return OK; }
Status GetTop(SqStack S,SElemType &e) {
if(S.top==S.base) return ERROR;
e= *(S.top-1);
return OK; }
Status Push(SqStack &S,SElemType e) {
if(S.top-S.base>=S.stacksize)
{
S.base=(SElemType*)realloc (S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));
if(!S.base) exit (OVERFLOW);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
return OK; }
Status Pop(SqStack &S,SElemType &e) {
if(S.top==S.base)return ERROR;
e= *--S.top;
return OK; }
Status StackEmpty(SqStack S) {
if(S.top==S.base)return TRUE;
else return FALSE; }
int main() {
int N,t;
SElemType e; SqStack S; InitStack(S); printf(\"需要转化的十进制数:\\n\");
while(scanf(\"%d\",&N)!=EOF)
{
printf(\"需要转化成(2,8,16):\\n\");
scanf(\"%d\",&t);
while(N)
{
Push(S,N%t);
N=N/t;
}
printf(\"转化后的数是:\\n\"); while(!StackEmpty(S))
{
Pop(S,e); switch (e) {
case 10: printf(\"A\");break;
case 11: printf(\"B\");break;
case 12: printf(\"C\");break;
case 13: printf(\"D\");break;
case 14: printf(\"E\");break;
case 15: printf(\"F\");break;
default: printf(\"%d\",e);break;
}
} } getchar(); getchar(); return 0; }