static void Main(string[] args)
        {
            ReversePolishNotation rpn    = new ReversePolishNotation();
            RCalculator           MyCalc = new RCalculator(new List <IOperation>()
            {
                new Addition(),
                new Substraction(),
                new Multiplication(),
                new Division()
            });

            string inputString = Console.ReadLine();

            /* Проверка выражения на верную структуру */
            if (Regex.IsMatch(inputString, @"[^()0-9*/+-]|[*/+-]{2,}|\A[*/+-]|[*/+-]\z"))
            {
                Console.WriteLine("Invalid input");
                Environment.Exit(-1);
            }

            rpn.Parse(inputString);
            List <string> parsedExpression = rpn.RpnExpression;
            List <string> executionList    = new List <string>();
            float         leftOp           = 0;
            float         rightOp          = 0;
            float         result           = 0;
            string        operation        = "";

            foreach (var item in parsedExpression)
            {
                if (!float.TryParse(item, out _)) //Если item не парсится во float, то item это символ операции
                {
                    leftOp    = float.Parse(executionList[^ 2]);
Exemple #2
0
 static void Main(string[] args)
 {
     while (true)
     {
         Console.Write("Введите выражение: ");
         Console.WriteLine(ReversePolishNotation.Calculate(Console.ReadLine())); //Считываем, и выводим результат
     }
 }
Exemple #3
0
        public double Calculate(String expression)
        {
            string[] str   = ReversePolishNotation.ConvertToReversePolishNotation(ReversePolishNotation.DeleteSpaces(expression));
            var      stack = new MyStack <double>();

            foreach (var s in str)
            {
                double number;
                if (Double.TryParse(s, out number))
                {
                    stack.Push(number);
                }
                else
                {
                    double num1;
                    double num2;
                    switch (s)
                    {
                    case "+":
                        num1 = stack.Pop();
                        num2 = stack.Pop();
                        stack.Push(num1 + num2);
                        break;

                    case "-":
                        num1 = stack.Pop();
                        num2 = stack.Pop();
                        stack.Push(num2 - num1);
                        break;

                    case "*":
                        num1 = stack.Pop();
                        num2 = stack.Pop();
                        stack.Push(num1 * num2);
                        break;

                    case "/":
                        num1 = stack.Pop();
                        num2 = stack.Pop();
                        stack.Push(num2 / num1);
                        break;
                    }
                }
            }
            return(stack.Pop());
        }
Exemple #4
0
        static void Main(string[] args)
        {
            ReversePolishNotation rpn = new ReversePolishNotation();

            while (true)
            {
                Console.WriteLine("Введите выражение:");
                Console.WriteLine("Введите Выход/Exit, чтобы закончить операцию");

                string expression = "";
                bool   exitStatus = false;

                while (true)
                {
                    expression = Console.ReadLine();
                    expression = expression.ToLower();
                    expression = expression.Replace(" ", string.Empty);

                    if (expression == "выход" || expression == "exit" || expression == "ds[jl" || expression == "учше")
                    {
                        exitStatus = true;
                        break;
                    }

                    if (rpn.StringToRPN(expression) != "Decoding error, let's try again")
                    {
                        break;
                    }

                    Console.WriteLine("Try to enter expression correctly again");
                }

                if (exitStatus)
                {
                    break;
                }

                Console.WriteLine(rpn.StringToRPN(expression));
                Console.WriteLine(rpn.RPNToAnswer(rpn.StringToRPN(expression)));
            }
        }