public bool Parse(string exp) { m_tokens.Clear(); //清空语法单元堆栈 if (exp.Trim() == "") //表达式不能为空 { return(false); } else if (!this.IsMatching(exp))//括号、引号、单引号等必须配对 { return(false); } Stack <object> operands = new Stack <object>(); //操作数堆栈 Stack <Operator> operators = new Stack <Operator>(); //运算符堆栈 OperatorType optType = OperatorType.ERR; //运算符类型 string curOpd = ""; //当前操作数 string curOpt = ""; //当前运算符 int curPos = 0; //当前位置 //int funcCount = 0; //函数数量 curPos = FindOperator(exp, ""); exp += "@"; //结束操作符 while (true) { curPos = FindOperator(exp, ""); curOpd = exp.Substring(0, curPos).Trim(); curOpt = exp.Substring(curPos, 1); //////////////测试代码/////////////////////////////////// //System.Diagnostics.Debug.WriteLine("***************"); //System.Diagnostics.Debug.WriteLine("当前读取的操作数:" + curOpd); //foreach (var item in operands.ToArray()) //{ // if (item is Operand) // { // System.Diagnostics.Debug.WriteLine("操作数栈:" + ((Operand)item).Value); // } // if (item is Operator) // { // System.Diagnostics.Debug.WriteLine("操作数栈:" + ((Operator)item).Value); // } //} //System.Diagnostics.Debug.WriteLine("当前读取的运算符:" + curOpt); //foreach (var item in operators.ToArray()) //{ // System.Diagnostics.Debug.WriteLine("运算符栈:" + item.Value); //} //////////////////////////////////////////////////////// //存储当前操作数到操作数堆栈 if (curOpd != "") { operands.Push(new Operand(curOpd, curOpd)); } //若当前运算符为结束运算符,则停止循环 if (curOpt == "@") { break; } //若当前运算符为左括号,则直接存入堆栈。 if (curOpt == "(") { operators.Push(new Operator(OperatorType.LB, "(")); exp = exp.Substring(curPos + 1).Trim(); continue; } //若当前运算符为右括号,则依次弹出运算符堆栈中的运算符并存入到操作数堆栈,直到遇到左括号为止,此时抛弃该左括号. if (curOpt == ")") { while (operators.Count > 0) { if (operators.Peek().Type != OperatorType.LB) { operands.Push(operators.Pop()); } else { operators.Pop(); break; } } exp = exp.Substring(curPos + 1).Trim(); continue; } //调整运算符 curOpt = Operator.AdjustOperator(curOpt, exp, ref curPos); optType = Operator.ConvertOperator(curOpt); //若运算符堆栈为空,或者若运算符堆栈栈顶为左括号,则将当前运算符直接存入运算符堆栈. if (operators.Count == 0 || operators.Peek().Type == OperatorType.LB) { operators.Push(new Operator(optType, curOpt)); exp = exp.Substring(curPos + 1).Trim(); continue; } //若当前运算符优先级大于运算符栈顶的运算符,则将当前运算符直接存入运算符堆栈. if (Operator.ComparePriority(optType, operators.Peek().Type) > 0) { operators.Push(new Operator(optType, curOpt)); } else { //若当前运算符若比运算符堆栈栈顶的运算符优先级低或相等,则输出栈顶运算符到操作数堆栈,直至运算符栈栈顶运算符低于(不包括等于)该运算符优先级, //或运算符栈栈顶运算符为左括号 //并将当前运算符压入运算符堆栈。 while (operators.Count > 0) { if (Operator.ComparePriority(optType, operators.Peek().Type) <= 0 && operators.Peek().Type != OperatorType.LB) { operands.Push(operators.Pop()); if (operators.Count == 0) { operators.Push(new Operator(optType, curOpt)); break; } } else { operators.Push(new Operator(optType, curOpt)); break; } } } exp = exp.Substring(curPos + 1).Trim(); } //转换完成,若运算符堆栈中尚有运算符时, //则依序取出运算符到操作数堆栈,直到运算符堆栈为空 while (operators.Count > 0) { operands.Push(operators.Pop()); } //调整操作数栈中对象的顺序并输出到最终栈 while (operands.Count > 0) { m_tokens.Push(operands.Pop()); } return(true); }