Exemplo n.º 1
0
        static void Test1()
        {
            //10+5 = 15
            Console.WriteLine("Enter string");
            string str  = Console.ReadLine();
            char   Sign = ' ';

            try
            {
                if (str.Contains('+'))
                {
                    Sign = '+';
                }
                else if (str.Contains('-'))
                {
                    Sign = '-';
                }
                else if (str.Contains('*'))
                {
                    Sign = '*';
                }
                else if (str.Contains('/'))
                {
                    Sign = '/';
                }
                else
                {
                    Sign = ' ';
                }
                string[] numbers = str.Split(Sign, StringSplitOptions.RemoveEmptyEntries);
                double   a       = double.Parse(numbers[0]);
                double   b       = double.Parse(numbers[1]);

                MathMethod mathod  = null;
                MyMath     algebra = new MyMath();

                switch (Sign)
                {
                case '+':
                    mathod = new MathMethod(algebra.Sum);     // старий запис
                    //Console.WriteLine(a+b);
                    break;

                case '-':
                    mathod = algebra.Sub;
                    break;

                case '*':
                    mathod = MyMath.Mult;
                    break;

                case '/':
                    mathod = MyMath.Div;
                    break;
                }
                //Console.WriteLine(mathod(a, b));
                Console.WriteLine(mathod?.Invoke(a, b));

                Console.WriteLine("-------------------------------------------------");
                MathMethod math = null;
                math  = MyMath.Mult; // ДОДАЄМО МЕТОДИ
                math += MyMath.Div;
                math += algebra.Sub;
                math += algebra.Sum;
                Console.WriteLine(math?.Invoke(a, b));
                math -= algebra.Sum; // видаляємо методи
                foreach (MathMethod fun in math.GetInvocationList())
                {
                    Console.WriteLine(fun(a, b));
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }
Exemplo n.º 2
0
        static void Test3()
        {
            //10+5 = 15
            Console.WriteLine("Enter string");
            string str  = Console.ReadLine();
            char   Sign = ' ';

            try
            {
                if (str.Contains('+'))
                {
                    Sign = '+';
                }
                else if (str.Contains('-'))
                {
                    Sign = '-';
                }
                else if (str.Contains('*'))
                {
                    Sign = '*';
                }
                else if (str.Contains('/'))
                {
                    Sign = '/';
                }
                else
                {
                    Sign = ' ';
                }
                string[] numbers = str.Split(Sign, StringSplitOptions.RemoveEmptyEntries);
                double   av      = double.Parse(numbers[0]);
                double   bv      = double.Parse(numbers[1]);

                MathMethod mathod = null;

                switch (Sign)
                {
                case '+':
                    mathod = (a, b) => a + b;
                    break;

                case '-':
                    mathod = (a, b) => a - b;
                    break;

                case '*':
                    mathod = (a, b) => a * b;
                    break;

                case '/':
                    mathod = (a, b) => a / b;
                    break;
                }
                //Console.WriteLine(mathod(a, b));
                Console.WriteLine(mathod?.Invoke(av, bv));

                ShowMethod sm = d => Console.WriteLine($"d={d}");

                Show show = () => Console.WriteLine($"Hello");

                show += () => Console.WriteLine($"By");

                sm(159.36);
                show();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                throw;
            }
        }