示例#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);
        }
示例#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);
        }
示例#3
0
 public override void Remove(TreeComponent component)
 {
     this._child = null;
 }
示例#4
0
 public override void Add(TreeComponent component)
 {
     this._child = component;
 }
示例#5
0
 public void AddOperation(TreeComponent component)
 {
     this._operation = component;
 }
示例#6
0
 public void AddSecondExp(TreeComponent component)
 {
     this._secondExp = component;
 }
示例#7
0
 public void AddFirstExp(TreeComponent component)
 {
     this._firstExp = component;
 }
示例#8
0
 public override void Remove(TreeComponent component)
 {
     this._children.Remove(component);
 }
示例#9
0
 public override void Add(TreeComponent component)
 {
     this._children.Add(component);
 }
示例#10
0
 public virtual void Remove(TreeComponent component)
 {
     throw new NotImplementedException();
 }