示例#1
0
        static void Main(string[] args)
        {
            string mode;
            string input_line = "";

            Console.WriteLine("Укажите пожалуйста режим работы:\n1 - Своя формула (бой)\n2- Предзаполнение (тест) [2 / (3 + 2) * 5]");
            mode = Console.ReadLine();

            if (mode == "1")
            {
                Console.WriteLine("Введите пожалуйста математическое выражение:");
                input_line = Console.ReadLine();
            }
            else if (mode == "2")
            {
                input_line = "2 / (3 + 2) * 5";
                Console.WriteLine("Проверяем работу программы на выражении {0}", input_line);
            }

            if (input_line == "")
            {
                Console.WriteLine("Строка выражения пустая, обработка невозмможна");
            }

            if (input_line != "")
            {
                Console.WriteLine("Берем в обработку {0}", input_line);

                input_line = input_line.Replace(" ", "");
                ProcessingExpression collect = new ProcessingExpression();
                collect.GetOutputString(input_line);
                CalculatePolskyReverse calculate = new CalculatePolskyReverse();
                calculate.GetResult(collect.resultExpression);

                Console.WriteLine(collect.resultExpression + "\n" + calculate.result);
            }
            else
            {
                Console.WriteLine("Передана пустая строка!");
            }
        }
示例#2
0
        public void GetOutputString(string expression)
        {
            CollectPolskyReverseString calculateResult = new CollectPolskyReverseString();

            for (int i = 0; i < expression.Length; i++)
            {
                Element thisElement = new Element();
                thisElement.Initialization(expression[i].ToString());

                if (thisElement.type == "Number")
                {
                    if (operation != "" && bracketLevel == 0)
                    {
                        calculateResult.AddCharToResult(thisElement.symbol + " " + operation);
                        operation = "";
                    }
                    else if (operation == "" && bracketLevel == 0)
                    {
                        calculateResult.AddCharToResult(thisElement.symbol);
                    }

                    if (operationInBracket != "" && bracketLevel != 0)
                    {
                        bracketExpression += operationInBracket;
                        bracketExpression += thisElement.symbol;
                    }
                    else if (operationInBracket == "" && bracketLevel != 0)
                    {
                        bracketExpression += thisElement.symbol;
                    }
                }

                if (thisElement.type == "Operation")
                {
                    if (operation != "" && bracketLevel == 0)
                    {
                        calculateResult.AddCharToResult(thisElement.symbol);
                        operation = "";
                    }
                    else if (operation == "" && bracketLevel == 0)
                    {
                        operation = thisElement.symbol;
                    }

                    if (operationInBracket != "" && bracketLevel != 0)
                    {
                        calculateResult.AddCharToResult(thisElement.symbol);
                        operationInBracket = "";
                    }
                    else if (operationInBracket == "" && bracketLevel != 0)
                    {
                        operationInBracket = thisElement.symbol;
                    }
                }

                if (thisElement.type == "Bracket")
                {
                    if (thisElement.symbol == "(")
                    {
                        bracketLevel += 1;
                    }

                    if (thisElement.symbol == ")")
                    {
                        ProcessingExpression calculateBracket = new ProcessingExpression();
                        calculateBracket.GetOutputString(bracketExpression);
                        calculateResult.AddCharToResult(calculateBracket.resultExpression.Trim());
                        if (operation != "")
                        {
                            calculateResult.AddCharToResult(operation);
                            operation = "";
                        }
                        bracketLevel -= 1;
                    }
                }
            }
            resultExpression = calculateResult.polskyString;
        }