Advertisement
Guest User

Untitled

a guest
Mar 26th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.04 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4.  
  5.  
  6. namespace March26
  7. {
  8.     class Steck<T>
  9.     {
  10.         //текущее значение
  11.         public T Value
  12.         {
  13.             set; get;
  14.         }
  15.         //ссылка на следующий элемент
  16.         public Steck<T> Next;
  17.         //вывод на консоль текущего значения
  18.         public void print()
  19.         {
  20.             Console.WriteLine(Value.ToString());
  21.         }
  22.         //конструктор
  23.         public Steck(T a)
  24.         {
  25.             Value = a;
  26.             Next = null;
  27.         }
  28.  
  29.         /*статический метод, который выводит весь стек, заданный параметром.
  30.         Перебирает все элементы и выводит значение поля Value,
  31.         пока очередной объект не равен null*/
  32.         public static void show(Steck<T> a)
  33.         {
  34.             if (a.Next != null)
  35.             {
  36.                 Console.WriteLine(a.Value);
  37.                 show(a.Next);
  38.             }
  39.         }
  40.     }
  41.     // обобщенный класс Массив элементов
  42.     class Array<T> where T : new()
  43.     {
  44.         //элементы массива
  45.         T[] array;
  46.         //количество элементов
  47.         public int N
  48.         {
  49.             set; get;
  50.         }
  51.         //конструктор
  52.         public Array(int n)
  53.         {
  54.             N = n;
  55.             array = new T[N];
  56.             for (int i = 0; i < N; i++)
  57.                 array[i] = new T();
  58.         }
  59.         //индексатор
  60.         public T this[int i]
  61.         {
  62.             set
  63.             {
  64.                 array[i] = value;
  65.             }
  66.             get
  67.             {
  68.                 return array[i];
  69.             }
  70.         }
  71.         //вывод элементов
  72.         override public string ToString()
  73.         {
  74.             string a = "{";
  75.             for (int i = 0; i < N; i++)
  76.                 a += array[i].ToString()+ "; ";
  77.  
  78.             return a+"}";
  79.         }
  80.         public void print()
  81.         {
  82.             Console.WriteLine(this.ToString());
  83.         }
  84.     }
  85.     //обобщенный класс Product
  86.     public enum categoris { продукты = 1, одежда, бытовая_химия };
  87.     class Product<T> where T : new()
  88.     {
  89.         private categoris category;
  90.         private double price;
  91.         private string name;
  92.         private T calorie = default(T); //калорийность
  93.         public Product()
  94.         {
  95.             category = categoris.продукты;
  96.             price = 0;
  97.             name = "";
  98.             calorie = new T();//вызов констрктора без параметров
  99.         }
  100.         public Product(categoris category, double price, string name, T calorie)
  101.         {
  102.             this.category = category;
  103.             this.price = price;
  104.             this.name = name;
  105.             this.calorie = calorie;
  106.         }
  107.         public void show()
  108.         {
  109.             Console.WriteLine($"Продукт {name}");
  110.             Console.WriteLine($"Категория {category}");
  111.             Console.WriteLine($"Цена {price}");
  112.             Console.WriteLine($"Колории {calorie.ToString()}");
  113.         }
  114.  
  115.         public void set(T newcalorie)
  116.         {
  117.             this.calorie = newcalorie;
  118.         }
  119.     }
  120.     class Program
  121.     {
  122.         public static void Main(string[] args)
  123.         { //формируем стек A
  124.             Steck<int> A = new Steck<int>(7);
  125.             A.Next = new Steck<int>(5);
  126.             A.Next.Next = new Steck<int>(3);
  127.             //вывод стека
  128.             Steck<int>.show(A);
  129.             //формируем массив точек
  130.             Array<Point> Arr = new Array<Point>(3);
  131.             Arr[1] = new Point(4, -2);
  132.             //выводим
  133.             Arr.print();
  134.             //формируем массив строк
  135.             Array<StringBuilder> Ars = new Array<StringBuilder>(6);
  136.             Ars[0] = new StringBuilder("abra");
  137.             Ars[1] = new StringBuilder("ca");
  138.             Ars[2] = new StringBuilder("dabra");
  139.  
  140.             // выводим массив строк, используя ToString()
  141.             Console.WriteLine(Ars.ToString());
  142.             //демонстрируем работу метода Eqauls
  143.             Console.WriteLine((Ars[0].Equals(Ars[0])));
  144.             Console.WriteLine((Ars[0].Equals(Ars[1])));
  145.  
  146.             //создание объекта класса Product<T> и демонстрация работы методов
  147.             Product<int> product = new Product<int>();
  148.             product.show();
  149.             Product<float> prod = new Product<float>(categoris.продукты, 200, "tvorog", 300);
  150.             prod.show();
  151.             prod.set(450);
  152.             prod.show();
  153.             Console.Write("Press any key to continue . . . ");
  154.             Console.ReadKey(true);
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement