zoukankan      html  css  js  c++  java
  • 4.1 delegate

    delegate  ---packed up function 

    public delegate double myDelegate (double x);

    my delegate d2 = new myDelegate (obj.method);

    using System;
     
    delegate double Fun( double x );
     
     
     
    public class DelegateIntegral
    {
        public static void Main()
        {
            Fun fun = new Fun(Math.Sin);
            double d = Integral( fun, 0, Math.PI/2, 1e-4 ); 
            Console.WriteLine( d );
     
            Fun fun2 = new Fun( Linear );
            double d2 = Integral( fun2, 0, 2, 1e-3 );
            Console.WriteLine( d2 );
     
            Rnd rnd = new Rnd();
            double d3 = Integral( new Fun(rnd.Num), 0, 1, 0.01 ); 
            Console.WriteLine( d3 );
        }
         
        static double Linear( double a )
        {
            return a*2+1;
        }
         
        class Rnd 
        {
            Random r = new Random();
            public double Num( double  x )
            {
                return r.NextDouble();
            }
        }
     
        static double Integral(Fun f, double a, double b, double eps)// 积分计算
        {
            int n,k;
            double fa,fb,h,t1,p,s,x,t=0;
     
            fa=f(a); 
            fb=f(b);
     
            // 迭代初值
            n=1; 
            h=b-a;
            t1=h*(fa+fb)/2.0;
            p=double.MaxValue;
     
            // 迭代计算
            while (p>=eps)
            { 
                s=0.0;
                for (k=0;k<=n-1;k++)
                { 
                    x=a+(k+0.5)*h;
                    s=s+f(x);
                }
     
                t=(t1+h*s)/2.0;
                p=Math.Abs(t1-t);
                t1=t; 
                n=n+n; 
                h=h/2.0;
            }
            return t;
        }
     
    }
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
     
    namespace TestWin
    {
        /// <summary>
        /// Summary description for Form1.
        /// </summary>
        public class Form1 : System.Windows.Forms.Form
        {
            private System.Windows.Forms.Button button1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;
     
            public Form1()
            {
                //
                // Required for Windows Form Designer support
                //
                InitializeComponent();
     
                //
                // TODO: Add any constructor code after InitializeComponent call
                //
            }
     
            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                if( disposing )
                {
                    if (components != null) 
                    {
                        components.Dispose();
                    }
                }
                base.Dispose( disposing );
            }
     
            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.button1 = new System.Windows.Forms.Button();
                this.SuspendLayout();
                // 
                // button1
                // 
                this.button1.Location = new System.Drawing.Point(216, 24);
                this.button1.Name = "button1";
                this.button1.Size = new System.Drawing.Size(64, 24);
                this.button1.TabIndex = 0;
                this.button1.Text = "button1";
                // 
                // Form1
                // 
                this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
                this.ClientSize = new System.Drawing.Size(292, 273);
                this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                                                              this.button1});
                this.Name = "Form1";
                this.Text = "Form1";
                this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
                this.ResumeLayout(false);
     
            }
            #endregion
     
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main() 
            {
                Application.Run(new Form1());
            }
     
            private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
            {
                Graphics g = e.Graphics;
                Pen pen = Pens.Blue;
     
                Fun [] funs = {
                                  new Fun( this.Square ),
                                  new Fun( Form1.XPlus ),
                                  new Fun( Math.Cos ),
                                  new Fun( Math.Sqrt )
                              };
                foreach( Fun fun in funs )
                {
                    PlotFun( fun, g, pen );
                }
            }
     
            delegate double Fun( double x );
     
            void PlotFun( Fun fun, Graphics g, Pen pen )
            {
                    for( double x=0; x<10; x+=0.1)
                    {
                        double y = fun(x);
                        Point point = new Point( (int)(x*20), (int)(200-y*30) );
                        g.DrawLine( pen, point, new Point( point.X+2, point.Y+2) );
                        Console.WriteLine( " " + x + " " + y );
                    }
            }
     
            double Square( double x )
            {
                return Math.Sqrt( Math.Sqrt( x) );
            }
            static double XPlus( double x )
            {
                return Math.Sin(x)+ Math.Cos(x*5)/5;
            }
     
        }
    }
    using System;
    delegate void D(int x);
    class C
    {
        public static void M1(int i) 
        {
            Console.WriteLine("C.M1: " + i);
        }
        public static void M2(int i) 
        {
            Console.WriteLine("C.M2: " + i);
        }
        public void M3(int i) 
        {
            Console.WriteLine("C.M3: " + i);
        }
    }
    class Test
    {
        static void Main() 
        { 
            D cd1 = new D( C.M1);
            cd1(-1);        // call M1
            D cd2 = null;
            cd2 += new D( C.M2);
            cd2(-2);        // call M2
            D cd3 = cd1 + cd2;
            cd3(10);        // call M1 then M2
            cd3 += cd1;
            cd3(20);        // call M1, M2, then M1
            C c = new C();
            D cd4 = new D(c.M3);
            cd3 += cd4;
            cd3(30);        // call M1, M2, M1, then M3
            cd3 -= cd1;       // remove last M1
            cd3(40);        // call M1, M2, then M3
            cd3 -= cd4;
            cd3(50);        // call M1 then M2
            cd3 -= cd2;
            cd3(60);        // call M1
            cd3 -= cd2;       // impossible removal is benign
            cd3(60);        // call M1
            cd3 -= cd1;       // invocation list is empty
            Console.WriteLine( cd3 == null );
            //      cd3(70);    // System.NullReferenceException thrown
            cd3 -= cd1;       // impossible removal
            Console.WriteLine( cd3 == null );
     
        }
    }
  • 相关阅读:
    c:forTokens标签循环输出
    jsp转long类型为date,并且格式化
    spring中@Param和mybatis中@Param使用区别(暂时还没接触)
    734. Sentence Similarity 有字典数组的相似句子
    246. Strobogrammatic Number 上下对称的数字
    720. Longest Word in Dictionary 能连续拼接出来的最长单词
    599. Minimum Index Sum of Two Lists两个餐厅列表的索引和最小
    594. Longest Harmonious Subsequence强制差距为1的最长连续
    645. Set Mismatch挑出不匹配的元素和应该真正存在的元素
    409. Longest Palindrome 最长对称串
  • 原文地址:https://www.cnblogs.com/CandiceW/p/4234046.html
Copyright © 2011-2022 走看看