Advertisement
Guest User

Untitled

a guest
Dec 11th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace OOP_8
  8. {
  9. public class QuestionException : Exception
  10. {
  11. public QuestionException(string message)
  12. : base(message)
  13. { }
  14. }
  15.  
  16. public sealed class Question
  17. {
  18. public string Text
  19. {
  20. get { return text; }
  21. set
  22. {
  23. if (String.IsNullOrEmpty(value))
  24. {
  25. throw new QuestionException("Передана пустая строка! Вопрос не задан.");
  26. }
  27. else
  28. {
  29. text = value;
  30. }
  31. }
  32. }
  33.  
  34. private string text;
  35.  
  36. public override bool Equals(object obj)
  37. {
  38. return base.Equals(obj);
  39. }
  40.  
  41. public override int GetHashCode()
  42. {
  43. return base.GetHashCode();
  44. }
  45.  
  46. public override string ToString()
  47. {
  48. return Text;
  49. }
  50. }
  51. interface IDo<T>
  52. {
  53. void add(T element);
  54. void delete(T element);
  55. void show();
  56. }
  57. public class Program
  58. {
  59. public class Owner
  60. {
  61. public int Id;
  62. public string Name;
  63. public string Organization;
  64. public Owner()
  65. {
  66. Id = 0;
  67. Name = null;
  68. Organization = null;
  69. }
  70. public Owner(int id, string name, string organization)
  71. {
  72. Id = id;
  73. Name = name;
  74. Organization = organization;
  75. }
  76. }
  77. public class CollectionType<T> : IDo<T> where T : new()//
  78. {
  79. public List<T> Set { get; set; }
  80. public int k;
  81. public Owner OwnerOfSet;
  82. public class Date
  83. {
  84.  
  85. public int Year;
  86. public int Month;
  87. public int Day;
  88. public Date()
  89. {
  90. Year = 0;
  91. Month = 0;
  92. Day = 0;
  93. }
  94. public Date(int year, int month, int day)
  95. {
  96. Year = year;
  97. Month = month;
  98. Day = day;
  99. }
  100. }
  101.  
  102. public void add(T element)
  103. {
  104. Set.Add(element);
  105. }
  106. public void delete(T element)
  107. {
  108. Set.Remove(element);
  109. }
  110. public void show()
  111. {
  112. Console.WriteLine("Id = " + OwnerOfSet.Id + ", Name = " + OwnerOfSet.Name + ", Organization = " + OwnerOfSet.Organization);
  113. Console.WriteLine("Элементы:");
  114. foreach (var t in Set)
  115. Console.WriteLine(t);
  116. }
  117.  
  118. public CollectionType()
  119. {
  120. Set = new List<T>();
  121. OwnerOfSet = new Owner();
  122. }
  123. public CollectionType(int number, string Name, string organization, params T[] items)
  124. {
  125. Set = new List<T>();
  126. Set.AddRange(items);
  127. OwnerOfSet = new Owner(number, Name, organization);
  128. }
  129. public static List<T> operator +(CollectionType<T> setarr1, CollectionType<T> setarr2) //перегрузка +, чтобы объёдинял множества
  130. {
  131. CollectionType<T> setarr3 = new CollectionType<T>();
  132. foreach (var x in setarr1.Set)
  133. {
  134. setarr3.Set.Add(x);
  135. }
  136. foreach (var x in setarr2.Set)
  137. {
  138. if (!setarr3.Set.Contains(x))
  139. {
  140. setarr3.Set.Add(x);
  141. }
  142. }
  143. return setarr3.Set;
  144. }
  145.  
  146. public static bool operator <=(CollectionType<T> sa1, CollectionType<T> sa2) //перегрузка <= and >=, чтобы сравнивали мощность
  147. {
  148. if (sa1.Set.Capacity <= sa2.Set.Capacity)
  149. return true;
  150. else
  151. return false;
  152. }
  153. public static bool operator >=(CollectionType<T> sa1, CollectionType<T> sa2)
  154. {
  155. if (sa1.Set.Capacity >= sa2.Set.Capacity)
  156. return true;
  157. else
  158. return false;
  159. }
  160. public static T operator %(CollectionType<T> sa, int num)
  161. {
  162. return sa.Set[num];
  163. }
  164. public static implicit operator int(CollectionType<T> sa)
  165. {
  166. return sa.Set.Count;
  167. }
  168. public override string ToString()
  169. {
  170. return ("Id = " + this.OwnerOfSet.Id + ", Name = " + this.OwnerOfSet.Name + ", Organization = " + this.OwnerOfSet.Organization);
  171. }
  172. }
  173.  
  174. static void Main(string[] args)
  175. {
  176.  
  177. try
  178. {
  179. CollectionType<int> sa1 = new CollectionType<int>(1000001, "Oleg", "NPD", 1, 2, 3);
  180. CollectionType<int> sa2 = new CollectionType<int>(1111100, "Kate", "Kolesnik", 3, 4, 5);
  181. CollectionType<int> sa3 = new CollectionType<int>();
  182. CollectionType<double> sa4 = new CollectionType<double>(1000111, "Nick", "Hyper", 1234.123333333333333333333, 412374681.123);
  183. CollectionType<double> sa5 = new CollectionType<double>(1001011, "Jim", "Kelmi", 321.1111111111111, 1.2);
  184. CollectionType<char> sa8 = new CollectionType<char>(1001011, "Jim", "Kelmi", 'a', 'e');
  185. CollectionType<float> sa6 = new CollectionType<float>(1101101, "John", "Klimbing", (float)2.2, 1212, 9, (float)3189.31, 213);
  186. sa3.Set = sa1 + sa2;
  187. foreach (var item in sa3.Set)
  188. {
  189. Console.WriteLine(item);
  190. }
  191. if (sa1 <= sa3)
  192. {
  193. Console.WriteLine("Первое множество по мощности больше либо равно объединенному первому множеству со вторым");
  194. }
  195. else
  196. {
  197. Console.WriteLine("Первое множество по мощности меньше либо равно объединенному первому множеству со вторым");
  198. }
  199. Console.WriteLine(sa1 % 2 + " - элемент под индексом 2 в sa1");
  200. Console.WriteLine(1 * sa1 + " - количество элементов в sa1");
  201. Console.WriteLine("Информация о sa1: " + sa1.ToString());
  202. sa5.Set = sa5 + sa4;
  203. Console.WriteLine("Элементы sa4 + sa5 :");
  204. foreach (var item in sa5.Set)
  205. {
  206. Console.WriteLine(item);
  207. }
  208.  
  209. sa4.add(44444);
  210. sa5.delete(1.2);
  211. Console.WriteLine("Информация о sa5:");
  212. sa5.show();
  213. Console.WriteLine("Информация о sa6:");
  214. sa6.show();
  215.  
  216. Question q1 = new Question();
  217. q1.Text = "Oh, it's a question";
  218.  
  219. CollectionType<Question> sa7 = new CollectionType<Question>(1110000, "Katrin", "Zippo", q1);
  220. Console.WriteLine("Информация о sa7:");
  221. sa7.show();
  222.  
  223. }
  224.  
  225. catch (QuestionException e)
  226. {
  227. Console.WriteLine(e);
  228. }
  229.  
  230. catch (Exception e)
  231. {
  232. Console.WriteLine(e);
  233. }
  234.  
  235. finally
  236. {
  237. Console.WriteLine("Программа завершена!");
  238. }
  239. Console.ReadKey();
  240. }
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement