/// <summary> /// 将表达式中的操作数和运算符分割出来 /// </summary> /// <param name="expression">文本表达式</param> /// <returns>操作数与运算符表</returns> internal static List <IOperatorOrOperand> SplitExpression(string expression) { List <IOperatorOrOperand> output = new List <IOperatorOrOperand>(); StringBuilder operandbuf = new StringBuilder(); StringBuilder operatorbuf = new StringBuilder(); // 记录刚才最后输出的表达式项 IOperatorOrOperand lastItem = null; // 在尾部添加一个空格,帮助分离最后一个操作数或运算符 expression = expression + " "; double result = 0; for (int i = 0; i < expression.Length; i++) { if (char.IsDigit(expression[i]) == true || expression[i] == '.') { // 如果是数字或小数点(操作数成份) // 结束前一个运算符 if (operatorbuf.Length > 0) { // 尝试获取运算符 OperatorBase opr = TryGetOperator(operatorbuf.ToString(), lastItem); if (opr != null) { output.Add(opr); lastItem = opr; operatorbuf.Length = 0; } else { throw new InvalidCastException(operatorbuf.ToString() + " 无法解析为合法的运算符。"); } } // 合并入当前操作数项 operandbuf.Append(expression[i]); } else { // 不是数字或小数点(运算符成份) // 结束前一个操作数 if (operandbuf.Length > 0) { if (double.TryParse(operandbuf.ToString(), out result) == false) { throw new FormatException(operandbuf.ToString() + " 无法解析为合法的操作数。"); } // 输出操作数 OperandInfo operand = new OperandInfo(double.Parse(operandbuf.ToString())); output.Add(operand); lastItem = operand; operandbuf.Length = 0; } // 合并非空白字符到当前运算符项 if (char.IsWhiteSpace(expression[i]) == false) { operatorbuf.Append(expression[i]); } // 分析并输出运算符 if (operatorbuf.Length > 0) { // 尝试获取运算符 OperatorBase opr = TryGetOperator(operatorbuf.ToString(), lastItem); if (opr != null) { output.Add(opr); lastItem = opr; operatorbuf.Length = 0; } } } } return(output); }
public Operand(string operandString) { m_OperandString = operandString; Info = new OperandInfo(operandString); State = OperandState.Init; }
/// <summary> /// �����ʽ�еIJ�������������ָ���� /// </summary> /// <param name="expression">�ı����ʽ</param> /// <returns>���������������</returns> internal static List<IOperatorOrOperand> SplitExpression(string expression) { List<IOperatorOrOperand> output = new List<IOperatorOrOperand>(); StringBuilder operandbuf = new StringBuilder(); StringBuilder operatorbuf = new StringBuilder(); // ��¼�ղ��������ı��ʽ�� IOperatorOrOperand lastItem = null; // ��β�����һ���ո����������һ��������������� expression = expression + " "; double result = 0; for (int i = 0; i < expression.Length; i++) { if (char.IsDigit(expression[i]) == true || expression[i] == '.') { // ��������ֻ�С���㣨�������ɷݣ� // ����ǰһ������� if (operatorbuf.Length > 0) { // ���Ի�ȡ����� OperatorBase opr = TryGetOperator(operatorbuf.ToString(), lastItem); if (opr != null) { output.Add(opr); lastItem = opr; operatorbuf.Length = 0; } else { throw new InvalidCastException(operatorbuf.ToString() + " ������Ϊ�Ϸ����������"); } } // �ϲ��뵱ǰ�������� operandbuf.Append(expression[i]); } else { // �������ֻ�С���㣨������ɷݣ� // ����ǰһ�������� if (operandbuf.Length > 0) { if (double.TryParse(operandbuf.ToString(), out result) == false) { throw new FormatException(operandbuf.ToString() + " ������Ϊ�Ϸ��IJ�������"); } // ��������� OperandInfo operand = new OperandInfo(double.Parse(operandbuf.ToString())); output.Add(operand); lastItem = operand; operandbuf.Length = 0; } // �ϲ��ǿհ��ַ�����ǰ������� if(char.IsWhiteSpace(expression[i]) == false) { operatorbuf.Append(expression[i]); } // �������������� if (operatorbuf.Length > 0) { // ���Ի�ȡ����� OperatorBase opr = TryGetOperator(operatorbuf.ToString(), lastItem); if (opr != null) { output.Add(opr); lastItem = opr; operatorbuf.Length = 0; } } } } return output; }