ConsoleApplication4
Console Application1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
delegate string MyDelegate(int arg);
class Program
{
public static string MyMethod(int arg)
{
return arg.ToString();
}
static void Main(string[] args)
{
MyDelegate dlg = new MyDelegate(MyMethod);
// ,
string returnValue = dlg(25);
Console.WriteLine(returnValue); //
}
}
}
Console Application2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
delegate void MesToPers(string s);
class Person
{
//
private string message;
// :
private string name;
private int id;
private double salary;
// :
public string Name
{
get { return (name); }
set { name = value; }
}
public double Salary
{
get { return (salary); }
set { salary = value; }
}
public int Id
{
get { return (id); }
set { id = value; }
}
//
public Person()
{
name = ""; id = 0; salary = 0.0;
}
public Person(string name)
{
this.name = name;
}
public Person(string name, int id, double salary)
{
this.name = name; this.id = id; this.salary = salary;
}
public Person(Person pers)
{
this.name = pers.name; this.id = pers.id;
this.salary = pers.salary;
}
// M
public void ToPerson(string mes)
{
this.message = mes;
Console.WriteLine("{0}, {1}", name, message);
}
}
class Program
{
static void Main(string[] args)
{
// Person
Person man1 = new Person("");
// personMessag ,
man1 Person
MesToPers personMessage = new MesToPers(man1.ToPerson);
// man1
personMessage(" !");
}
}
}
Console Application3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication2
{
delegate void MesSalary();
delegate void MesId();
class Person
{
//
private string message;
// :
private string name;
private int id;
private double salary;
// :
public string Name
{
get { return (name); }
set { name = value; }
}
public double Salary
{
get { return (salary); }
set { salary = value; }
}
public int Id
{
get { return (id); }
set { id = value; }
}
//
public Person()
{
name = ""; id = 0; salary = 0.0;
|
|
}
public Person(string name)
{
this.name = name;
}
public Person(string name, int id, double salary)
{
this.name = name; this.id = id;
this.salary = salary;
}
public Person(Person pers)
{
this.name = pers.name; this.id = pers.id;
this.salary = pers.salary;
}
// M
public void ToPerson(string mes)
{
this.message = mes;
Console.WriteLine("{0}, {1}", name, message);
}
public void PrintSalary()
{
Console.WriteLine(salary);
}
public void PrintID()
{
Console.WriteLine(id);
}
}
class Program
{
static void Main(string[] args)
{
Person man = new Person("",1,15000);
MesSalary s=new MesSalary(man.PrintSalary);
MesId i = new MesId(man.PrintID);
i();
s();
Console.ReadKey();
}
}
}
Console Application4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3._1
{
delegate void MesToPers(string mes);
class Combination
{
private static void policeman(string mes)
{
//
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
private static void ambulanceman(string mes)
{
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
private static void fireman(string mes)
{
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
public static MesToPers Policeman
{ get { return (new MesToPers(policeman)); } }
public static MesToPers Fireman
{ get { return (new MesToPers(fireman)); } }
public static MesToPers Ambulanceman
{ get { return (new MesToPers(ambulanceman)); } }
}
class Program
{
static void Main(string[] args)
{
MesToPers Comb;
Comb = (MesToPers)Delegate.Combine
(Combination.Ambulanceman, Combination.Policeman);
Comb = (MesToPers)Delegate.Combine
(Comb, Combination.Fireman);
Comb("!");
}
}
}
Console Application5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _3._1
{
delegate void MesToPers(string mes);
class Combination
{
private static void policeman(string mes)
{
//
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
private static void ambulanceman(string mes)
{
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
private static void fireman(string mes)
{
if (mes == "!") Console.WriteLine(mes + "
!");
else Console.WriteLine(mes + " !");
}
public static MesToPers Policeman
{ get { return (new MesToPers(policeman)); } }
public static MesToPers Fireman
{ get { return (new MesToPers(fireman)); } }
public static MesToPers Ambulanceman
{ get { return (new MesToPers(ambulanceman)); } }
|
|
public static void BadService(string mes)
{
int i = 7, j = 5, k = 0;
Console.WriteLine("Bad Service: Zero Divide");
j = i / k;
}
}
class Program
{
static void Main(string[] args)
{
MesToPers Comb;
Comb = (MesToPers)Delegate.Combine
(Combination.Ambulanceman, Combination.Policeman);
Comb = (MesToPers)Delegate.Combine
(Comb, Combination.Fireman);
Comb("!");
Console.ReadKey();
Comb = (MesToPers)Delegate.Remove(Comb,
Combination.Policeman);
// :
Comb = (MesToPers)Delegate.Remove(Comb,
Combination.Policeman);
Comb(" 30 !");
Comb = (MesToPers)Delegate.Remove(Comb,
Combination.Ambulanceman);
Comb(" !");
Comb = (MesToPers)Delegate.Remove(Comb,
Combination.Fireman);
//Comb(" !"); // Comb
Console.ReadKey();
Comb = Combination.Ambulanceman;
Console.WriteLine(Comb.Method.Name);
Comb += Combination.Fireman;
Comb += Combination.Policeman;
Comb(" !");
Comb -= Combination.Ambulanceman;
Comb -= Combination.Fireman;
Comb(" !");
Console.ReadKey();
//---------------------------------
Comb=null;
Comb = (MesToPers)Delegate.Combine
(Combination.Ambulanceman,
Combination.Policeman);
Comb = (MesToPers)Delegate.Combine
(Comb, new MesToPers(Combination.BadService));
Comb = (MesToPers)Delegate.Combine
(Comb, Combination.Fireman);
foreach (MesToPers currentJob in
Comb.GetInvocationList())
{
try { currentJob("!"); }
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.WriteLine(currentJob.Method.Name);
}
}
}
}
}
Console Application6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _4
{
public class HighOrderIntegral
{
//
public delegate double SubIntegralFun(double x);
public double EvalIntegral(double a, double b, double eps,
SubIntegralFun fun)
{
int n = 4;
double I0 = 0, I1 = I(a, b, n, fun);
for (n = 8; n < Math.Pow(2.0, 15.0); n *= 2)
{
I0 = I1; I1 = I(a, b, n, fun);
if (Math.Abs(I1 - I0) < eps) break;
}
if (Math.Abs(I1 - I0) < eps)
Console.WriteLine(" ! "
+" eps = {0}, ={1}, n= {2}",
eps, Math.Abs(I1 - I0), n);
else
Console.WriteLine("
! " +" eps = {0}, ={1},
n= {2}", eps, Math.Abs(I1 - I0), n);
return (I1);
}
private double I(double a, double b, int n,
SubIntegralFun fun)
{
//
double x = a, sum = fun(x) / 2, dx = (b - a) / n;
for (int i = 2; i <= n; i++)
{ x += dx; sum += fun(x); }
x = b; sum += fun(x) / 2;
return (sum * dx);
}
}
class Functions
{
//
static int k=1;
static int b=0;
static int a = 0;
static int q = 0;
static int c = 0;
static public void SetParametrs()
{
Console.WriteLine("f1(x)=k*x+b");
Console.WriteLine(" k");
k = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" b");
b = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("f1(x)="+k+"x+"+b);
Console.WriteLine("f2(x)=a*x^2+b*x+c");
Console.WriteLine(" a");
a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" b");
q = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" c");
c = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("f2(x)="+a+"*x^2+"+q+"*x+"+c);
}
static public double fun1(double x)
{
return (double)(k * x + b);
}
static public double fun2(double x)
{
|
|
return (double)(a * x * x + q * x + c);
}
}
class Program
{
static void Main(string[] args)
{
double val = 0.0;
Functions.SetParametrs();
HighOrderIntegral f = new HighOrderIntegral();
HighOrderIntegral.SubIntegralFun f1 =
new HighOrderIntegral.SubIntegralFun(Functions.fun1);
val = f.EvalIntegral(2, 3, 0.1e-5, f1);
Console.WriteLine("myintegral1 = {0}", val);
HighOrderIntegral.SubIntegralFun f2 =
new HighOrderIntegral.SubIntegralFun(Functions.fun2);
val = f.EvalIntegral(2, 3, 0.1e-5, f2);
Console.WriteLine("myintegral2 = {0}", val);
Console.ReadKey();
}
}
}