堆栈入门-简单计算器模板-中缀转后缀

题目链接

后缀表达式又称逆波兰表示法,不含括号,运算符放在两个参与运算的语法成分的后面。

后缀表达式运算求值

自左向右顺序扫描后缀表达式。最后栈中的数字就是答案。

(1)如果是数字,则压入栈中。

(2)如果是运算符,就从栈中弹出两个数字进行运算,将运算结果压入栈中。

中缀表达式转后缀表达式

从左向右扫描中缀表达式。

(1)当输入为数字时,直接输出到后续表达式序列中。

(2)当遇到开括号时,将其入栈。

(3)当遇到闭括号时,先判断栈是否为空,若为空,则表示括号不匹配,报告异常并退出。若非空,则将栈中元素依次弹出,直到遇到第一个开括号为止(将开括号也弹出)。将弹出的元素输出到后缀表达式序列中。若没有遇到开括号,则报告异常并退出。

(4)当输入为运算符时(四则运算+ - * /之一)

  • 循环,当(栈非空&&栈顶不是开括号&&栈顶运算符的优先级不低于输入运算符的优先级)时,反复操作将栈顶元素弹出,放入后缀表达式序列。
  • 将输入的运算符压入栈内。

(5)中缀表达式全部扫描完毕,清栈,全部弹出放入后缀表达式序列中。若弹出元素中有开括号,报告异常并退出。

模板-后缀表达式转中缀表达式

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;

int priority(char c)
{
    if (c == '+' || c == '-')return 0;
    else if (c == '*' || c == '/')return 1;
}

int main()
{
    char in[205];
    char post[205] = { 0 };
    stack<char> s;
    scanf("%s", in);
    int l = strlen(in);
    int size = 0;
    for (int i = 0; i < l; i++)
    {
        if (in[i] >= '0'&&in[i] <= '9')post[size++] = in[i];
        else if (in[i] == '(')s.push(in[i]);
        else if (in[i] == ')')
        {
            if (s.empty())
            {
                printf("Wrong!\n");
                return 0;
            }
            while (s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            if (s.top() != '(')printf("Wrong!/n");
            else s.pop();//弹出开括号
        }
        else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-')
        {
            while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            s.push(in[i]);
        }
    }
    if (!s.empty())
    {
        post[size++] = s.top();
        s.pop();
    }
    printf("%s\n", post);
    system("pause");
    return 0;
}

简单计算器该思路题解

#include<cstdio>
#include<cstring>
#include<iostream>
#include<stack>
using namespace std;

int priority(char c)
{
    if (c == '+' || c == '-')return 0;
    else if (c == '*' || c == '/')return 1;
}

int main()
{
    char in[205];
    char post[205] = { 0 };
    stack<char> s;
    scanf("%s", in);
    int l = strlen(in);
    int size = 0;
    for (int i = 0; i < l; i++)
    {
        if (in[i] >= '0'&&in[i] <= '9')post[size++] = in[i];
        else if (in[i] == '(')s.push(in[i]);
        else if (in[i] == ')')
        {
            if (s.empty())
            {
                printf("Wrong!\n");
                return 0;
            }
            while (s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            if (s.top() != '(')printf("Wrong!/n");
            else s.pop();//弹出开括号
        }
        else if (in[i] == '*' || in[i] == '/' || in[i] == '+' || in[i] == '-')
        {
            while (!s.empty() && priority(s.top()) >= priority(in[i]) && s.top() != '(')
            {
                post[size++] = s.top();
                s.pop();
            }
            s.push(in[i]);
        }
    }
    if (!s.empty())
    {
        post[size++] = s.top();
        s.pop();
    }
    stack<int> ans;
    int len = strlen(post);
    for (int i = 0; i < len; i++)
    {
        if (post[i] >= '0'&&post[i] <= '9')ans.push(post[i] - '0');
        else
        {
            int b = ans.top(); ans.pop();
            int a = ans.top(); ans.pop();
            int c;
            if (post[i] == '+')c = a + b;
            else if (post[i] == '-')c = a - b;
            else if (post[i] == '*')c = a * b;
            else if (post[i] == '/')c = a / b;
            ans.push(c);
        }
    }
    printf("%d\n", ans.top());
    system("pause");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/yun-an/p/11357212.html