使用c++,基于顺序栈实现十进制数到八进制数的转换
时间: 2024-05-06 08:20:48 浏览: 149
```c
#include <stdio.h>
#include <stdlib.h>
#define STACK_SIZE 100
typedef struct {
int* data;
int top;
} Stack;
Stack* initStack() {
Stack* s = (Stack*)malloc(sizeof(Stack));
s->data = (int*)malloc(sizeof(int) * STACK_SIZE);
s->top = -1;
return s;
}
void push(Stack* s, int x) {
s->data[++s->top] = x;
}
int pop(Stack* s) {
return s->data[s->top--];
}
int isEmpty(Stack* s) {
return s->top == -1;
}
void convert(int n) {
Stack* s = initStack();
while (n) {
push(s, n % 8);
n /= 8;
}
while (!isEmpty(s)) {
printf("%d", pop(s));
}
printf("\n");
}
int main() {
int n;
printf("Please input a decimal number: ");
scanf("%d", &n);
printf("The octal number is: ");
convert(n);
return 0;
}
```
阅读全文