栈及其应用,应该怎么写

栈及其应用
在栈中要求实现
①构造一个空的顺序栈;
②判断栈是否为空,空返回true,否则返回false;
③插入元素e为新的栈顶元素;
④删除栈顶元素,用e返回值;
⑤对于任意一个非负十进制数,打印输出与其等值的八进制数,并在主函数中调用该函数。
实现运行如下图所示。

img


#include <iostream>
#include <cstring>
#define MAX 200

using namespace std;
class Stack {
    int _data[MAX];
    int _top;
public:
    Stack() :_top(-1) {

    }
    void push(int e) {
        _data[++_top] = e;
    }
    int pop() {
        return _data[_top--];
    }
    int  top() {
        return _data[_top];
    }
    bool isEmpty() {
        if (_top == -1)
            return true;
        else
            return false;
    }
};


int main() {
    Stack s;
    int n;
    cin >> n;
    while (n) {
        s.push(n % 8);
        n = n / 8;
    }
    while (!s.isEmpty()) {
        cout << s.pop() ;
    }
    return 0;
}
//大致代码如此,未考虑出错情况,请自行补上