Пример #1
0
        public LogicExpression(LogicExpression leftExpr, LogicExpression rightExpr, EOperation op)
        {
            //Creating new "body" sequence with two existing sequences separated with EQU operation
            List <ExpressionPart> inputLeft   = new List <ExpressionPart>(leftExpr.body);
            List <EOperand>       newOperands = new List <EOperand>(leftExpr.operands);

            inputLeft[inputLeft.Count - 1] = new EBrackets(")");
            inputLeft.Insert(1, new EBrackets("("));

            inputLeft.Add(op);

            List <ExpressionPart> inputRight    = new List <ExpressionPart>(rightExpr.body);
            List <EOperand>       rightOperands = new List <EOperand>(rightExpr.operands);

            inputRight[0] = new EBrackets("(");
            inputRight.Insert(inputRight.Count - 1, new EBrackets(")"));

            inputLeft.AddRange(inputRight);
            newOperands.AddRange(rightOperands);

            //Removing vars repeating
            for (int i = 0; i < newOperands.Count; i++)
            {
                for (int j = i + 1; j < newOperands.Count; j++)
                {
                    if (newOperands[i].Equals(newOperands[j]))
                    {
                        newOperands.RemoveAt(j);
                    }
                }
            }

            //Recovering reference connection between .body and .operands
            for (int i = 0; i < inputLeft.Count; i++)
            {
                if (!(inputLeft[i] is EOperand))
                {
                    continue;
                }
                for (int j = 0; j < newOperands.Count; j++)
                {
                    if (inputLeft[i].Equals(newOperands[j]) && !Object.ReferenceEquals(inputLeft, newOperands))
                    {
                        inputLeft[i] = newOperands[j];
                    }
                }
            }

            this.body     = new Queue <ExpressionPart>(inputLeft);
            this.operands = newOperands;
        }
Пример #2
0
        public LogicExpression(String strExpression)
        {
            //Parsing expression from string to set of ExpressionPart objects
            body = new Queue <ExpressionPart>();
            String delimiters = LogicSymbols.OR + LogicSymbols.AND + LogicSymbols.IMP + LogicSymbols.NOT;

            String[] parts = Regex.Split(strExpression, @"(?=[" + delimiters + ")(]|)|(?<=[" + delimiters + ")(])");
            body.Enqueue(new ESeparateSymbol(true));

            ExpressionPart x;

            foreach (String i in parts)
            {
                if (EBrackets.isSuitablePart(i))
                {
                    body.Enqueue(new EBrackets(i));
                }
                else if (EOperation.isSuitablePart(i))
                {
                    x = new EOperation(i);
                    body.Enqueue(x);
                }
                else if (EOperand.isSuitablePart(i))
                {
                    x = new EOperand(i);
                    if (operands.Contains(x) && !((EOperand)x).isConstant)
                    {
                        x = operands.First <EOperand>(p => p.Equals(x));
                    }
                    if (!operands.Contains(x) && !((EOperand)x).isConstant)
                    {
                        operands.Add((EOperand)x);
                    }
                    body.Enqueue((EOperand)x);
                }
            }
            body.Enqueue(new ESeparateSymbol(false));
            this.operands = this.operands.OrderBy <EOperand, String>(p => p.text).ToList <EOperand>();
        }