Analytical expression --- C ++ realization

 

1. infix, prefix, suffix expression

  For a person can identify the expression: 1 + (2 + 3) * 4-5

  The position of the operator divided into:

    ① infix expression: 1 + (2 + 3) * 4-5

    ② prefix expression: - 1 + + * 2345  

    ③ postfix expression: 1 2 3 * 4 + + 5 -

  Prefix and postfix expression which already contains the calculation order, there is no need to determine the priority parentheses

2. turn prefix infix

  2.1 infix turn prefix

  ① press operator precedence bracketed all operational unit

    ((1+((2+3)*4))-5)

  ② the operator moves to the front of the corresponding bracket

    - ( + ( 1 * ( + 2 3 ) 4 )) 5 )

  ③ removal of the brackets, to give prefix expression

    - + 1 * + 2 3 4 5

 Conversion of computer-implemented:

(1) an expression tree

(2) Stack 

  ① two stacks, the stack operators Sl, S2 stack store intermediate results

  ② right to left scan expression

  ③ encounter operand, push S2

  ④ encountered operator, compares it to the stack S1 operator precedence

    If S1 is empty, or the top of the stack is the right parenthesis ")", then the operator stack S1

    When the operator precedence than the stack height or equal, the operator push S1

    If the priority is lower than the top of the stack, the stack operators S1 to S2 pressure inside the stack, and then continue with the comparison operator stack S2

  ⑤ encountered brackets

    S1 directly into the right parenthesis

    Left parenthesis, the operator sequentially eject the stack S1 to S2, until it encounters the right bracket, then these brackets discarded pile

  ⑥ Repeat until the leftmost expression

  ⑦ The remaining operators S1 to S2 sequentially eject pressure

  ⑧ turn the pop-S2 heavy elements to give expression prefix  

 

  2.2 Prefix expression analytical calculation

  ① from right to left scan expression

  ② a numeral, the digital push, operator experience, taken two numbers do stack operations: op-top stack, the stack results

  ③ is repeated until the leftmost expression

3. infix turn suffix

  3.1 infix turn suffix  

  ① press operator precedence bracketed all operational unit

  ② the operator moves to the rear of the corresponding brackets

  ③ removal of the brackets, to give prefix expression

(1) an expression tree

(2) Stack

 

  3.2 postfix expression parsing settlement 

  ① scan from left to right expression

  ② a numeral, a digital push, operator experience, taken two numbers do stack operations: op-top stack, the stack results

  ③ repeated, until the far right of expression

4. determine the legality of expression

Legitimacy (1) of the brackets

 Here only the expression in brackets {} [] (), not temporary numbers and operators, and there is no input of additional invalid expression character string

bool expreIsOK()
{

    map<char,char> exmap = {{'}','{'},{']','['},{')','('}};
    stack<char> char_stack;
    string str; getline (cin, str);
    cout<<"Your expression:"<<str<<endl; Iterator ITER :: = String str.begin (); the while (! = ITER str.end ()) {if (! exmap.find (* ITER) = exmap.end ()) {// if the right parenthesis if ( ! char_stack.empty () || char_stack.top () = Exmap [* ITER]) {return to false ;} the else {char_stack.pop ();}} // if the else {left bracket char_stack.push (* ITER) ;} ++ ITER;} IF (char_stack.empty ()) {return to true ;} the else {return to false ;}}

 

(2) Operators legitimacy

 

Guess you like

Origin www.cnblogs.com/taoXiang/p/12563477.html