Пример #1
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            item = null;

            string trimmed = pattern.Trim();

            ValueInfo valueInfo = FormulaBuilderTools.Instance.GetValueInfo(trimmed);

            if (!valueInfo.IsOk)
            {
                return(false);
            }

            item         = new ValueItem();
            item.Pattern = pattern;
            (item as ValueItem).Value = valueInfo.Value;

            UnitItem unit = new UnitItem();

            unit.Unit    = valueInfo.Unit;
            unit.Pattern = valueInfo.Unit;
            (item as ValueItem).Arguments.Add(unit);



            return(true);
        }
Пример #2
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);
        }
Пример #3
0
        public override double GetValue()
        {
            double result = 0;

            for (int i = 0; i < Arguments.Count; ++i)
            {
                FormulaItem item  = Arguments[i];
                double      value = item.GetValue();
                if (i == 0)
                {
                    result = value;
                    continue;
                }

                bool flag = Flags[i];
                if (flag)
                {
                    result *= value;
                }
                else
                {
                    result /= value;
                }
            }

            return(result);
        }
Пример #4
0
        public override double GetValue()
        {
            FormulaItem value  = Arguments.First();
            double      arg    = value.GetValue();
            double      result = Math.Abs(arg);

            return(result);
        }
Пример #5
0
        public override double GetValue()
        {
            FormulaItem baseItem     = Arguments[0];
            FormulaItem exponentItem = Arguments[1];

            double baseValue     = baseItem.GetValue();
            double exponentValue = exponentItem.GetValue();

            return(Math.Pow(baseValue, exponentValue));
        }
Пример #6
0
        private void button1_Click(object sender, EventArgs e)
        {
            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();
        }
Пример #7
0
        public override FormulaItem Clone(List <SymbolValuePair> values)
        {
            FunctionItem cloned = (FunctionItem)Activator.CreateInstance(this.GetType());

            foreach (FormulaItem argument in Arguments)
            {
                FormulaItem clonedArg = (FormulaItem)argument.Clone(values);
                cloned.Arguments.Add(clonedArg);
            }

            return(cloned);
        }
Пример #8
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);
        }
Пример #9
0
        void addItem(TreeNode parent, FormulaItem item)
        {
            TreeNode node = new TreeNode();

            node.Text = item.Pattern;
            parent.Nodes.Add(node);

            if (item is FunctionItem)
            {
                FunctionItem function = (FunctionItem)item;

                foreach (FormulaItem arg in function.Arguments)
                {
                    addItem(node, arg);
                }
            }
        }
Пример #10
0
        public void CreateFormulaTree(TreeView view, FormulaItem formula)
        {
            TreeNode node = new TreeNode();

            node.Text = formula.Pattern;
            view.Nodes.Add(node);

            if (formula is FunctionItem)
            {
                FunctionItem function = (FunctionItem)formula;

                foreach (FormulaItem arg in function.Arguments)
                {
                    addItem(node, arg);
                }
            }
        }
Пример #11
0
        public override bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item)
        {
            string trimmed = pattern.Trim();

            item = null;
            bool isSymbol = Regex.IsMatch(trimmed, REGEX) || Regex.IsMatch(trimmed, REGEX2);

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

            item         = new SymbolItem();
            item.Pattern = pattern;
            (item as SymbolItem).Symbol = trimmed;

            return(true);
        }
Пример #12
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);
        }
Пример #13
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;
            }
        }
Пример #14
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);
        }
Пример #15
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);
        }
Пример #16
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);
        }
Пример #17
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);
        }
Пример #18
0
        public Element Convert(FormulaItem item)
        {
            Element element = null;

            if (item is ValueItem)
            {
                ValueItem    i = (ValueItem)item;
                ValueElement e = new ValueElement();
                e.Value = i.Value.ToString();
                element = e;
            }

            if (item is SymbolItem)
            {
                SymbolItem    i = (SymbolItem)item;
                SymbolElement e = new SymbolElement();

                if (!string.IsNullOrEmpty(i.Symbol))
                {
                    string[] splited = i.Symbol.Split('.');
                    e.Symbol = splited[0];
                    if (splited.Length == 2)
                    {
                        e.Suffix = splited[1];
                    }
                }

                element = e;
            }

            if (item is UnitItem)
            {
                UnitItem    u = (UnitItem)item;
                UnitElement e = new UnitElement();
                e.Unit  = u.Unit;
                element = e;
            }



            if (item is FunctionSum)
            {
                FunctionSum sum     = (FunctionSum)item;
                SumElement  sumElem = new SumElement();

                for (int i = 0; i < sum.Coeffs.Count; ++i)
                {
                    if (i == 0)
                    {
                        sumElem.Signs.Add(string.Empty);
                        continue;
                    }
                    double coeff = sum.Coeffs[i];
                    string sign  = (coeff > 0) ? "+" : "-";
                    sumElem.Signs.Add(sign);
                }

                element = sumElem;
            }

            if (item is FunctionMultiply)
            {
                FunctionMultiply multi = (FunctionMultiply)item;

                if (multi.Arguments.Count == 2 && multi.Flags[1] == false)
                {
                    FractionElement fraction = new FractionElement();
                    element = fraction;
                }
                else
                {
                    SumElement sumElem = new SumElement();

                    for (int i = 0; i < multi.Flags.Count; ++i)
                    {
                        if (i == 0)
                        {
                            sumElem.Signs.Add(string.Empty);
                            continue;
                        }
                        bool   flag = multi.Flags[i];
                        string sign = flag ? "x" : "/";
                        sumElem.Signs.Add(sign);
                    }

                    element = sumElem;
                }
            }

            if (item is FunctionSqrt)
            {
                element = new SqrtElement();
            }

            if (item is FunctionPow)
            {
                element = new PowElement();
            }

            if (item is FunctionMax)
            {
                element = createElement("max");
            }

            if (item is FunctionMin)
            {
                element = createElement("min");
            }

            if (item is FunctionSin)
            {
                element = createElement("sin");
            }

            if (item is FunctionCos)
            {
                element = createElement("cos");
            }

            if (item is FunctionTan)
            {
                element = createElement("tan");
            }


            if (item is BracketItem)
            {
                element = createElement("");
            }



            if (item is FunctionItem)
            {
                FunctionItem function = (FunctionItem)item;
                foreach (FormulaItem arg in function.Arguments)
                {
                    Element argElement = Convert(arg);
                    element.Arguments.Add(argElement);
                }
            }

            return(element);
        }
Пример #19
0
 public abstract bool TryCreate(string pattern, FormulaBuilder builder, out FormulaItem item);
Пример #20
0
 void updateTree(FormulaItem item)
 {
     treeView1.Nodes.Clear();
     TreeViewFactory.Instance.CreateFormulaTree(treeView1, item);
     treeView1.ExpandAll();
 }