7-1 expression conversion (25 points)

Original link: http://www.cnblogs.com/sykline/p/9762524.html

topic:

Arithmetic expression notation prefix, infix and postfix notation representation method. Arithmetic expression is the use of daily use infix notation, i.e., binary operators in the middle of two operands. Please design a program to infix expression into postfix notation.

Input formats:

Input given free spaces in a row infix expressions may comprise +, -, *, \and left and right parentheses (), the expression does not exceed 20 characters.

Output formats:

Output line postfix notation converted, separated by a space required between the different objects (operand arithmetic symbols), but not the end of the extra spaces.

Ideas:

Thank big brother blog inspired card over a week's question: https: //www.cnblogs.com/8023spz/p/7635353.html

in conclusion:

1, to build a stack of empty storage operator

2, when it comes to the direct digital output value is noted that here may be a decimal number, negative (with negative sign and the digital output is) with a positive sign or digital;

3, when the operator encountered, the first comparison operation priority of the stack size of the current symbol a and a symbol B, if a> b directly pushed onto the stack, otherwise the output stack or operator until the stack is empty elements '(';

4, when it comes '') when the operator, direct output stack operator until the stack is empty or encounter operator '(';

5, the final output stack operator until the stack is empty.

6, a situation where the card format is most operators and right bracket consecutive such as 2 + (5) -1, careful consideration of processing an output format;

7, '+', '-' and the case with digital output is the sign of the first bit string or the front of the sign is '(', the remaining cases are treated as an operator.

The first 2 + (5) -1 to hematemesis card code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
typedef long long ll;
stack<char> sta;

int main () {
    map<char,int> mp;
    string str;
    cin>>str;
    bool isfirst = true;
    mp [ ' - ' ] = 1 , mp [ ' + ' ] = 1 ;
    Mp [ ' * ' ] = 2 , Mp [ ' / ' ] = 2 ;
    mp['('] = 3,mp[')'] = 3;
    for(int i = 0; i<str.size(); i++) {
        if(((i==0||str[i-1]=='(') && (str[i]=='+' || str[i] =='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {
            IF (! STR [I] = ' + ' ) {
                 IF (! isFirst) { /// / careful consideration of a position of the first process output format input + 2 (+. 5) -1 
                    COUT << "  " ; / // I / O 25 + 1 - format error, in order to find the error, for example to give the sub-Yuxianyusi 
                } /// / 
                COUT << STR [I];
            }
            while(str[i+1]=='.' || (str[i+1]>='0' && str[i+1]<='9')) {
                i++;
                cout<<str[i];
            }
            isfirst = false;
        } else {
            if(str[i]==')') {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop ();
                }
                sta.pop ();
            } else if(sta.empty()||mp[str[i]] > mp[sta.top()]) {
                sta.push(str[i]);
            } else {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop ();
                }
                sta.push(str[i]);
            }
        }
    }
    while(!sta.empty()) {
        cout<<' '<<sta.top();
        sta.pop ();
    }
    return 0;
}
/*
Sample input:
2+3*(7-4)+8/4
Sample output:
2 3 7 4 - * + 8 4 / +
*/
View Code

Correct the code for the whole:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4 + 10;
typedef long long ll;
stack<char> sta;

int main () {
    map<char,int> mp;
    string str;
    cin>>str;
    bool isfirst = true;
    mp [ ' - ' ] = 1 , mp [ ' + ' ] = 1 ;
    Mp [ ' * ' ] = 2 , Mp [ ' / ' ] = 2 ;
    mp['('] = 3,mp[')'] = 3;
    for(int i = 0; i<str.size(); i++) {
        if(((i==0||str[i-1]=='(') && (str[i]=='+' || str[i] =='-')) || (str[i]>='0'&&str[i]<='9') || (str[i]=='.')) {
            if(!isfirst) {
                cout<<" ";
            }
            if(str[i]!='+') {
                cout<<str[i];
            }
            while(str[i+1]=='.' || (str[i+1]>='0' && str[i+1]<='9')) {
                i++;
                cout<<str[i];
            }
            isfirst = false;
        } else {
            if(str[i]==')') {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop ();
                }
                sta.pop ();
            } else if(sta.empty()||mp[str[i]] > mp[sta.top()]) {
                sta.push(str[i]);
            } else {
                while(!sta.empty() && sta.top()!='(') {
                    cout<<' '<<sta.top();
                    sta.pop ();
                }
                sta.push(str[i]);
            }
        }
    }
    while(!sta.empty()) {
        cout<<' '<<sta.top();
        sta.pop ();
    }
    return 0;
}
View Code

 

Reproduced in: https: //www.cnblogs.com/sykline/p/9762524.html

Guess you like

Origin blog.csdn.net/weixin_30687587/article/details/94789354
Recommended