📄 10to8.c
字号:
//用栈实现10进制转8进制
#define STACK_INIT_SIZE 100 //存储空间初始分配量
#define STACKINCREMENT 10 //存储空间分配增量
#include <iostream.h>
#include <stdlib.h>
typedef struct
{
int *base; //在栈构造之前和销毁之后,base的值为NULL
int *top; //栈顶指针
int stacksize; //当前已分配的存储空间,以元素为单位
}SqStack;
SqStack S;
void InitStack(SqStack& S) //构造一个空栈
{
S.base=(int *)malloc(STACK_INIT_SIZE*sizeof(int));
if(!S.base)
exit(0);
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
}
void Push(SqStack& S,int e) //插入元素e为新的栈顶元素
{
if((S.top-S.base)>=S.stacksize) //如果栈满,追加存储空间
{
S.base=(int *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));
if(!S.base)
exit(0);
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
*S.top++=e;
}
int Pop(SqStack& S,int& e)
{
if(S.top==S.base)
return 0;
e=*--S.top;
}
int StackEmpty(SqStack S)
{
if(S.top==S.base)
return 1;
else
return 0;
}
void conversion()
{
int N,e;
InitStack(S);
cout<<"Input N:"<<endl;
cin>>N;
while(N)
{
Push(S,N%8);
N/=8;
}
while(!StackEmpty(S))
{
Pop(S,e);
cout<<e;
}
cout<<endl;
}
void main()
{
conversion();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -