#include <stdio.h>
#include <stdbool.h>
#define STACK_INIT_SIZE 100
#define STACKINCREMENT 10
typedef struct SqStack{
int data[STACK_INIT_SIZE];
int top;
}SqStack;
// 初始化栈
void InitStack(SqStack *S) {
S->top = -1;
}
// 判断栈是否为空
bool StackEmpty(SqStack *S) {
return S->top == -1;
}
// 压栈
void Push(SqStack *S, int elem) {
if (S->top == STACK_INIT_SIZE - 1) {
printf("SqStack S is overflow\n");
return;
}
S->data[++S->top] = elem;
}
// 出栈
void Pop(SqStack *S, int *elem) {
if (StackEmpty(S)) {
printf("The SqStack S is empty\n");
return;
}
*elem=S->data[S->top--];
}
int main() {
SqStack S;
int e,N;
InitStack(&S);
printf("请输入要转化为八进制的数:\n");
scanf("%d",&N);
while (N){
Push(&S, N%8);
N/=8;
}
while (!StackEmpty(&S)){
Pop(&S,&e);
printf("%d",e);
}
printf("\n");
return 0;
}
C语言通过栈将十进制转换为8进制
最新推荐文章于 2024-05-28 16:23:43 发布