Алгоритм работы программы ConsoleApplication2

Алгоритм работы программы 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();

}

}

}