基于顺序栈的进制转换
#include <stdio.h>
#include <stdlib.h>
#define M 10
typedef struct //定义一个顺序栈
{
int data[M];
int top;
}SqStack;
void InitStack(SqStack &st)//创建一个栈
{
st.top=-1;
}
int PushStack(SqStack &st, int x)//进栈操作
{
if(st.top==M-1)
return 0;
else
{
st.top++;
st.data[st.top]=x;
return 1;
}
}
int PopStack(SqStack &st,int &x) //出栈操作
{
if(st.top==-1)
return 0;
else
{
x=st.data[st.top];
st.top--;
return 1;
}
}
int GetTop(SqStack st,int &x) //取栈顶元素
{
if(st.top==-1)
return 0;
else
{
x=st.data[st.top];
return 1;
}
}
int StackEmpty(SqStack st) //判断栈空
{
if(st.top==-1)
return 1;
else
return 0;
}
void conversion() //进制转换函数
{
int N,e,m;
SqStack S;
InitStack(S);
scanf("%d%d",&N,&m);
while(N)
{
PushStack(S,N%m);
N=N/m;
}
while(!StackEmpty(S))
{
PopStack(S,e);
printf("%d",e);
}
printf("\n");
}
int main()
{
conversion();
return 0;
}
转换进制在2-9之间
请dalao不吝赐教。
浙公网安备 33010602011771号