Beispiel #1
0
        static void Main(string[] args)
        {
            Thread.CurrentThread.CurrentUICulture = new CultureInfo("ru-RU");

            Console.WriteLine(Messages.Greeting);
            Console.WriteLine(Messages.GoodbyeMessage);

            while (true)
            {
                string s = "";
                try
                {
                    while (true)
                    {
                        var k = Console.ReadKey();
                        if (k.KeyChar == '=')
                        {
                            Console.WriteLine(ParsingHelper.Calculate(s));
                            break;
                        }
                        else
                        {
                            s += k.KeyChar;
                        }
                    }

                    break;
                }
                catch (DivideByZeroException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
            }
        }
        //This method executes calculations.
        public static double Calculate(string s)
        {
            CalculatorDelegate d = Calculator;

            if (s.Length < 3)
            {
                throw new ArgumentException("Input Error! Please, try again.");
            }

            string[] arr = ParsingHelper.MyParse(s);
            arr = Converter(arr, d);
            int    i   = 1;
            double res = Convert.ToDouble(arr[0]);

            while (i < arr.Length)
            {
                res = d(res, Convert.ToDouble(arr[i + 1]), arr[i]);
                i  += 2;
            }

            return(res);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            Console.WriteLine(" Enter the expression with " +
                              "'+', '-', '*', '/', '(', ')' signs");
            while (true)
            {
                string s = "";
                try
                {
                    while (true)
                    {
                        var k = Console.ReadKey();
                        if (k.KeyChar == '=')
                        {
                            Console.WriteLine(ParsingHelper.Calculate(s));
                            break;
                        }
                        else
                        {
                            s += k.KeyChar;
                        }
                    }

                    break;
                }
                catch (DivideByZeroException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
                catch (ArgumentException ex)
                {
                    Console.WriteLine(ex.Message);
                    continue;
                }
            }
        }