2.C++实现简单的栈

习题2.编写一个类,实现简单的栈

2.编写一个类,实现简单的栈。栈中有以下操作:
> 元素入栈 void push(int);
> 元素出栈 void pop();
> 读出栈顶元素 int top();
> 判断栈空 bool emty();
> 判断栈满 bool full();
如果栈溢出,程序终止。栈的数据成员由存放
10个整型数据的数组构成。先后做如下操作:
> 创建栈
> 将10入栈
> 将12入栈
> 将14入栈
> 读出并输出栈顶元素
> 出栈
> 读出并输出栈顶元素

解题思路:

栈:先进后出。

  • 需要数组*_data存储数据。_size表示栈的总体大小。
  • 数组的下标_top每次记录当前位置(栈顶指针的下标)。
  • 当数组内没有数据时,_top为-1。
  • 出栈时,直接把_top减一,不用关心那个位置的数据,下次压栈会把数据覆盖
  • 压栈时,把栈顶_top加一,然后把要压栈的数据赋值给当前数组的位置。

数据成员需要:
栈顶指针的下标 int _top;
栈的总大小 int _size;
栈中元素 int *_data; (数组)

条件:
栈为空 -1 == _top
栈满 _top == _size

操作:
入栈 _data[++_top] = value;
出栈 --top;
获取堆顶元素 _data[_top]

#include <iostream>

using std::cout;
using std::endl;

class Stack
{
public:
    //构造函数,默认栈大小为10,可输入数据进行改变;
    Stack(int size = 10)
    :_top(-1)
    ,_size(size)
    ,_data(new int[size]())
    {
        cout << "Stack()" << endl;
    }

    //判断栈是否为空
    bool empty()
    {
       return _top == -1; 
    }
    
    //判断栈是否为满
    bool full()
    {
        return (_top == _size - 1);
    }

    //压栈
    void push(const int &value)
    {
        if(!full())
        {
            _data[++_top] = value;
        }
        else
        {
            cout << "Stack is full, cannot add any data!" << endl;
        }
    }

    //出栈
    void pop()
    {
        if(!empty())
        {
            _top--;
        }
        else
        {
            cout << "Stack is empty, cannot pop any data!" << endl;
        }
    }

    //获得栈顶元素
    int gettop()
    {
        if(empty())
        {
            cout << "Stack is empty!" << endl;
        }
        return _data[_top];
    }

    //析构函数
    ~Stack()
    {
        cout << "~Stack()" << endl;
        if(_data)
        {
            delete []_data;
            _data = nullptr;
        }
    }

private:
    int _top;
    int _size;
    int *_data;
};

int main()
{
    Stack s1;
    cout << "栈是否为空:" ;
    cout << s1.empty() << endl;
    cout << "栈是否为满:" ;
    cout << s1.full() << endl;
    s1.push(10);
    s1.push(12);
    s1.push(14);
    int temp = s1.gettop();
    cout << "当前栈顶元素为:" << temp << endl;
    s1.pop();
    temp = s1.gettop();
    cout << "当前栈顶元素为:" << temp << endl;
    return 0;
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是C++实现简易计算器的代码: ```c++ #include <iostream> #include <stack> #include <string> using namespace std; int main() { stack<int> numStack; // 存储数字的 stack<char> opStack; // 存储运算符的 string expression; // 表达式字符串 cin >> expression; int len = expression.length(); for (int i = 0; i < len; i++) { char ch = expression[i]; if (ch >= '0' && ch <= '9') { // 如果是数字,入数字 numStack.push(ch - '0'); } else { // 如果是运算符 if (opStack.empty() || ch == '(') { // 如果运算符为空或者当前运算符是左括号,直接入 opStack.push(ch); } else if (ch == ')') { // 如果当前运算符是右括号,弹出运算符的元素直到遇到左括号 while (opStack.top() != '(') { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.pop(); // 弹出左括号 } else { // 如果当前运算符是加减乘除 while (!opStack.empty() && opStack.top() != '(' && ((ch == '*' || ch == '/') || (opStack.top() == '+' || opStack.top() == '-'))) { int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } opStack.push(ch); } } } while (!opStack.empty()) { // 处理剩余的运算符 int num2 = numStack.top(); numStack.pop(); int num1 = numStack.top(); numStack.pop(); char op = opStack.top(); opStack.pop(); int result; if (op == '+') { result = num1 + num2; } else if (op == '-') { result = num1 - num2; } else if (op == '*') { result = num1 * num2; } else { result = num1 / num2; } numStack.push(result); } cout << numStack.top() << endl; // 输出最终结果 return 0; } ``` 这个程序可以处理包含加减乘除和括号的表达式,但是没有考虑负数的情况。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值