Algorithm Flow:
It is divided into four main steps:
1. The current character is a number or a letter, the direct output
2. The current word identifier), then match the output in the stack, always match to), the stop output (that is) and its top elements are all out of the output)
3. The current character is an operator, compares the current character's stack priority (ICP) and the character stack precedence (ISP), and if icp<=isp, pops out the stack operator and repeats 3
4. The current character is empty, then the elements in the stack are ejected sequentially
Baidu Encyclopedia above description:
1, build the operator stack stackoperator for the storage of operators, pressing into ' \ '.
2, preprocessing expression, positive, minus sign plus 0 (if a plus sign (minus) appears in front of the first or after the opening parenthesis, then the plus sign (minus) is signed).
3, sequential scan expression, if the current character is a number (priority 0 symbol), the direct output of the number, if the current character is an operator or parenthesis (priority is not a 0 symbol), then the 4th is judged.
4, if the current operator is ' (', directly into the stack, if ') ', out of the stack and sequential output operator until the first ' (', the first encountered ' ('), but not the output, if the arithmetic character, the top element of the stack and the current element precedence: If the top of the stack operator precedence >= the current element priority , out of the stack and sequential output operator until the top element of the stack priority < current element priority, and then the current element into the stack, if the top element of the stack < The current element, directly into the stack.
5, repeat the 3rd until the expression scan is complete.
6, sequentially out of the stack and output operators until the top of the stack element is ' \. '
Of course, my own understanding is to follow the simple changes that I have achieved, not including curly braces
My Code:
The middle sequence traversal changes to subsequent traversal of public string tranform (String obj) {stack<character> Stack = new stack<character> (); String obj2 = ""; for (int i = 0; I < obj.length (); i++) {Char ch = obj.charat (i), if (Character.isletter (CH))//letter or digital Direct output {obj2 + = ch; SYSTEM.OUT.PRINTLN (CH);} else if (ch = = ') ') operations characters stop out of stack {char temp;while (temp = Stack.pop ())! = ' (') {obj2 + = temp; SYSTEM.OUT.PRINTLN (temp);}} else//the stack priority of the comparison operator {if (Stack.isempty ()) {Stack.push (ch); continue;} Char temp = Stack.peek (), while (ICP (CH) <= ISP (temp))//stack priority is less than the priority in the stack, the stack {System.out.println (temp); obj2+=temp; Stack.pop (); if (Stack.isempty ()) break; Temp=stack.peek (); }stack.push (ch);}} Bounce the remaining elements out of the stack while (!stack.isempty ()) {char temp=stack.pop (); obj2+=temp; System.oUT.PRINTLN (temp);} return obj2;} Operator precedence within the stack private int ISP (char ch) {switch (ch) {case ' + ': Case '-': return 2;case ' * ': Case '/': Return 4;case ') ': return 7; Case ' (': Return 1;default:break;} return 0;} Precedence priority of the operator into the stack private static int ICP (char ch) {switch (ch) {case ' + ': Case '-': return 3;case ' * ': Case '/': Return 5;case ') ': Return 1;case ' (': Return 7;default:break;} return 0;} public static void Main (string[] args) {String objstring= "A * (b+c) +c/d"; treenode<character> treenode=new treenode<character> (null); Treenode.tranform (objString);}Operating effect:
A * (b+c) +c/d -"abc+*cd/*
If it's easy to implement a simple calculator with this post-sequential traversal, I remember a grammar-based calculator for the next implementation.
Sequential traversal in data structures (Java implementation) (II)