示例#1
0
        /// <summary>
        /// 변수를 설정함
        /// </summary>
        /// <param name="varname"></param>
        /// <param name="value"></param>
        public void SetVar(string varname, string value)
        {
            Term term = new Term();

            term.Parse(value);
            SetVar(varname, term.Value);
        }
        /// <summary>
        /// 라벨에서 이름을 읽어서 Textbox의 값으로 변수값을 설정.
        /// </summary>
        private void SetVar()
        {
            foreach (Control ctrl in panel1.Controls)
            {
                if (ctrl.GetType() == typeof(Label))
                {
                    string[] str     = ctrl.Name.Split('\\'); // 이름은 Name\\ControlType으로 정해져있음
                    TextBox  textbox = (TextBox)panel1.Controls[str[0] + "\\TextBox"];
                    if (textbox == null)
                    {
                        continue;
                    }
                    double temp;
                    //사용자가 텍스트박스에 아무것도 입력 안했으면 0이라 가정
                    if (textbox.Text == "")
                    {
                        textbox.Text = "0";
                    }


                    // 결과값이 infinity나 NaN이면 변수로 인식하기 때문에 이것또한 계산할때 0으로 가정
                    // 나중에 Infinity나 NaN을 처리해줄 일이 있으면 파서 클래스를 변경할 필요
                    if (textbox.Text.Contains("infinity") || textbox.Text.Contains("nan"))
                    {
                        textbox.Text = "0";
                    }
                    //입력된 값을 해당 Infomation의 변수에 입력함.
                    var EquationInfo = m_Dictionary_EquationInfo[m_SelectedEquation];
                    if (EquationInfo == null)
                    {
                        return;
                    }

                    var vdata = EquationInfo.m_Dictionary_Equation[str[0]];
                    vdata.IsZeroIsNullValue = false;

                    // 계산하기전에 분기 조건문인지 확인하고 일치하면 계산속행.
                    if (vdata.m_checkOption == VariableData.CheckOption.Case)
                    {
                        if (!CheckCase(vdata, textbox.Text.ToLower()))
                        {
                            throw new ParseException(null, 0, "분기조건문이 일치하지 않습니다.");
                        }
                    }
                    // 파서에 입력된 값을 등록
                    else
                    {
                        m_Term.Parse(textbox.Text);
                        m_Term.SetVar(str[0], m_Term.Value);
                    }


                    vdata.Value = m_Term.Value;
                }
            }
        }
        void Assert(string equation, double result)
        {
            try
            {
                m_term.Parse(equation);
                if (m_term.Value != result)
                {
                    string text = string.Format("Fail;; - Equation {0}, Parsed {1}, Value {2}, Expected {3}", equation, m_term.ToString(), m_term.Value, result);
                    throw new EquationUnitTestException(equation, text);
                }
                // MainForm.m_LogWindow.WriteLog(string.Format("성공 - Equation {0}, Parsed {1}, Value {2}", equation, m_term.ToString(), m_term.Value));
            }
            catch (Exception error)
            {
                throw new EquationUnitTestException(equation, error.Message);
            }
            //m_term.ClearVars();
            var strings = m_term.GetVars();

            foreach (var str in strings)
            {
            }
        }
示例#4
0
        public Function(string src, int start, EquationElement root)
        {
            m_startIndex = start;
            m_func       = Term.ExtractName(src, start).ToLower();
            start       += m_func.Length;
            // space는 무시, 다음문자는 무조건 괄호가 와야함.
            while (src[start] == ' ')
            {
                start++;
            }
            if (src[start] != '(')
            {
                throw new ParseException(src, m_startIndex, "함수는 무조건 () 로 실행해야 합니다.'");
            }
            int termstart = start;
            int end       = Term.FindMatchingEnd('(', src, termstart);

            if (end < termstart)
            {
                throw new ParseException(src, m_startIndex, "함수 매칭 실패");
            }

            m_endindex = end;
            string allterms = src.Substring(termstart + 1, end - termstart - 1);

            //string[] terms = allterms.Split(',');
            string[] terms = GetTerms(allterms);
            m_terms = new List <Term>();
            foreach (string term in terms)
            {
                Term newterm = new Term();
                newterm.Parse(term, root);
                newterm.Parent = this;
                m_terms.Add(newterm);
            }
        }
示例#5
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);
        }