这段代码怎么回事。。。
  • 板块灌水区
  • 楼主demonie1014
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/8/7 13:44
  • 上次更新2023/11/3 05:25:42
查看原帖
这段代码怎么回事。。。
1056059
demonie1014楼主2023/8/7 13:44

这是我写的栈的代码,放在名为stack.h的文件里:

// this header file defined a stack

#ifndef STACK_H
#define STACK_H

#include<iostream>
#include<string>

constexpr unsigned long MAX = 4294967295UL; // this is the max value of an unsigned long int

class Stack
{
    private:
        int data[MAX]; // nums stored in the stack
        int size; // used part of the stack
    public:
        void push(int num);
        int pop();
        int get_size();
        int get_index(int index);
        bool empty();
        bool full();
        int get_top();
        void print_stack();
};

/**
 * push a number onto the stack
 * 
 * if reach max size, raise runtime error
 * 
 * @param num value to be pushed onto the stack
 * @return no return value
 */
void Stack::push(int num)
{
    if (size == MAX)
    {
        throw std::runtime_error("Reached max stack size");
    }
    else
    {
        data[size] = num;
        size ++;
    }
}

/**
 * pop an element out of the stack
 * 
 * if there's no elements in the stack, raise runtime error
 * 
 * @return element poped out of the stack, -1 if there are no elements in the stack
 */
int Stack::pop()
{
    int tmp;
    if (size == 0)
    {  
        throw std::runtime_error("No element to be poped");
    }
    else
    {
        tmp = data[size-1];
        data[size-1] = 0;
        size --;
        return tmp;
    }
}

/**
 * return the size of the stack
 * 
 * @return the size of the stack
 */
int Stack::get_size()
{
    return size;
}

/**
 * return the element at some index
 * 
 * raise runtime error if there's no such index
 * 
 * @param index the index of the target element, start at zero
 * @return the element at position index, -1 if no such index
 */
int Stack::get_index(int index)
{
    if (index >= size)
    {
        throw std::runtime_error("Index too big, max index: " + std::to_string(size));
        return -1;
    }
    else if (index < 0)
    {
        throw std::runtime_error("Index too small, minindex: 0");
        return -1;
    }
    else
    {
        return data[index];
    }
    
}

/**
 * check if the stack is empty
 * 
 * @return if the stack is empty
 */
bool Stack::empty()
{
    return size == 0;
}

/**
 * check if the stack is full
 * 
 * @return if the stack is full
 */
bool Stack::full()
{
    return size == MAX;
}

/**
 * get the top element of the stack
 * 
 * raise runtime error if there's no element in the stack
 * 
 * @return the top element of the stack, -1 if the stack is empty
 */
int Stack::get_top()
{
    if (size == 0)
    {
        throw std::runtime_error("The stack is empty");
        return -1;
    }
    else
    {
        return data[size-1];
    }
}

/**
 * print all the elements in the array, seperate by ','
 * output method: std::cout
 */
void Stack::print_stack()
{
    for (int i = 0; i<size; i++)
    {
        std::cout << data[i] << ", ";
    }
    std::cout << std::endl;
}

#endif

还有这些,放在main.cpp里:

#include "stack.h"
#include<iostream>

int main()
{
    Stack stack;
    
    // 将数字压入栈中
    stack.push(5);
    stack.push(10);
    stack.push(15);
    
    // 打印栈中的元素
    stack.print_stack(); // 输出:5, 10, 15
    
    // 弹出栈顶元素
    int popped = stack.pop();
    std::cout << "Popped element: " << popped << std::endl; // 输出:Popped element: 15
    
    // 获取栈的大小
    int size = stack.get_size();
    std::cout << "Stack size: " << size << std::endl; // 输出:Stack size: 2
    
    // 检查栈是否为空
    bool isEmpty = stack.empty();
    std::cout << "Is stack empty? " << (isEmpty ? "Yes" : "No") << std::endl; // 输出:Is stack empty? No
    
    // 获取栈顶元素
    int top = stack.get_top();
    std::cout << "Top element: " << top << std::endl; // 输出:Top element: 10
    
    return 0;
}

现在的问题是运行main.cpp后没有任何输出,也没有错误报告,试过命令行手动编译,我的IDE是VScode,路径没有问题。

请大家帮帮我

2023/8/7 13:44
加载中...