Exemple #1
0
        /// <summary>
        /// 파싱함수
        /// </summary>
        /// <param name="equation"></param>
        /// <param name="index"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        int Parse(string equation, int index, EquationElement root)
        {
            while (index < equation.Length)
            {
                char ch = equation[index];
                if (char.IsWhiteSpace(ch))                 // 공백무시
                {
                    index++;
                    continue;
                }
                if (Operator.IsValidOperator(ch))                           // 각 클래스에 정의된 Valid옵션을 토대로 Parse
                {
                    EquationElement n = Add(new Operator(equation, index)); // Add에서 해당 Element의 길이를 구해서 스택에 저장.
                    index += n.Length;                                      //인덱스를 n의 길이만큼 늘려서 계속 검색
                    continue;
                }
                if (Number.IsValidDigit(equation, index))
                {
                    EquationElement n = Add(new Number(equation, index));
                    index += n.Length;
                    continue;
                }
                if (Constant.IsValidDigit(equation, index))
                {
                    EquationElement n = Add(new Constant(equation, index));
                    index += n.Length;
                    continue;
                }
                if (Function.IsValidDigit(equation, index))
                {
                    EquationElement n = Add(new Function(equation, index, root));
                    index += n.Length;
                    continue;
                }
                if (CompareOperator.IsValidOperator(equation))
                {
                    EquationElement n = Add(new CompareOperator(equation));
                    index += n.Length;
                    continue;
                }
                if (Variable.IsValidDigit(equation, index, root))
                {
                    EquationElement n = Add(new Variable(equation, index, root));
                    index += n.Length;
                    continue;
                }
                index++;

                if (Term.IsValidDigit(ch))                 // 괄호가 또나오면 그 괄호를 풀어야됨.
                {
                    int endindex = FindMatchingEnd(ch, equation, index - 1);
                    if (endindex > index)
                    {
                        int    len = endindex - index;
                        string s   = equation.Substring(index, len);
                        Term   g   = Add(new Term(s, 0)) as Term;
                        len    = g.Parse(s, 0, root) + 1;
                        index += len;
                        continue;
                    }
                    throw new ParseException(equation, index - 1, "매칭되는 괄호를 찾을 수 없습니다.");
                }
                else
                {
                    if (ch == ']' || ch == ')')
                    {
                        throw new ParseException(equation, index - 1, " 매칭되는 괄호를 찾을 수 없습니다");
                    }
                }
            }
            return(index);
        }