利用顺序栈实现数制转换
#include<stdio.h>
//3-1存储结构
#define MAXSIZE 100
typedef struct
{
int *base;
int *top;
int stacksize;
}SqStack;
//3-2初始化
void Init(SqStack &S)
{
S.base=new int[MAXSIZE];
S.top=S.base;
S.stacksize=MAXSIZE;
}
//3-3入栈
void Push(SqStack &S,int e)
{
if(S.top-S.base==S.stacksize){
return;
}
*S.top++=e;
}
//3-4出栈
int Pop(SqStack &S)
{
if(S.top==S.base){
return -1;
}
return *--S.top;
}
//3-5进制转换
void zhuan(int N)
{
SqStack S;
Init(S);
while(N){
Push(S,N%8);
N=N/8;
}
while(S.top!=S.base){
printf("%d",Pop(S));
}
}
//主函数
int main(){
int N;
printf("请输入十进制整数N:");
scanf("%d",&N);
printf("十进制整数%d对应的八进制数是:",N);
zhuan(N);
printf("\n");
}
![Xnip2020-12-21_21-54-01](https://img-blog.csdnimg.cn/img_convert/6943603efaa90fb72a7abb0a628008a1.png)