public Expression2(string stExp) { this.stExp = Normalize(stExp); lstSymbol = Str2SymbolList(); PrioritizeOperators(); expTree = SymbolLst2ExpTree(0, lstSymbol.Count() - 1); }
private ExpNode SymbolLst2ExpTree(int start, int end) { if (start > end) { return(null); } while (RemoveOutestBracketPari(ref start, ref end)) { ; //use while to handle the case like (((...))), we need to remove all the 3 pairs } int idx = FindLowestPriOperator(start, end); if (idx < 0) { //no operators, return the first symbol, should be a operand for a valid expression return(new ExpNode(lstSymbol[start])); } else { ExpNode rootOp = new ExpNode(lstSymbol[idx]); switch (rootOp.Value.Op) { case Operator.ADD: case Operator.MINUS: case Operator.MUL: case Operator.DIV: case Operator.POW: // operator in middle, and 2 operands operators rootOp.SubExps = new List <ExpNode>(); if (start <= idx - 1) { rootOp.SubExps.Add(SymbolLst2ExpTree(start, idx - 1)); } if (idx + 1 <= end) { rootOp.SubExps.Add(SymbolLst2ExpTree(idx + 1, end)); } break; default: // others are function like operators, i.e. func(p1, p2, ..., pn) rootOp.SubExps = ReadFuncParameters(start, end); break; } return(rootOp); } }