Ejemplo n.º 1
0
        // 20*3+34/2
        private static TreeComponent FindExpressions(List <string> list, int position1, out int position)
        {
            int count = 0;

            position = position1;
            for (int i = position; i < list.Count; i++)
            {
                var sTemp = list[i];
                if (isOperator(sTemp))
                {
                    i++;
                    TreeComponent c1 = FindExpressions(list, i, out i);
                    i++;
                    TreeComponent         c2   = FindExpressions(list, i, out i);
                    ComplicatedExpression comp = new ComplicatedExpression();
                    var operation = new OperationComponent();
                    operation.value = sTemp;
                    comp._operation = operation;
                    comp.AddFirstExp(c2);
                    comp.AddSecondExp(c1);
                    position = i;
                    return(comp);
                }
                else
                {
                    var           simple = new SimpleExpression();
                    TreeComponent varComp;
                    if (isConstant(list[i]))
                    {
                        varComp = new Constant();
                        var varDig = new Digit();
                        varDig.value = int.Parse(list[i]);
                        varComp.Add(varDig);
                    }
                    else
                    {
                        varComp = new Variable();
                        var varDig = new Name();
                        varDig.value = list[i];
                        varComp.Add(varDig);
                    }
                    simple.Add(varComp);
                    position = i;
                    return(simple);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        private static TreeComponent ParseExpression(List <string> list)
        {
            TreeComponent mainComp = new Expression();

            for (int i = 0; i < list.Count; i++)
            {
                if (isOperator(list[i]))
                {
                    ComplicatedExpression cExp = new ComplicatedExpression();
                    var operation = new OperationComponent();
                    operation.value = list[i];
                    cExp._operation = operation;
                    i++;
                    TreeComponent complicated1 = FindExpressions(list, i, out i);
                    i++;
                    TreeComponent complicated2 = FindExpressions(list, i, out i);
                    cExp.AddFirstExp(complicated2);
                    cExp.AddSecondExp(complicated1);
                    mainComp.Add(cExp);
                    Console.WriteLine($"Складний вираз:\n{cExp._firstExp.Operation()}{cExp._operation.Operation()}{cExp._secondExp.Operation()}");
                }
                else
                {
                    var           simple = new SimpleExpression();
                    TreeComponent varComp;
                    if (isConstant(list[i]))
                    {
                        varComp = new Constant();
                        var varDig = new Digit();
                        varDig.value = int.Parse(list[i]);
                        varComp.Add(varDig);
                    }
                    else
                    {
                        varComp = new Variable();
                        var varDig = new Name();
                        varDig.value = list[i];
                        varComp.Add(varDig);
                    }
                    simple.Add(varComp);
                    mainComp.Add(simple);
                }
            }
            return(mainComp);
        }