Infix to postfix conversion expression

Infix to postfix conversion expression

Topic information

Infix expressions are mathematical expressions that we usually write. Postfix expressions are also called reverse Polish expressions. When the compiler checks the syntax of the expressions in the programs we write, they can often pass reverse Polish expressions. get on. The program we want to design and implement is to convert the arithmetic expression represented by the infix into a suffix representation. For example, the infix expression is
(A 一 (B*C 十 D)*E) / (F 十 G )
converted into a suffix and represented as:
ABC*D十E*--FG十/
Note: In order to simplify the programming implementation, it is assumed that the variable names are all single letters. The only symbols are +, -, *, / and ^ (exponential operation), which can handle parentheses () and assume that the input arithmetic expression is correct.
Requirement: Use the stack data structure to achieve, the input infix expression ends with # sign

enter

Integer N. Indicates that there are N infix expressions below and N expressions
composed of single letters, integers and operators

Output

N postfix expressions.

Test sample

Test example 1

1
(A-(B*C+D)*E)/(F+G)#
ABC*D+E*-FG+/

Test example 2

2
a+b*c-d#
a-b*(c+d)#
abc*+d-
abcd+*-

answer

#include <iostream>
#include <stack>
#include <string>
using namespace std;

stack <char> s;

int main()
{
    
    
    //freopen("/Users/zhj/Downloads/test.txt","r",stdin);
    int N;
    cin >> N;
    while (N--)
    {
    
    
        string in;
        cin >> in;
        int t = 0;
        while (in[t] != '#')
        {
    
    
            if (t == 0 && in[t] == '-')
            {
    
    //第一个数就是负数
                cout << in[t];
            }
            else if (isalnum(in[t]))
            {
    
    //如果是数字字母的话就输出
                cout << in[t];
            }
            else if (in[t] == '(')
            {
    
    //是左括号的话就压入栈中
                s.push(in[t]);
            }
            else if (in[t] == ')')
            {
    
    //如果是右括号的话,栈元素弹出,将弹出的操作符输出直到遇到左括号为止
                while(s.top()!='(')
                {
    
    
                    cout<<s.top();
                    s.pop();
                }
                s.pop();
            }
            else if( in[t] =='-' && ( in[t - 1] == '(' || in[t - 1] == '*' || in[t - 1] == '/' || in[t - 1] == '%' || in[t - 1] == '+' || in[t - 1] == '-') )
            {
    
    //此时是负数,且之前的是这些个符号的话,证明当前是一个负数
                cout << in[t];
            }
            else
            {
    
    //其他的操作符,从栈中弹出元素直到遇到发现更低优先级的元素(或者栈为空)为止
                if(s.empty())
                {
    
    
                    s.push(in[t]);
                }
                else
                {
    
    
                    int x, y;//x=ch,y=top of the stack
                    while (1)
                    {
    
    
                        if (s.empty())
                        {
    
    
                            s.push(in[t]);
                            break;
                        }

                        char chtmp = s.top();//chtmp此时是栈顶的元素
                        switch (in[t])
                        {
    
    //给此时的符号标记一个优先级
                            case '^':{
    
     x = 7; break;}
                                //10^2^2当然便是刚输入这个^优先级别更高,因为后面还可以继续续上^2,变成10^2^2^2
                            case '*':{
    
     x = 4; break;}
                            case '/':{
    
     x = 4; break;}
                            case '+':{
    
     x = 2; break;}
                            case '-':{
    
     x = 2; break;}
                        }
                        switch (chtmp)
                        {
    
    //给之前的符号标记一个优先级
                            case '^':{
    
     y = 6; break;}
                            case '*':{
    
     y = 5; break;}
                            case '/':{
    
     y = 5; break;}
                            case '(':{
    
     y = 1; break;}
                            case '+':{
    
     y = 3; break;}
                                //如果之前是一个加号,此时的ch仍然是一个加号,那么理应该输出这个+号的,所以相应的之前的优先级大一点
                            case '-':{
    
     y = 3; break;}
                        }

                        if (y > x || y == x)
                        {
    
    //如果原来栈顶的优先级大于之前的优先级,那么就输出
                            cout<<chtmp;
                            s.pop();
                            continue;
                        }
                        else
                        {
    
    //不优先说明仍然在累计,则压入栈中
                            s.push(in[t]);
                            break;
                        }
                    }
                }
            }
            t++;
        }
        while (!s.empty())
        {
    
    
            cout<<s.top();
            s.pop();
        }
        cout<<endl;
    }
}

to sum up

The main point of this topic also appears in the handling of negative numbers. Here are some examples for reference

10
14*10-(10)*2#
14*10-(-10)*2#
14*10--10*2#
-14*-12#
-(1-2*-3)#
-3^-(1-2*-3)#
30/(-3+3)+1#
(-2)--3*(-8)#
-2--3*-8#
-A+B#

Guess you like

Origin blog.csdn.net/zhj12399/article/details/109317675