Exemplo n.º 1
0
        static void Main(String[] args)
        {
            Function f = new Function();

            Console.WriteLine("请输入要操作的两个浮点数:");
            double x = Convert.ToDouble(Console.ReadLine());
            double y = Convert.ToDouble(Console.ReadLine());

            Console.WriteLine("积为{0}", f.twoNumMultiply(x, y));
            Console.WriteLine("和为{0}", f.twoNumAdd(x, y));

            intDelegate addDelegate      = new intDelegate(f.twoNumAdd);
            intDelegate multiplyDelegate = new intDelegate(f.twoNumMultiply);

            int i = 0;

            while (i < 2)
            {
                Console.WriteLine("请输入要操作的两个整数:");
                int a = Convert.ToByte(Console.ReadLine());
                int b = Convert.ToByte(Console.ReadLine());
                if (a < 10 && b < 10)
                {
                    Console.WriteLine("两数都小于10,和为{0}", addDelegate(a, b));
                }
                else
                {
                    Console.WriteLine("两数不都小于10,积为{0}", multiplyDelegate(a, b));
                }
                i++;
            }
            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            intDelegate[] intDelegates = new intDelegate[]
            {
                intValueReturn,
                intValueReturn,
                intValueReturn,
                intValueReturn,
                intValueReturn
            };

            arithmeticMean am = delegate(intDelegate[] delegates)
            {
                if (delegates.Length > 0)
                {
                    int sum = 0;
                    for (int i = 0; i < delegates.Length; i++)
                    {
                        sum = sum + delegates[0]();
                    }
                    decimal result = sum / delegates.Length;
                    return(Math.Round(result, 2));
                }
                else
                {
                    return(0);
                }
            };

            decimal result = am(intDelegates);

            Console.WriteLine($"Результат выполнения программы: {result}");
        }
Exemplo n.º 3
0
        //Q4. Write a generic delegate which will point to method which will
        //return square , cube ,and factorial of a number.

        static void Main(string[] args)
        {
            intDelegate <int> square    = new intDelegate <int>(Square);
            intDelegate <int> cube      = new intDelegate <int>(Cube);
            intDelegate <int> factorial = new intDelegate <int>(Factorial);

            Console.WriteLine(square(5));
            Console.WriteLine(cube(5));
            Console.WriteLine(factorial(5));
        }
Exemplo n.º 4
0
 private void UpdateProgress(int value)
 {
     if (statusProgress.ProgressBar.InvokeRequired)
     {
         intDelegate id = new intDelegate(UpdateProgress);
         this.Invoke(id, new object[] { value });
     }
     else
     {
         statusProgress.ProgressBar.Value = value;
     }
     taskbar.SetProgressValue(value, 100);
 }
Exemplo n.º 5
0
        static void Main(string[] args)
        {
            // C# 3.0. A delegate can be initialized with
            // a lambda expression. The lambda also takes a string
            // as an input parameter (x). The type of x is inferred by the compiler.
            intDelegate  cardI    = (x) => { return(x); };
            dblDelegate  cardDbl  = (x) => { return(x * 2); };
            succDelegate cardSucc = (x) => { return(x + 1); };



            // Invoke the delegates.
            Console.WriteLine(cardI(4));
            Console.WriteLine(cardDbl(4));
            Console.WriteLine(cardSucc(4));
            Console.WriteLine(cardDbl(cardDbl(cardDbl(cardSucc(0)))));

            // Keep console window open in debug mode.
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
Exemplo n.º 6
0
        /* edit_distn -- returns the edit distance between two strings, or -1 on
         * failure */
        public static int edit_distn(byte[] from, int from_len, byte[] to, int to_len)
        {
#if (!TRN_SPEEDUP)
            int ins, del, ch;        /* local copies of edit costs */
#endif
            int row, col, index = 0; /* dynamic programming counters */
            int radix;               /* radix for modular indexing */
#if (TRN_SPEEDUP)
            int low;
#endif
            int[] buffer; /* pointer to storage for one row
                           *                     of the d.p. array */

            var store = new int[THRESHOLD / sizeof(int)];

            /* a small amount of static
             * storage, to be used when the
             * input strings are small enough */

            /* Handle trivial cases when one string is empty */

            if (from == null)
            {
                if (to == null)
                {
                    return(0);
                }
                else
                {
                    return(to_len * insert_cost);
                }
            }
            if (to == null)
            {
                return(from_len * delete_cost);
            }

            /* Initialize registers */

            radix = 2 * from_len + 3;

#if (TRN_SPEEDUP)
            var ins       = 1;
            var del       = 1;
            var ch        = 3;
            var swap_cost = 5;
#else
            ins = insert_cost;
            del = delete_cost;
            ch  = change_cost;
#endif

            /* Make   from   short enough to fit in the static storage, if it's at all possible */

            if (from_len > to_len && from_len > STRLENTHRESHOLD)
            {
                swap_int(ref from_len, ref to_len);
                swap_char(ref from, ref to);
#if (!TRN_SPEEDUP)
                swap_int(ref ins, ref del);
#endif
            } /* if from_len > to_len */

            /* Allocate the array storage (from the heap if necessary) */

            buffer = from_len <= STRLENTHRESHOLD ? store : new int[radix];

            /* Here's where the fun begins.  We will find the minimum edit distance
             *  using dynamic programming.  We only need to store two rows of the matrix
             *  at a time, since we always progress down the matrix.  For example,
             *  given the strings "one" and "two", and insert, delete and change costs
             *  equal to 1:
             *
             *      _  o  n  e
             *  _  0  1  2  3
             *  t  1  1  2  3
             *  w  2  2  2  3
             *  o  3  2  3  3
             *
             *  The dynamic programming recursion is defined as follows:
             *
             *  ar(x,0) := x * insert_cost
             *  ar(0,y) := y * delete_cost
             *  ar(x,y) := min(a(x - 1, y - 1) + (from[x] == to[y] ? 0 : change),
             *              a(x - 1, y) + insert_cost,
             *              a(x, y - 1) + delete_cost,
             *              a(x - 2, y - 2) + (from[x] == to[y-1] &&
             *                      from[x-1] == to[y] ? swap_cost :
             *                      infinity))
             *
             *  Since this only looks at most two rows and three columns back, we need
             *  only store the values for the two preceeding rows.  In this
             *  implementation, we do not explicitly store the zero column, so only 2 *
             *  from_len + 2   words are needed.  However, in the implementation of the
             *  swap_cost   check, the current matrix value is used as a buffer; we
             *  can't overwrite the earlier value until the   swap_cost   check has
             *  been performed.  So we use   2 * from_len + 3   elements in the buffer.
             */

            //#define ar(x,y,index) (((x) == 0) ? (y) * del : (((y) == 0) ? (x) * ins :
            //    \ buffer[mod(index)]))
            //#define NW(x,y)	  ar(x, y, index + from_len + 2)
            //#define N(x,y)	  ar(x, y, index + from_len + 3)
            //#define W(x,y)	  ar(x, y, index + radix - 1)
            //#define NNWW(x,y) ar(x, y, index + 1)
            //#define mod(x) ((x) % radix)

            intDelegate mod = delegate(int x) { return(x % radix); };

            arDelegate ar = delegate(int x, int y, int idx)
            {
                return(x == 0
                 ? y * del
                 : y == 0
                   ? x * ins
                   : buffer[mod(idx)]);
            };

            doubleIntDelegate NW = delegate(int x, int y) { return(ar(x, y, index + from_len + 2)); };

            doubleIntDelegate N = delegate(int x, int y) { return(ar(x, y, index + from_len + 3)); };

            doubleIntDelegate W = delegate(int x, int y) { return(ar(x, y, index + radix - 1)); };

            doubleIntDelegate NNWW = delegate(int x, int y) { return(ar(x, y, index + 1)); };

            index = 0;

#if (DEBUG_EDITDIST)
            Console.Write("      ");
            for (col = 0; col < from_len; col++)
            {
                Console.Write(" {0} ", from[col]);
            }
            Console.Write("\n   ");

            for (col = 0; col <= from_len; col++)
            {
                Console.Write("{0}", col * del);
            }
#endif

            /* Row 0 is handled implicitly; its value at a given column is   col*del.
             * The loop below computes the values for Row 1.  At this point we know the
             * strings are nonempty.  We also don't need to consider swap costs in row
             * 1.
             *
             * COMMENT:  the indicies   row and col   below point into the STRING, so
             * the corresponding MATRIX indicies are   row+1 and col+1.
             */

            buffer[index++] = min2(ins + del, from[0] == to[0] ? 0 : ch);
#if (TRN_SPEEDUP)
            low = buffer[mod(index + radix - 1)];
#endif

#if (DEBUG_EDITDIST)
            Console.Write("\n {0} {1} {2} ", to[0], ins, buffer[index - 1]);
#endif

            for (col = 1; col < from_len; col++)
            {
                buffer[index] = min3(
                    col * del + (from[col] == to[0] ? 0 : ch),
                    (col + 1) * del + ins,
                    buffer[index - 1] + del);
#if (TRN_SPEEDUP)
                if (buffer[index] < low)
                {
                    low = buffer[index];
                }
#endif
                index++;

#if (DEBUG_EDITDIST)
                Console.Write("{0} ", buffer[index - 1]);
#endif
            } /* for col = 1 */

#if (DEBUG_EDITDIST)
            Console.Write("\n {0} {1} ", to[1], 2 * ins);
#endif

            /* Now handle the rest of the matrix */

            for (row = 1; row < to_len; row++)
            {
                for (col = 0; col < from_len; col++)
                {
                    buffer[index] = min3(
                        NW(row, col) + (from[col] == to[row] ? 0 : ch),
                        N(row, col + 1) + ins,
                        W(row + 1, col) + del);

                    if (from[col] == to[row - 1] && col > 0 && from[col - 1] == to[row])
                    {
                        buffer[index] = min2(buffer[index], NNWW(row - 1, col - 1) + swap_cost);
                    }

#if (DEBUG_EDITDIST)
                    Console.Write("{0} ", buffer[index]);
#endif
#if (TRN_SPEEDUP)
                    if (buffer[index] < low || col == 0)
                    {
                        low = buffer[index];
                    }
#endif

                    index = mod(index + 1);
                } /* for col = 1 */

#if (DEBUG_EDITDIST)
                if (row < to_len - 1)
                {
                    Console.Write("\n {0} {1} ", to[row + 1], (row + 2) * ins);
                }
                else
                {
                    Console.Write("\n");
                }
#endif
#if (TRN_SPEEDUP)
                if (low > MIN_DIST)
                {
                    break;
                }
#endif
            } /* for row = 1 */

            row = buffer[mod(index + radix - 1)];

            return(row);
        } /* edit_distn */
Exemplo n.º 7
0
        static void Main(string[] args)
        {
            //anonymous method (pre-Lambda)
            //anonymous method that returns square value of an int
            intDelegate intInstance = delegate(int input)
            {
                return(input * input);
            };
            int value = 5;

            Console.WriteLine("Square value of 5: {0}", intInstance(value));

            Console.WriteLine("---------------------------");

            //anonymous method (pre-Lambda)
            //anonymous method that doesn't return any value but prints given value to console
            voidDelegate voidInstance = delegate(string text)
            {
                Console.WriteLine(text);
            };

            voidInstance("Given text");

            Console.WriteLine("---------------------------");

            //delagate with Lambda expresion that returns int
            intDelegate intLambda = (int input) => input * input;

            value = 6;
            Console.WriteLine("Square value of 5: {0}", intLambda(value));

            Console.WriteLine("---------------------------");

            //delegate with extended Lambda expresion that returns int
            intDelegate intExtLambda = (int input) =>
            {
                int buffer;
                buffer = input * input;
                buffer = (int)Math.PI * buffer;
                return(buffer);
            };

            Console.WriteLine("Processed value of given input: {0}", intExtLambda(value));

            Console.WriteLine("---------------------------");

            //delgate with Lambda expression that returns nothing
            voidDelegate voidLambda = (string text) => Console.WriteLine(text);

            voidLambda("Given text");

            Console.WriteLine("---------------------------");

            //delegate with extended Lambda expression that return nothing
            voidDelegate voidExtLambda = (string text) =>
            {
                Console.WriteLine("Given text: {0}", text);
                Console.WriteLine("Length: {0}", text.Length);
                Console.WriteLine("Capitalizad text: {0}", text.ToUpper());
            };

            voidExtLambda("input text_XXX");
        }
Exemplo n.º 8
0
Arquivo: Form1.cs Projeto: bvp/en2ki
 private void UpdateProgress(int value)
 {
     if (statusProgress.ProgressBar.InvokeRequired)
     {
         intDelegate id = new intDelegate(UpdateProgress);
         this.Invoke(id, new object[] { value });
     }
     else
     {
         statusProgress.ProgressBar.Value = value;
     }
     taskbar.SetProgressValue(value, 100);
 }