C++ stack

该程序实现了一个中缀表达式到后缀表达式的转换,并计算后缀表达式的值。通过栈操作,检查运算符的优先级和括号匹配,最后得出合法表达式的后缀形式及其计算结果。

一、定义

栈(stack)又名堆栈,它是一种运算受限的线性表。限定仅在表尾进行插入和删除操作的线性表。这一端被称为栈顶,相对地,把另一端称为栈底。向一个栈插入新元素又称作进栈、入栈或压栈,它是把新元素放到栈顶元素的上面,使之成为新的栈顶元素;从一个栈删除元素又称作出栈或退栈,它是把栈顶元素删除掉,使其相邻的元素成为新的栈顶元素。

https://baike.baidu.com/item/%E6%A0%88/12808149#3_3

二、常用函数

函数功能
q.top()获取栈顶元素(并不删除)
q.pop()删除栈顶元素
q.push(x)向栈中加入元素
q.empty()判断栈是否为空

三、程序示例

例如有一个这样的题目:

输入一个中缀表达式(由0-9组成的运算数、加+减-乘*除/四种运算符、左右小括号组成。注意“-”也可作为负数的标志,表达式以“@”作为结束符),判断表达式是否合法,如果不合法,请输出“NO”;否则请把表达式转换成后缀形式,再求出后缀表达式的值并输出。

#include<iostream>
#include<stack>
const int N = 10000 + 10;
using namespace std;

int n, m;
stack<int> s1;//操作数栈
stack<char> s2;//运算符栈

int level(char x)//运算符优先级
{
    if (x == '+' || x == '-')
        return 1;
    if (x == '*' || x == '/')
        return 2;
    if (x == '^')
        return 3;
    return 0;
}

void calc(stack<int>& s1, stack<char>& s2)//弹出栈顶元素并计算
{
    /*取出后弹出栈*/
    int y = s1.top();
    s1.pop();
    int x = s1.top();
    s1.pop();
    char z = s2.top();
    s2.pop();

    /*根据运算符计算,并压入栈*/
    if (z == '+')
        s1.push(x + y);
    if (z == '-')
        s1.push(x - y);
    if (z == '*')
        s1.push(x * y);
    if (z == '/')
        s1.push(x / y);
    if (z == '^')
        s1.push(pow(x, y));
}

int c(int x)
{
    return x != 0;
}
char str[1000000];
int sum[1000000];

int main() {

    scanf("%s", str + 1);
    n = strlen(str + 1) - 1;//忽略掉'@'的字符串长度

    for (int i = 1; i <= n; ++i)//检查匹配
    {
        sum[i] += sum[i - 1];
        if (str[i] == '(')
            sum[i]++;
        if (str[i] == ')')
            sum[i]--;
    }

    bool out = false;
    for (int i = 2; i <= n; ++i)
        if (c(level(str[i])) && c(level(str[i - 1])))
        {
            out = 1;
            break;
        }

    if ((n == 1 && c(level(str[1]))) || sum[n] || out)//表达式不合法
    {
        cout << "NO" << endl;
        return 0;
    }

    stack<int> s1;
    stack<char> s2;
    int temp = 0;
    bool flag = false;
    for (int i = 1; i <= n; i++)
    {
        if ('0' <= str[i] && str[i] <= '9')//判断当前字符是否为数字
        {
            temp = (temp << 3) + (temp << 1) + str[i] - '0';
            flag = true;
        }
        else
        {
            if (flag)
            {
                s1.push(temp);
                temp = 0;
                flag = false;
            }
            if (str[i] == '(')
            {
                s2.push(str[i]);
                continue;
            }
            if (str[i] == ')')
            {
                while (s2.top() != '(')
                    calc(s1, s2);
                s2.pop();
                continue;
            }
            while (!s2.empty() && level(s2.top()) >= level(str[i]))//优先级判断
                calc(s1, s2);
            s2.push(str[i]);//运算符入栈
        }
    }
    if (flag)
    {
        s1.push(temp);
        temp = 0;
        flag = false;
    }
    while (!s2.empty())
        calc(s1, s2);
    cout << s1.top() << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值