Exemple #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Строковый калькулятор");
            string s = null;

            while (s != "нет")
            {
                Console.WriteLine("Введите пример:");
                s = Console.ReadLine();
                var result = new StringCalc(s);
                Console.WriteLine(result.Message);
                Console.WriteLine("Повторить?(да/нет)");
                s = Console.ReadLine();
            }
        }
Exemple #2
0
        private static void Main(string[] args)
        {
            char[] operations = { ',', '+', '-', '/', '*', '^', '(', ')', 'E', 'e' };
            do
            {
                try
                {
                    string expression = string.Empty;
                    char   a;
                    while ((a = Console.ReadKey(true).KeyChar) != '=')
                    {
                        if (char.IsDigit(a) || operations.Contains(a))
                        {
                            expression += a;
                            Console.Write(a);
                        }

                        if (a == '\b' && expression.Length != 0)
                        {
                            expression = expression.Remove(expression.Length - 1);
                            Console.Write("\b \b");
                        }
                    }

                    Console.Write('=');
                    StringCalc calculator = new StringCalc();

                    Console.WriteLine(calculator.Calculate(expression));
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                }
                catch (OverflowException)
                {
                    Console.WriteLine("\nOverflow error in the calculation");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    Console.WriteLine("Try again?[Esc to exit]");
                }
            } while (Console.ReadKey(true).Key != ConsoleKey.Escape);
        }