C++栈的实现

摘要:用C++模板实现一个原始栈的操作,包括插入,删除,判空,清空,取栈顶元素,取栈长度

关键字:C++ ,stack


使用C++模板类实现了一个原始栈,功能有:清空栈,判空,取栈顶元素,插入,删除,取栈长度,支持基本的数据类型和自定义的数据类型,但是没有做异常处理。

代码如下

/*!
* \file stack.cpp
* \date 2017/04/13 8:40
*
* \author 甘超浩
* Contact: chht123@126.com
*
* \brief 使用模板来实现一个简单栈,支持基本数据类型和自定义数据类型
*
* TODO: 异常捕捉
*
* \note
*/

#include <iostream>
#include <string>
#define MAX_STACKSIZE 50
using namespace std;

template<class T>
struct stack
{
    T data[MAX_STACKSIZE];
    int top = -1;


    void ClearStack();
    bool StackEmpty();
    int GetTop(T & e);
    void Push(T e);
    T Pop();
    int StackLength();

};



template<class T>
void stack<T>::ClearStack()
{
    this->top = -1;
}

template<class T>
bool stack<T>::StackEmpty()
{
    return this->top == -1 ? true : false;
}

template<class T>
int stack<T>::GetTop(T & e)
{
    if(this->StackEmpty())
    {
        return 0;
    }
    e = this->data[s.top];
    return 1;
}

template<class T>
void stack<T>::Push(T e)
{
    if(this->StackLength() >= MAX_STACKSIZE)
    {
        return;
    }
    this->top++;
    this->data[this->top] = e;
}

template<class T>
T stack<T>::Pop()
{
    if(this->StackEmpty())
    {
        return (T)0;
    }
    T e = this->data[this->top];
    this->top--;
    return e;
}

template<class T>
int stack<T>::StackLength()
{
    return this->top + 1;
}

void test_stack(void)
{
    stack<char> s;
    string str = s.StackEmpty() ? "是" : "否";
    cout << "s是否为空:" << str << endl;
    s.Push('a');
    s.Push('b');
    cout << "栈顶元素:" << s.Pop() << endl;
    cout << "栈顶元素:" << s.Pop() << endl;
    cout << "s的大小:" << s.StackLength() << endl;
}

结果截图如下:

这里写图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值