using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
namespace ConsoleApplication5
{
[Serializable]
public class Flower : IComparable
{
public int kol;
public string name;
public Flower()
{
kol = 0;
name = "";
}
public Flower(int k, string n)
{
kol = k;
name = n;
//throw new DivideByZeroException();
}
public override string ToString()
{
return string.Format("{0} количество липестков = {1}", name, kol);
}
public int CompareTo(object obj)
{
Flower tmp = (Flower)obj;
if (this.kol > tmp.kol) return 1;
else if (kol < tmp.kol) return -1;
return 0;
}
public class SortByName : IComparer
{
public int Compare(object o1, object o2)
{
Flower t1 = (Flower)o1;
Flower t2 = (Flower)o2;
return string.Compare(t1.name, t2.name);
}
}
public static IComparer sortByName
{
get
{
return (IComparer)new SortByName();
}
}
}
[Serializable]
public class Home : Flower, ICloneable
{
public bool room;
public Home(int k, string n, bool r)
: base(k, n)
{
room = r;
}
public override string ToString()
{
return string.Format("{0}, количество липестков {1}, в комнате ли цветок {2} ", name, kol, room);
}
public object Clone()
{
return new Home(this.kol, this.name, this.room);
}
public bool Room
{
get { return room; }
set { room = value; }
}
}
[Serializable]
public class Street : Flower
{
public double height;
public Street(int k, string n, float h) : base(k, n) { height = h; }
public override string ToString()
{
return string.Format("{0}, количество липестков = {1} , высота растения {2} ", name, kol, height);
}
}
[Serializable]
public class Park : Flower,IComparable<Park>
{
public string type;
public Park(int k, string n, string t) : base(k, n) { type = t; }
public override string ToString()
{
return string.Format("{0}, количество липестков = {1} , тип растения {2} ", name, kol, type);
}
public int CompareTo(Park obj)
{
return string.Compare(this.type, obj.type);
}
}
[Serializable]
public class FlowerBox : IEnumerable, IEnumerator
{
int pos = -1;
Flower[] mas = new Flower[3];
public Flower this[int i]
{
get
{
if (i < 0 || i > mas.Length)
throw new IndexOutOfRangeException();
return mas[i];
}
set
{
if (i < 0 || i > mas.Length)
throw new IndexOutOfRangeException();
mas[i] = value;
}
}
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if (pos < mas.Length - 1)
{
pos++;
return true;
}
return false;
}
public object Current
{
get { return mas[pos]; }
}
public void Reset()
{
pos = -1;
}
}
/// <summary>
/// /////////////////////////////////////////////////////////////////////////
/// </summary>
///
class Node
{
public Node(object value)
{
Value = value;
}
public object Value { get; private set; }
public Node Prev { get; internal set; }
public Node Next { get; internal set; }
}
class MyList : IEnumerable
{
public Node First { get; private set; }
public Node Last { get; private set; }
public Node Add(object node)
{
var newNode = new Node(node);
if (First == null)
{
First = newNode;
Last = First;
}
else
{
Node prev = Last;
Last.Next = newNode;
Last = newNode;
Last.Prev = prev;
}
return newNode;
}
public IEnumerator GetEnumerator()
{
Node current = First;
while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
}
class Node<T>
{
public Node(T value)
{
Value = value;
}
public T Value { get; private set; }
public Node<T> Prev { get; internal set; }
public Node<T> Next { get; internal set; }
}
class MyLista<T> : IEnumerable<T>
{
public Node <T>First { get; private set; }
public Node <T>Last { get; private set; }
public Node <T>Add(T node)
{
var newNode = new Node<T>(node);
if(First == null)
{
First = newNode;
Last = First;
}
else
{
Node<T> prev = Last;
Last.Next = newNode;
Last = newNode;
Last.Prev = prev;
}
return newNode;
}
public IEnumerator <T> GetEnumerator()
{
Node<T> current = First;
while(current != null)
{
yield return current.Value;
current = current.Next;
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
/// <summary>
/// ////////////////////////////////////////////////////
/// </summary>
class MyObj<T,K,L>
{
T obj1;
K obj2;
L obj3;
public MyObj(T obj1, K obj2, L obj3)
{
this.obj1 = obj1;
this.obj2 = obj2;
this.obj3 = obj3;
}
public void ObjectType()
{
Console.WriteLine("Тив объекта = " + typeof(T));
Console.WriteLine("Тив объекта = " + typeof(K));
Console.WriteLine("Тив объекта = " + typeof(L));
}
}
delegate string MyDelegate(string d);
delegate Test MyDelegateObj(Test d);
class Test
{
public string str;
public Test(string s)
{
str = s;
}
public static string replace(string s)
{
return s.Replace(" ","_");
}
public static string remove(string s)
{
return s.Replace(" ","");
}
public static string change(string s)
{
return s.Replace("a","b");
}
public static Test replaceObj(Test s)
{
s.str= s.str.Replace("A", "_");
return s;
}
public static Test removeObj(Test s)
{
s.str= s.str.Replace("B", "-");
return s;
}
public static Test changeObj(Test s)
{
s.str = s.str.Replace("a", "b");
return s;
}
}
/// <summary>
/// ///////////////////////////////////////////////////
/// </summary>
///
public delegate void CarHander(string s);
class Car
{
public static event CarHander KillAuto;
public int Speed, MaxSpeed;
public string Name;
public Car() { MaxSpeed = 100; }
public Car(string n, int mS, int s)
{
Name = n;
Speed = s;
MaxSpeed = mS;
}
public void AddSpeed (int delta)
{
Speed += delta;
if ((MaxSpeed - Speed) == 10)
Console.WriteLine("Осторожно!!!Скорость уже {0} MaxSpeed = {1}", Speed, MaxSpeed);
else if (Speed >= MaxSpeed)
{
KillAuto(Name);
}
else
{
Console.WriteLine("Speed = {0}", Speed);
}
}
public static void OnKillAuto(string s)
{
Console.WriteLine("Превышение скорости. Авария {0}",s);
}
}
class Program
{
//public static void OnCarEvent(string msg)
//{
// Console.Write("Message From Car -> {0}", msg);
//}
//public static void OnCarEvent2(string msg)
//{
// Console.WriteLine("->{0}", msg.ToUpper());
//}
public static void OnKillAuto(string s)
{
Console.WriteLine("Превышение скорости. Авария {0}", s);
}
static void Main()
{
Car ob = new Car();
Type t;
t = ob.GetType();
t = Type.GetType("Car");
t = typeof(Car);
Console.WriteLine(t);
Car obj = new Car("Audi", 200, 100);
Car.KillAuto += new CarHander(Car.OnKillAuto);
//obj.Registration(new Car.CarHander(OnCarEvent));
for (int i = 0; i < 10; i++)
obj.AddSpeed(10);
//Car c1 = new Car("Audi",100,200);
//c1.Registration(new Car.CarHander(OnCarEvent));
//c1.Registration(new Car.CarHander(OnCarEvent2));
//for(int i = 0; i<10; i++)
//{
// c1.AddSpeed(10);
//}
//MyDelegate D = new MyDelegate(Test.replace);
//Console.WriteLine(D("A B C D"));
//D = new MyDelegate(Test.remove);
//Console.WriteLine(D("A B C D"));
//D = new MyDelegate(Test.change);
//Console.WriteLine(D("Мама мыла раму"));
//MyDelegate D1 = new MyDelegate(Test.replace);
//MyDelegate D2 = new MyDelegate(Test.remove);
//MyDelegate D3 = D1 + D2;
//Console.WriteLine(D3("A B C"));
//Test obj = new Test("A B C");
//MyDelegateObj DD1 = new MyDelegateObj(Test.replaceObj);
//MyDelegateObj DD2 = new MyDelegateObj(Test.removeObj);
//MyDelegateObj DD3 = DD1 + DD2;
//Test t = DD3(obj);
//Console.WriteLine(t.str);
////MyObj<Flower> obj3 = new MyObj<Flower>(new Flower(35,"drh"));
//MyObj<int, string, bool> obj1 = new MyObj<int, string, bool>(25,"ABC",false);
////MyObj<string> obj2 = new MyObj<string>("ABC");
//obj1.ObjectType();
////obj2.ObjectType();
//FlowerBox mas = new FlowerBox();
//mas[0] = new Home(7, "Fikus", true);
//mas[1] = new Street(3, "Palma", 40);
//mas[2] = new Park(10, "Rose", "Парковая");
////foreach (Flower T in mas)
//// Console.WriteLine(T);
//Park[] mas2 = new Park[2] ;
////foreach (Flower O in mas)
////{
//// Console.WriteLine(O);
////}
//FileStream fs = File.Create("Text.txt");
//BinaryFormatter bf = new BinaryFormatter();
//bf.Serialize(fs, mas);
//fs.Close();
//Console.WriteLine();
//FileStream fr = File.OpenRead("Text.txt");
//BinaryFormatter br = new BinaryFormatter();
//mas = (FlowerBox)br.Deserialize(fr);
//fr.Close();
//for (int j = 0; j < 3; j++)
//{
// Console.WriteLine(mas[j]);
//}
//ArrayList list = new ArrayList();
//list.Add(25);
//list.Add("ABCD");
//list.Add(new Flower());
//foreach (object i in list)
// Console.WriteLine(i);
Console.ReadKey();
}
}
}