#include <stddef.h>
#include <stdio.h>
#define DATATYPE char
#define MAXSIZE 100
typedef struct
{ DATATYPE data[MAXSIZE];
int top;
} SEQSTACK;
SEQSTACK *s;
void init_stack(SEQSTACK *s)
{
s->top= -1;
}
int stack_empty (SEQSTACK *s)
{
if(s->top==-1)
return 1;
else
return 0;
}
void push_stack(SEQSTACK *s,DATATYPE x)
{
if(s->top==MAXSIZE-1)
printf("stack full\n");
else
{
s->top++;
s->data[s->top]=x;
}
}
DATATYPE gettop_stack(SEQSTACK *s)
{
DATATYPE x;
if(stack_empty(s))
{
printf("stack empty.\n");
x=0;
}
else
x=s->data[s->top];
return x;
}
DATATYPE pop_stack(SEQSTACK *s)
{
DATATYPE x;
if(stack_empty (s))
{
printf("stack empty\n");
x=0;
}
else
{
x=s->data[s->top];
s->top--;
}
return x;
}
void clear_stack(SEQSTACK *s)
{
s->top =-1;
}
int stacklength(SEQSTACK *s)
{
return s->top+1;
}
void dec_to_oct(int num, SEQSTACK *s) {
if (num == 0) {
push_stack(s, '0');
} else {
while (num > 0) {
push_stack(s, (num % 8) + '0');
num /= 8;
}
}
}
int main() {
SEQSTACK stack;
init_stack(&stack);
int num;
printf("请输入一个10进制整数: ");
if (scanf("%d", &num) != 1) {
printf("输入无效!\n");
return 1;
}
dec_to_oct(num, &stack);
printf("转换后的8进制数为: ");
while (!stack_empty(&stack)) {
printf("%c", pop_stack(&stack));
}
printf("\n");
return 0;
}