コード例 #1
0
        private Stack <double> GetConvertStack(Stack <string> operands)
        {
            Stack <double> result = new Stack <double>();

            while (!operands.IsEmpty())
            {
                if (!(operands.Top() == "pi" || operands.Top() == "e"))
                {
                    result.Push(double.Parse(operands.Top()));
                }
                else
                {
                    if (operands.Top() == "pi")
                    {
                        result.Push(Math.PI);
                    }
                    else
                    {
                        result.Push(Math.E);
                    }
                }
                operands.Pop();
            }

            return(result.ReverseStack());
        }
コード例 #2
0
        public string GetReverseNotation(string line, out Stack <Actions> actions, out Stack <string> operands)
        {
            string result = "";

            actions  = new Stack <Actions>();
            operands = new Stack <string>();

            Stack <Actions> functions = new Stack <Actions>();

            for (int i = 0; i < line.Length; i++)
            {
                if (IsSplite(line[i]))
                {
                    continue;
                }
                if (IsDigit(line[i]) && (i == 0 || line[i - 1] != '.'))
                {
                    AddDigits(ref result, line, ref i, operands);
                    continue;
                }

                if (line[i] == '(')
                {
                    functions.Push(new Actions("(", false, -1));
                    continue;
                }

                if (line[i] == ')')
                {
                    AddFunctionsAndOperations(functions, ref result, actions);
                    continue;
                }

                if (IsBinaryOperation(line, i))
                {
                    AddOperationToStack(functions, line, i, ref result, actions);
                    continue;
                }

                if (IsConstant(line, ref i, ref result, operands))
                {
                    continue;
                }

                AddFunctionToStack(functions, line, ref i);
            }

            while (!functions.IsEmpty())
            {
                if (!IsFunction(functions.Top().Name) && (functions.Top().Name.Length == 0 ||
                                                          functions.Top().Name[0] != '+' && functions.Top().Name[0] != '-' &&
                                                          functions.Top().Name[0] != '*' && functions.Top().Name[0] != '/' &&
                                                          functions.Top().Name[0] != '^'))
                {
                    throw new Exception("Строка не корректно задана");
                }
                result += functions.Top().Name;
                actions.Push(functions.Top());
                result += ' ';
                functions.Pop();
            }

            actions  = actions.ReverseStack();
            operands = operands.ReverseStack();

            return(result);
        }