#include <stdio.h>
#include <stdlib.h>
#define StackSize 100 /*假定预分配的栈空间最多为100个元素*/
typedef int DataType; /*应将顺序栈的DataType定义改为整型*/
typedef struct
{ DataType data[StackSize];
int top;
}SeqStack;
void main()
{
void InitStack(SeqStack *S);
int StackEmpty(SeqStack *S);
int StackFull(SeqStack *S);
void Push(SeqStack *S,DataType x);
DataType Pop(SeqStack *S);
DataType StackTop(SeqStack *S);
void MultiBaseOutput(int N,int B);
int N,B;
printf("请输入欲转换的十进制数:");
scanf("%d",&N);
printf("请输入欲转换成的进制:");
scanf("%d",&B);
MultiBaseOutput(N,B);
}
void InitStack(SeqStack *S)
{ /*将顺序栈置空*/
S->top=-1;
}
int StackEmpty(SeqStack *S)
{
return S->top==-1;
}
int StackFull(SeqStack *S)
{
return S->top==StackSize-1;
}
void Push(SeqStack *S,DataType x)
{ if (StackFull(S))
{ printf("Stack overflow");
exit(0); /*上溢,退出运行*/
}
S->data[++S->top]=x; /*栈顶指针加1后将x进栈*/
}
DataType Pop(SeqStack *S)
{ if (StackEmpty(S))
{ printf("Stack underflow");
exit(0); /*下溢,退出运行*/
}
return S->data[S->top--]; /*栈顶元素返回后将栈顶指针减1*/
}
DataType StackTop(SeqStack *S)
{ if (StackEmpty(S))
{ printf("Stack is empty");
exit(0);
}
return S->data[S->top];
}
void MultiBaseOutput(int N,int B)
{ /*假设N是非负的十进制整数,输出等值的B进制数*/
int i;
SeqStack S;
InitStack(&S);
printf("十进制数%d的%d进制数是",N,B);
while(N)
{ /*从右向左产生B进制数的各位数字,并将其进栈*/
Push(&S,N%B);
N=N/B;
}
while(!StackEmpty(&S))
{ /*栈非空时退栈输出*/
i=Pop(&S);
printf("%d",i);
}
printf("\n");
}