Ejemplo n.º 1
0
        public static void Demo()
        {
            //(1) мы можем написать метод
            //  static int Square(int x) { return x * x; }
            //  и передать его делегату
            //  Sq d =  Square;   // или Sq d = new Sq(Square);
            //  тогда 3*3 это d(3)

            //(2) можно избежать построения функции,
            //  воспользовавшись анонимным делегатом
            Sq sqDelegat1 = delegate(int x) { return(x * x); };

            Console.WriteLine(sqDelegat1(3));

            //(3) можно использовать лямбда выражения
            Sq sqDelegat2 = x => (x * x);

            Console.WriteLine(sqDelegat2(3));

            // Аналогичные записи:
            // Sq sqDelegat2 = (int)x => (x * x);
            // Sq sqDelegat2 = x => (return x * x);

            // !!! явные типы в списке параметров лямбда выражений
            // необходимы, если делегат имеет out-, ref- параметры.

            // Для ограниченного числа параметров существуют встроенные
            // делегаты Func<>, так что можно не вводить свой делегат и
            // написать так:
            Func <double, double> res = (x) => (x * x);  // Func<in T,out TResult>(T arg)

            Console.WriteLine(res(3));
            //---------------------------------------------------------
            // Вот еще некоторые примеры лямбда-выражений
            // находится ли val между low и hight
            Func <int, int, int, bool> sam2 = (low, hight, val) => val >= low && val <= hight;

            Console.WriteLine(sam2(10, 30, 15));

            // лямбда выражения могут быть также блочными
            Func <int, int> factorial = (n) =>
            {
                int r = 1;
                for (int i = 1; i <= n; i++)
                {
                    r = i * r;
                }
                return(r);
            };

            Console.WriteLine("10! = {0}", factorial(10));

            // Помимо Func есть встроенные делегаты, которые не возвращают значений
            //Action()
            //Action<in T>(T obj)
            //...
            //Action<in T1,in T2,in T3,in T4,in T5,in T6,in T7,in T8,in T9,in T10,in T11,in T12,in T13,in T14,in T15,in T16>(T1 arg1, T2 arg2, T3 arg3, T4 arg4, T5 arg5, T6 arg6, T7 arg7, T8 arg8, T9 arg9, T10 arg10, T11 arg11, T12 arg12, T13 arg13, T14 arg14, T15 arg15, T16 arg16)
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Фасад
        /// </summary>
        public static void GoFacadePrepareSq()
        {
            Sq      sq      = new Sq();
            Colonel colonel = new Colonel("Пикалов А.В", "Командир роты");

            Console.WriteLine("{0} {1} {2} Снаряжает свое подразделение:", colonel.post, colonel.rank, colonel.name);
            colonel.ToManage(sq);
            Console.ReadKey();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Information Expert
        /// </summary>
        public static void GoInformationExpertGetSolderCount()
        {
            Sq      sq      = new Sq();
            Colonel colonel = new Colonel("Пикалов А.В", "Командир роты");

            Console.WriteLine("{0} {1} {2} Снаряжает свое подразделение:", colonel.post, colonel.rank, colonel.name);
            Print(colonel.ToManage(sq));
            Console.WriteLine();
            colonel.GetSqSolderCount(sq);
            Console.ReadKey();
        }
Ejemplo n.º 4
0
        public void SquaredIntArrayTest()
        {
            int[] x = new int[f.Length];
            int[] y = Sq.Squared(f);
            int   i = 0;

            foreach (int a in f)
            {
                x[i] = Sq.Squared(a);
                Assert.AreEqual(x[i], y[i]);
                i++;
            }
        }
Ejemplo n.º 5
0
        public void SquaredDoubleArrayTest()
        {
            double[] x = new double[e.Length];
            double[] y = Sq.Squared(e);
            int      i = 0;

            foreach (double a in e)
            {
                x[i] = Sq.Squared(a);
                Assert.AreEqual(x[i], y[i]);
                i++;
            }
        }
Ejemplo n.º 6
0
        public static double Variance(dynamic values)
        {
            double mean = StatMean.Mean(values);

            double[] squaredDeviation = new double[Helpers.Array.Length(values)];;
            int      i = 0;

            foreach (int a in values)
            {
                squaredDeviation[i] = Sq.Squared(Subtraction.Difference(a, mean));
                i++;
            }
            double variance = StatMean.Mean(squaredDeviation);

            return(variance);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Combine queue.
        /// </summary>
        public override void Combine()
        {
            if (Sq.Length < 2)
            {
                return;
            }

            char[] items = Sq.TwoLatestItems;

            var t = Qc.GetCombine(items[0], items[1]);

            if (t != 0)
            {
                Sq.RemoveElement();
                Sq.RemoveElement();
                Sq.AddElement(t);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Oppose queue.
        /// </summary>
        public override void Oppose()
        {
            if (Sq.Length < 2)
            {
                return;
            }

            var c = Sq.LastItem;

            foreach (char item in Sq.UniqueItems)
            {
                if (Qc.IsOpposed(item, c))
                {
                    Sq.Clear();

                    return;
                }
            }
        }
Ejemplo n.º 9
0
Archivo: 6.cs Proyecto: qifanyyy/CLCDSA
        private static MirrorsCase[] ReadFile(string filepath)
        {
            string[] lines     = File.ReadAllLines(filepath);
            int      lineIndex = 0;
            int      cases     = int.Parse(lines[lineIndex++]);
            var      result    = new MirrorsCase[cases];

            for (int caseNo = 1; caseNo <= cases; caseNo++)
            {
                var items = lines[lineIndex++].Split(' ').Select(s => int.Parse(s)).ToArray();
                int H, W, D;
                H = items[0];
                W = items[1];
                D = items[2];

                var squares = new Sq[H][];

                var meX = 0.0;
                var meY = 0.0;

                for (int y = 0; y < H; y++)
                {
                    squares[y] = lines[lineIndex++].Select(
                        (c, x) =>
                        c == '.' ?
                        Sq.Empty :
                        c == 'X' && ((meX = x + 0.5) + (meY = y + 0.5) <= int.MaxValue) ?
                        Sq.Me :
                        Sq.Mirror).ToArray();
                }

                result[caseNo - 1] = new MirrorsCase {
                    H = H, W = W, D = D, Squares = squares, MeX = meX, MeY = meY
                };
            }
            return(result);
        }
Ejemplo n.º 10
0
 public void SquaredDoubleTest()
 {
     Assert.AreEqual(57.76, Sq.Squared(c));
 }
Ejemplo n.º 11
0
 public void SquaredTest()
 {
     Assert.AreEqual(225, Sq.Squared(a));
 }
Ejemplo n.º 12
0
 public dynamic Squared(dynamic a)
 {
     result = Sq.Squared(a);
     return(result);
 }