Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Text;
- namespace March26
- {
- class Steck<T>
- {
- //текущее значение
- public T Value
- {
- set; get;
- }
- //ссылка на следующий элемент
- public Steck<T> Next;
- //вывод на консоль текущего значения
- public void print()
- {
- Console.WriteLine(Value.ToString());
- }
- //конструктор
- public Steck(T a)
- {
- Value = a;
- Next = null;
- }
- /*статический метод, который выводит весь стек, заданный параметром.
- Перебирает все элементы и выводит значение поля Value,
- пока очередной объект не равен null*/
- public static void show(Steck<T> a)
- {
- if (a.Next != null)
- {
- Console.WriteLine(a.Value);
- show(a.Next);
- }
- }
- }
- // обобщенный класс Массив элементов
- class Array<T> where T : new()
- {
- //элементы массива
- T[] array;
- //количество элементов
- public int N
- {
- set; get;
- }
- //конструктор
- public Array(int n)
- {
- N = n;
- array = new T[N];
- for (int i = 0; i < N; i++)
- array[i] = new T();
- }
- //индексатор
- public T this[int i]
- {
- set
- {
- array[i] = value;
- }
- get
- {
- return array[i];
- }
- }
- //вывод элементов
- override public string ToString()
- {
- string a = "{";
- for (int i = 0; i < N; i++)
- a += array[i].ToString()+ "; ";
- return a+"}";
- }
- public void print()
- {
- Console.WriteLine(this.ToString());
- }
- }
- //обобщенный класс Product
- public enum categoris { продукты = 1, одежда, бытовая_химия };
- class Product<T> where T : new()
- {
- private categoris category;
- private double price;
- private string name;
- private T calorie = default(T); //калорийность
- public Product()
- {
- category = categoris.продукты;
- price = 0;
- name = "";
- calorie = new T();//вызов констрктора без параметров
- }
- public Product(categoris category, double price, string name, T calorie)
- {
- this.category = category;
- this.price = price;
- this.name = name;
- this.calorie = calorie;
- }
- public void show()
- {
- Console.WriteLine($"Продукт {name}");
- Console.WriteLine($"Категория {category}");
- Console.WriteLine($"Цена {price}");
- Console.WriteLine($"Колории {calorie.ToString()}");
- }
- public void set(T newcalorie)
- {
- this.calorie = newcalorie;
- }
- }
- class Program
- {
- public static void Main(string[] args)
- { //формируем стек A
- Steck<int> A = new Steck<int>(7);
- A.Next = new Steck<int>(5);
- A.Next.Next = new Steck<int>(3);
- //вывод стека
- Steck<int>.show(A);
- //формируем массив точек
- Array<Point> Arr = new Array<Point>(3);
- Arr[1] = new Point(4, -2);
- //выводим
- Arr.print();
- //формируем массив строк
- Array<StringBuilder> Ars = new Array<StringBuilder>(6);
- Ars[0] = new StringBuilder("abra");
- Ars[1] = new StringBuilder("ca");
- Ars[2] = new StringBuilder("dabra");
- // выводим массив строк, используя ToString()
- Console.WriteLine(Ars.ToString());
- //демонстрируем работу метода Eqauls
- Console.WriteLine((Ars[0].Equals(Ars[0])));
- Console.WriteLine((Ars[0].Equals(Ars[1])));
- //создание объекта класса Product<T> и демонстрация работы методов
- Product<int> product = new Product<int>();
- product.show();
- Product<float> prod = new Product<float>(categoris.продукты, 200, "tvorog", 300);
- prod.show();
- prod.set(450);
- prod.show();
- Console.Write("Press any key to continue . . . ");
- Console.ReadKey(true);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement