数据结构之栈及应用(c++版)

本文介绍了一个使用栈实现的简单表达式求值程序。该程序定义了通用的栈类和表达式求值类,能够解析包含加减乘除运算的括号算术表达式,并计算出最终结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Stack.h

#pragma once
template<typename T>
class Node
{
public:
    Node():last(nullptr) {};
    Node(const T& v):value(v),last(nullptr){}
    Node*& getlast() { return last; }
    T& getvalue(){ return value; }
private:
    Node* last;
    T value;
};
template<typename T>
class Stack
{
public:
    Stack():counts(0),tail(nullptr){}
    void push(const T& item);
    T pop();
    bool isEmpty()const;
    int size()const;
private:
    int counts;
    Node<T>* tail;
};
template<typename T>
void Stack<T>::push(const T& item)
{
    Node<T>* top = new Node<T>;
    top->getlast() = tail;
    tail = top;
    top->getvalue() = item;
    ++counts;
}
template<typename T>
bool Stack<T>::isEmpty() const
{
    return counts == 0;
}
template<typename T>
T Stack<T>::pop()
{
    if (isEmpty())
        exit(EXIT_FAILURE);
    T ret = tail->getvalue();
    Node<T>* topop = tail;
    tail = tail->getlast();
    --counts;
    delete topop;
    topop = nullptr;
    return ret;
}
template<typename T>
int Stack<T>::size() const
{
    return counts;
}

测试及应用(用于求括号算术表达式的值)
Evaluate.h

#pragma once
#include "Stack.h"
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

class Evaluate
{
public:
    Evaluate(std::string s):exp(s) {};
    operator double();
private:
    Stack<std::string> ops;
    Stack<double> value;
    std::string exp;
};
Evaluate::operator double()
{
    std::istringstream is(exp);
    std::istream_iterator<std::string> part(is);
    auto f = [this](const std::string& s) mutable
    {
        if (s == "(");
        else if (s == "+")
            ops.push("+");
        else if (s == "-")
            ops.push("-");
        else if (s == "*")
            ops.push("*");
        else if (s == "/")
            ops.push("/");
        else if (s == ")")
        {
            double v = value.pop();
            std::string op = ops.pop();
            if (op == "+")
                v = v + value.pop();
            else if (op == "-")
                v = v - value.pop();
            else if (op == "*")
                v = v * value.pop();
            else if (op == "/")
                v = v / value.pop();
            value.push(v);
        }
        else
        {
            std::stringstream ss(s);
            double get;
            ss >> get;
            value.push(get);
        }
    };
    for_each(part, std::istream_iterator<std::string>(), f);
    return value.pop();
}

main.cpp

#include <iostream>
#include "Stack.h"
#include <string>
#include "Evaluate.h"
using namespace std;

int main()
{
    Evaluate ca("( 1 + ( ( 2 + 3 ) * ( 4 * 5 ) ) )");
    cout << ca << endl;
    double b = ca + 3;
    cout << b;
    system("pause");
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值