コード例 #1
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;


            FunctionInfo functionInfo = FormulaBuilderTools.Instance.GetFunctionElements(pattern);

            if (!functionInfo.IsOK)
            {
                return(false);
            }

            string functionName = functionInfo.Name;

            if (!_functions.Keys.Contains(functionName))
            {
                return(false);
            }

            Type functionType = _functions[functionName];

            item         = (FunctionItem)Activator.CreateInstance(functionType);
            item.Pattern = pattern;

            string[] args = functionInfo.Arguments.Split(FUNCTION_ARGUMENT_SEPARATOR);

            foreach (string arg in args)
            {
                FormulaItem argItem = builder.Create(arg);
                (item as FunctionItem).Arguments.Add(argItem);
            }

            return(true);
        }
コード例 #2
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;

            List <string> elements = FormulaBuilderTools.Instance.GetElements(pattern, OperationType.Power);

            if (elements.Count != 3 || elements[1] != "^")
            {
                return(false);
            }

            FormulaItem baseItem     = builder.Create(elements[0]);
            FormulaItem exponentItem = builder.Create(elements[2]);

            item         = new FunctionPow();
            item.Pattern = pattern;
            (item as FunctionItem).Arguments.Add(baseItem);
            (item as FunctionItem).Arguments.Add(exponentItem);

            return(true);
        }
コード例 #3
0
        private void onFormulaChanged(object sender, EventArgs e)
        {
            if (!checkBox1.Checked)
            {
                return;
            }

            try {
                string      pattern = textBox1.Text;
                FormulaItem formula = _builder.Create(pattern);
                double      value   = formula.GetValue();
                textBox2.Text = value.ToString();

                _element = _converter.Convert(formula);
                updateTree(formula);
                panel1.Invalidate();
            } catch (Exception exc) {
                textBox2.Text = "[ERROR]";
                _element      = null;
            }
        }
コード例 #4
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;
            string trimmed = pattern.Trim();



            bool isMatchRound   = Regex.IsMatch(trimmed, REGEX_ROUND);
            bool isMatchSquared = Regex.IsMatch(trimmed, REGEX_SQUARED);
            bool isAnyMatch     = isMatchRound || isMatchSquared;



            if (!isAnyMatch)
            {
                return(false);
            }

            item         = new BracketItem();
            item.Pattern = pattern;

            string args = null;

            if (isMatchRound)
            {
                args = RegexTools.GetValue(pattern, REGEX_ROUND, "args");
            }

            if (isMatchSquared)
            {
                args = RegexTools.GetValue(pattern, REGEX_SQUARED, "args");
            }

            FormulaItem argsItem = builder.Create(args);

            (item as BracketItem).Arguments.Add(argsItem);
            if (isMatchRound)
            {
                (item as BracketItem).TypeOfBracket = BracketShapeType.Round;
            }

            if (isMatchSquared)
            {
                (item as BracketItem).TypeOfBracket = BracketShapeType.Squared;
            }


            return(true);
        }
コード例 #5
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item    = null;
            pattern = pattern.Trim();
            string args = RegexTools.GetValue(pattern, REGEX, "args");

            if (string.IsNullOrEmpty(args))
            {
                return(false);
            }

            item         = new FunctionAbs();
            item.Pattern = pattern;
            FormulaItem arg = builder.Create(args);

            (item as FunctionAbs).Arguments.Add(arg);

            return(true);
        }
コード例 #6
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;

            List <string> elements = FormulaBuilderTools.Instance.GetElements(pattern, OperationType.AddSubstr);

            if (elements.Count < 3)
            {
                return(false);
            }

            item         = new FunctionSum();
            item.Pattern = pattern;

            for (int i = 0; i < elements.Count; ++i)
            {
                string element = elements[i];
                if (element == "+" || element == "-")
                {
                    continue;
                }


                double coeff = 1.0;
                if (i > 0)
                {
                    coeff = (elements[i - 1] == "+") ? 1.0 : -1.0;
                }

                FormulaItem arg = builder.Create(element);
                (item as FunctionSum).Arguments.Add(arg);
                (item as FunctionSum).Coeffs.Add(coeff);
            }

            return(true);
        }
コード例 #7
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;

            List <string> elements = FormulaBuilderTools.Instance.GetElements(pattern, OperationType.MultiDivide);

            if (elements.Count < 3)
            {
                return(false);
            }

            item         = new FunctionMultiply();
            item.Pattern = pattern;

            for (int i = 0; i < elements.Count; ++i)
            {
                string element = elements[i];
                if (element == "*" || element == "/")
                {
                    continue;
                }


                bool coeff = true;
                if (i > 0)
                {
                    coeff = (elements[i - 1] == "*") ? true : false;
                }

                FormulaItem arg = builder.Create(element);
                (item as FunctionMultiply).Arguments.Add(arg);
                (item as FunctionMultiply).Flags.Add(coeff);
            }

            return(true);
        }
コード例 #8
0
        public override bool TryCreate(List <string> lines, CalculationBuilder builder, out CalculationItem item)
        {
            string pattern = lines.First();

            bool isMatch = Regex.IsMatch(pattern, REGEX_PATTERN);

            if (!isMatch)
            {
                FormulaItem supposedFormula = _formulaBuilder.Create(pattern);
                if (supposedFormula is ValueItem)
                {
                    Equation iEquation = new Equation();
                    iEquation.Value = (ValueItem)supposedFormula;
                    item            = iEquation;
                    builder.RemoveLines(lines, 1);
                    return(true);
                }

                if (supposedFormula is SymbolItem)
                {
                    Equation iEquation = new Equation();
                    iEquation.Symbol = (SymbolItem)supposedFormula;
                    item             = iEquation;
                    builder.RemoveLines(lines, 1);
                    return(true);
                }

                item = null;
                return(false);
            }

            string left  = RegexTools.GetValue(pattern, REGEX_PATTERN, "left");
            string right = RegexTools.GetValue(pattern, REGEX_PATTERN, "right");

            FormulaItem symbol  = _formulaBuilder.Create(left);
            FormulaItem formula = _formulaBuilder.Create(right);

            Equation equation = new Equation();

            equation.Symbol = (SymbolItem)symbol;

            if (formula is ValueItem)
            {
                equation.Value = (ValueItem)formula;
            }
            else
            {
                if (formula.IsSymbol)
                {
                    equation.Symbolic = formula;
                }
                else
                {
                    equation.Numbers = formula;
                }
            }

            item = equation;

            builder.RemoveLines(lines, 1);

            return(true);
        }