编辑代码

#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 = NULL;
    } else
        x = &(s->data[s->top]);
    return x;
}

datatype* Pop_stack(SEQSTACK *s) {
    datatype* x;
    if (Stack_Empty(s)) {
        printf("Stack empty\n");
        x = NULL;
    } 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 DecimalToBinary(int n) {
    SEQSTACK stack, *s;
    s = &stack;
    Init_Stack(s);
    while (n > 0) {
        int remainder = n % 8;
        Push_Stack(s, remainder + '0'); // Convert integer remainder to character
        n = n / 8;
    }
    while (!Stack_Empty(s)) {
        printf("%c", *(Pop_stack(s)));
    }
}

int main() {
    int decimal;
    printf("请输入一个十进制的数: ");
    scanf("%d", &decimal);
    printf("这个数的八进制为: ");
    DecimalToBinary(decimal);
    return 0;
}