Exemplo n.º 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;
        }
Exemplo n.º 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>();
        }
Exemplo n.º 3
0
        //Method finds solution of logic expression using RPN
        public EOperand calculate()
        {
            if (!isRPNcreated)
            {
                throw new InvalidOperationException("Reversed polish notation hasn't been created yet");
            }

            Stack <ExpressionPart> calcField      = new Stack <ExpressionPart>();
            Stack <ExpressionPart> polishNotation = new Stack <ExpressionPart>(California);

            while (polishNotation.Count > 0)
            {
                //Selecting operands
                while ((polishNotation.Count > 0) && (polishNotation.Peek() is EOperand))
                {
                    calcField.Push(polishNotation.Pop());
                }

                //Looking for operators and computing
                while ((polishNotation.Count > 0) && (polishNotation.Peek() is EOperation))
                {
                    EOperation op = null;
                    try
                    {
                        op = (EOperation)polishNotation.Pop();
                        op.commit(calcField);
                    }
                    catch (InvalidOperationException) {
                        throw new FormatException("Math expression isn't correct");
                    }
                }
            }

            this.isCalculated = true;
            this.solution     = (EOperand)calcField.Pop();
            return(this.solution);
        }