コード例 #1
0
        public float CalculateONPExpresion(string onpExpression)
        {
            int id = 0;
            OutputOperationBuffer = new InternalBuffer<OutputOperation>();

            InternalStack<string> stack = new InternalStack<string>();

            List<string> tokenList = onpExpression.Trim().Split(' ').ToList();
            string input = onpExpression.Trim();

            OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));

            foreach (string token in tokenList)
            {
                if (!string.IsNullOrEmpty(input.Trim()))
                    input = input.Trim().Remove(0, 1);

                if (token.IsOperator())
                {
                    string secondToken = stack.Pop();
                    string firstToken = stack.Pop();
                    string result = Calculate(token, firstToken, secondToken);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Format("{0} {1} {2}", firstToken, token, secondToken)));

                    stack.Push(result);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));
                }
                else
                {
                    stack.Push(token);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), string.Empty));
                }
            }

            string resultToken = stack.Pop();
            float resultValue = float.Parse(resultToken);
            return resultValue;
        }
コード例 #2
0
        public string ConvertToONP(string infix)
        {
            int id = 0;
            OutputOperationBuffer = new InternalBuffer<OutputOperation>();
            InternalStack<Operator> stack = new InternalStack<Operator>();

            string output = string.Empty;
            char[] infixArray = infix.Trim().ToArray();
            string input = infix;
            OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));

            foreach (char infixChar in infixArray)
            {
                if(!string.IsNullOrEmpty(input))
                    input = input.Remove(0, 1);

                if (!infixChar.IsOperator())
                {
                    AddToOutput(ref output, infixChar);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else if (infixChar == Operators.OpenBracket)
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    stack.Push(newOperator);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else if (infixChar == Operators.CloseBracket)
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    while (stack.Any() && stack.Peek().OperatorType != Operators.OpenBracket)
                    {
                        AddToOutput(ref output, stack.Pop());
                        OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                    }
                    stack.Pop();

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
                else
                {
                    AddToOutput(ref output);

                    Operator newOperator = new Operator(infixChar);
                    while (stack.Any() && stack.Peek().Priority >= newOperator.Priority)
                    {
                        AddToOutput(ref output, stack.Pop());
                    }
                    stack.Push(newOperator);

                    OutputOperationBuffer.Push(new OutputOperation(id++, input, stack.ToReverseString(), output));
                }
            }
            while (stack.Any())
            {
                AddToOutput(ref output, stack.Pop());
            }

            return output;
        }