Загрузка данных


Задание 1
using System;

namespace _1
{
    internal class Program
    {
        class Utils
        {
            public static int Greater(int a, int b)
            {
                if (a > b)
                    return a;
                else
                    return b;
            }
            public static void Swap(ref int a, ref int b)
            {
                int temp = a;
                a = b;
                b = temp;
            }
            public static bool Factorial(int n, out int answer)
            {
                int k;
                int f = 1;
                bool ok = true;
                try
                {
                    checked
                    {
                        for (k = 2; k <= n; ++k)
                        {
                            f = f * k;
                        }
                    }
                }
                catch (Exception)
                {
                    f = 0;
                    ok = false;
                }
                answer = f;
                return ok;
            }
        }
        static void Main(string[] args)
        {
            int x;
            int y;
            Console.WriteLine("Введите 1 число: ");
            x = int.Parse(Console.ReadLine());
            Console.WriteLine("Введите 2 число: ");
            y = int.Parse(Console.ReadLine());
            int g = Utils.Greater(x, y);
            Console.WriteLine("Большим из чисел {0} и {1} является {2} ", x, y, g);
            Console.WriteLine("До swap: \t" + x + " " + y);
            Utils.Swap(ref x, ref y);
            Console.WriteLine("После swap: \t" + x + " " + y);
            int f;
            bool ok;
            Console.WriteLine("Number for factorial:");
            x = int.Parse(Console.ReadLine());
            ok = Utils.Factorial(x, out f);
            if (ok)
            {
                Console.WriteLine("Factorial(" + x + ") = " + f);
            }
            else
            {
                Console.WriteLine("Cannot compute this factorial");
            }
        }
    }
}



Задание 2
using System;
using System.Collections;

namespace _2
{
    internal class Program
    {
        class Car
        {
            private string name = "noname";
            public Car() { }
            public Car(string name)
            {
                this.name = name;
            }
            public string Name
            {
                get { return name; }
            }
        }
        class Cars : IEnumerable, IEnumerator
        {
            private Car[] cars;
            private int current = -1;
            public Cars()
            {
                cars = new Car[4];
                cars[0] = new Car("alpha"); 
                cars[1] = new Car("porshe"); 
                cars[2] = new Car("nissan"); 
                cars[3] = new Car("ГАЗ-13");
            }
            public void Reset()
            {
                current = -1;
            }
            public bool MoveNext()
            {
                if (current < cars.Length - 1)
                {
                    current++;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public object Current
            {
                get
                {
                    return cars[current];
                }
            }
            public IEnumerator GetEnumerator()
            {
                return (IEnumerator)this;
                /*/
                for (int i = 0; i < cars.Length; i++) {
                // многократный возврат
                yield return cars[i];
                }
                /*/
            }
        }
        static void Main(string[] args)
        {
            Cars carlot = new Cars(); 
            foreach (Car c in carlot)
            {
                Console.WriteLine(c.Name);
            }
            carlot.Reset(); 
            carlot.MoveNext();
            Console.WriteLine(((Car)carlot.Current).Name);
        }
    }
}



Задание 3
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3
{
    class Car
    {
        private string name = "noname";
        public Car() { }
        public Car(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get { return name; }
        }
    }
    class Cars : IEnumerable
    {
        private Car[] cars;
        public Cars()
        {
            cars = new Car[4];
            cars[0] = new Car("alpha");
            cars[1] = new Car("porshe");
            cars[2] = new Car("nissan");
            cars[3] = new Car("ГАЗ-13");
        }
        public IEnumerator GetEnumerator()
        {
            return new CarsEnumerator(this);
        }
        public class CarsEnumerator : IEnumerator
        {
            private int current = -1;
            private Cars c;
            public CarsEnumerator(Cars c)
            {
                this.c = c;
            }
            public void Reset()
            {
                current = -1;
            }
            public bool MoveNext()
            {
                if (current < c.cars.Length - 1)
                {
                    current++;
                    return true;
                }
                else
                {
                    return false;
                }
            }
            public object Current
            {
                get { return c.cars[current]; }
            }
        }
        

    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Cars carlot = new Cars(); 
            foreach (object o in carlot)
            {
                Console.WriteLine(((Car)o).Name);
            }
        }
    }
}



Задание 4
using System;

namespace _4
{
    internal class Program
    {
        class Point
        {
            public int x, y; public Point() { }
            public Point(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
            public override string ToString()
            {
                return "(" + x.ToString() + "," + y.ToString() + ")";
            }
        }
        class Point2 : ICloneable
        {
            public int x, y;
            public Point2() { }
            public Point2(int x, int y)
            {
                this.x = x;
                this.y = y;
            }
            public override string ToString()
            {
                return "(" + x.ToString() + "," + y.ToString() + ")";
            }
            public object Clone()
            {
                return new Point2(this.x, this.y);
            }
        }
        static void Main(string[] args)
        {
            Point p1 = new Point(2, 3);
            Console.WriteLine("1: " + p1.ToString());
            Point p2 = p1; p2.x = 3;
            Console.WriteLine("2: " + p2.ToString()); Console.WriteLine("1: " + p1.ToString());
            Point2 p3 = new Point2(6, 6); Point2 p4 = (Point2)p3.Clone(); p4.x = 3;
            Console.WriteLine("4: " + p4.ToString()); Console.WriteLine("3: " + p3.ToString());
        }
    }
}



Задание 5
using System;

namespace _5
{
    internal class Program
    {
        class Car : IComparable
        {
            private string name = "noname"; public Car() { }
            public Car(string name)
            {
                this.name = name;
            }
            public string Name
            {
                get { return name; }
            }
            int IComparable.CompareTo(object o)
            {
                return name.CompareTo(((Car)o).name);
            }
        }
        static void Main(string[] args)
        {
            Car[] cars = new Car[4]; cars[0] = new Car("3");
            cars[1] = new Car("2");
            cars[2] = new Car("1");
            cars[3] = new Car("4"); foreach (object c in cars)
            {
                Console.WriteLine(((Car)c).Name);
            }
            Array.Sort(cars); Console.WriteLine("Sorted:"); foreach (object c in cars)
            {
                Console.WriteLine(((Car)c).Name);
            }
        }
    }
}



Задание 6
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _6
{
    class Car
    {
        private string name = "noname"; public Car() { }
        public Car(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get { return name; }
        }
        public static System.Collections.IComparer SortByName
        {
            get
            {
                return (System.Collections.IComparer)new SortByName();
            }
        }
    }
    class SortByName : System.Collections.IComparer
    {
        public SortByName() { }
        int System.Collections.IComparer.Compare(object a, object b)
        {
            return String.Compare(((Car)a).Name, ((Car)b).Name);
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Car[] cars = new Car[4]; cars[0] = new Car("4");
            cars[1] = new Car("1");
            cars[2] = new Car("3");
            cars[3] = new Car("2");
            foreach (Car c in cars)
            {
                Console.WriteLine(c.Name);
            }
            Array.Sort(cars, new SortByName()); Console.WriteLine("Sorted:"); foreach (Car c in cars)
            {
                Console.WriteLine(c.Name);
            }
            Array.Sort(cars, Car.SortByName);
        }
    }
}



Задание 7
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _7
{
    class Car
    {
        private string name = "noname"; public Car() { }
        public Car(string name)
        {
            this.name = name;
        }
        public string Name
        {
            get { return name; }
        }
    }
    class Cars : IEnumerable
    {
        private ArrayList cars; public Cars()
        {
            cars = new ArrayList();
        }
        public void Add(Car c)
        {
            cars.Add(c);
        }
        public int Count
        {
            get
            {
                return cars.Count;
            }
        }
        public void Clear()
        {
            cars.Clear();
        }
        public void RemoveAt(int index)
        {
            cars.RemoveAt(index);
        }
        public bool Contains(Car c)
        {
            return cars.Contains(c);
        }
        public IEnumerator GetEnumerator()
        {
            return cars.GetEnumerator();
        }
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            Cars cars = new Cars(); cars.Add(new Car("Alpha"));
            cars.Add(new Car("RR"));
            cars.Add(new Car("117")); cars.Add(new Car("Nissan"));
            Console.WriteLine("We have {0} cars:", cars.Count); foreach (Car c in cars)
            {
                Console.WriteLine(c.Name);
            }
            cars.RemoveAt(3);
            Console.WriteLine("We have {0} cars:", cars.Count); foreach (Car c in cars)
            {
                Console.WriteLine(c.Name);
            }
            Car a = new Car("Audi100"); cars.Add(a);
            if (cars.Contains(a))
            {
                Console.WriteLine(a.Name + " is present");
            }
            cars.Clear();
            Console.WriteLine("We have {0} cars.", cars.Count);
            ArrayList ar = new ArrayList(); ar.Add(cars);
            ar.Add(new Car("abc"));
            ar.Add("Hi"); ar.Add(33);
            foreach (object o in ar)
            {
                Console.WriteLine(o.ToString());
            }
        }
    }
}