コード例 #1
0
        public void commit(Stack <ExpressionPart> values)
        {
            EOperand left  = null;
            EOperand right = null;

            if (this.priority != 6)
            {
                right = (EOperand)values.Pop();
            }
            left = (EOperand)values.Pop();

            try
            {
                switch (this.priority)
                {
                case 6:
                    values.Push(new EOperand((!left.value).ToString()));
                    break;

                case 5:
                    values.Push(new EOperand((left.value & right.value).ToString()));
                    break;

                case 4:
                    values.Push(new EOperand((left.value | right.value).ToString()));
                    break;

                case 3:
                    values.Push(new EOperand(((!left.value) | right.value).ToString()));
                    break;

                case 2:
                    values.Push(new EOperand((left.value == right.value).ToString()));
                    break;
                }
            }
            catch (InvalidOperationException ex) {
                throw new InvalidOperationException("Some of operands doesn't have it's value(value property == null)", ex);
            }
        }
コード例 #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>();
        }
コード例 #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);
        }