Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.72 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace Prog_Tech_lab1
  6. {
  7.  
  8. class HashCode
  9. {
  10. public static int GetRand()
  11. {
  12. Random rnd = new Random();
  13. return rnd.Next(100, 1000);
  14. }
  15. }
  16.  
  17. interface INameAndCopy
  18. {
  19. string Name { get; set; }
  20. object DeepCopy();
  21. int GetHashCode();
  22. }
  23.  
  24. class Person : INameAndCopy
  25. {
  26. private string name;
  27. private string sname;
  28. private System.DateTime birthD;
  29.  
  30. public string Name { get => name; set => name = value; }
  31. public string Sname { get => sname; set => sname = value; }
  32.  
  33. public Person(string n, string sn, DateTime bD)
  34. {
  35. name = n;
  36. sname = sn;
  37. birthD = bD;
  38. }
  39.  
  40. public Person()
  41. {
  42. name = "*Person Name*";
  43. sname = "*Peron SName*";
  44. birthD = new DateTime(1, 1, 1);
  45. }
  46.  
  47. public DateTime BirthD => birthD;
  48.  
  49. public int BirthY
  50. {
  51. get => birthD.Year;
  52. set => birthD = new DateTime(value, birthD.Month, birthD.Day);
  53. }
  54.  
  55. public override string ToString()
  56. {
  57. return string.Format("{0} {1} {2}", Name, Sname, birthD.Year);
  58. }
  59.  
  60. public string ToShortString()
  61. {
  62. return Name + " " + Sname;
  63. }
  64.  
  65. public override bool Equals(Object obj)
  66. {
  67. Person person = (Person)obj;
  68.  
  69. if (Name == person.Name && Sname == person.Sname && BirthD == person.BirthD)
  70. return true;
  71. return false;
  72.  
  73. }
  74.  
  75. public static bool operator ==(Person p1, Person p2)
  76. {
  77. return p1.Equals(p2);
  78. }
  79. public static bool operator !=(Person p1, Person p2)
  80. {
  81. return p1.Equals(p2);
  82. }
  83.  
  84. public override int GetHashCode()
  85. {
  86. return Name.Length + Sname.Length + BirthD.Day + BirthD.Month + BirthD.Year;
  87. }
  88.  
  89. public object DeepCopy()
  90. {
  91. return new Person(Name, Sname, BirthD);
  92. }
  93.  
  94. }
  95.  
  96. class Team : INameAndCopy
  97. {
  98. protected string name;
  99. protected int regNum;
  100.  
  101. public Team()
  102. {
  103. name = "*Def organisation Name*";
  104. regNum = -1;
  105. }
  106.  
  107. public Team(string organisationName, int registrationNum)
  108. {
  109. name = organisationName;
  110. regNum = registrationNum;
  111.  
  112. }
  113.  
  114. public string Name { get => name; set { if (value.Length <= 48) name = value; } }
  115. public int RegNum
  116. {
  117. get => regNum;
  118. set
  119. {
  120. if (value >= 1 && value <= 999)
  121. regNum = value;
  122. else
  123. Console.WriteLine("Ошибка! Введеный регистрационный номер не входит в промежуток [1, 999]");
  124. }
  125. }
  126.  
  127. public virtual object DeepCopy()
  128. {
  129. return new Team(name, regNum);
  130. }
  131.  
  132. public override bool Equals(object obj)
  133. {
  134. Team team = (Team)obj;
  135. if (Name == team.Name && RegNum == team.RegNum)
  136. return true;
  137. return false;
  138. }
  139.  
  140. public static bool operator ==(Team t1, Team t2)
  141. {
  142. return t1.Equals(t2);
  143. }
  144. public static bool operator !=(Team t1, Team t2)
  145. {
  146. return t1.Equals(t2);
  147. }
  148. public override int GetHashCode()
  149. {
  150. return Name.Length + RegNum;
  151. }
  152.  
  153. public override string ToString()
  154. {
  155. return string.Format("Название группы: {0} \nРегистрационный номер: {1}", Name, RegNum);
  156. }
  157. }
  158.  
  159. enum TimeFrame
  160. {
  161. Year = 1,
  162. TwoYear = 2,
  163. Long
  164. }
  165.  
  166. class Paper
  167. {
  168. public string Name { get; set; }
  169. private Person author = new Person();
  170. public Person Author { get => author; set => author = value; }
  171. public System.DateTime PubDate { get; set; }
  172.  
  173. public Paper()
  174. {
  175. Name = "*Name of Book*";
  176. Author.Name = "*Author Name*";
  177. Author.Sname = "*Author Sname*";
  178. Author.BirthY = -1;
  179. PubDate = new DateTime(1, 1, 1);
  180. }
  181.  
  182. public Paper(string name, Person person, DateTime date)
  183. //требуется наличия Person существующего
  184. {
  185. Name = name;
  186. Author = person;
  187. PubDate = date;
  188. }
  189.  
  190. public Paper(string name, string author_name, string author_sname, int author_birthY, DateTime publishing_date)
  191. // без создания отдельного Person
  192. {
  193. Name = name;
  194. Author.Name = author_name;
  195. Author.Sname = author_sname;
  196. Author.BirthY = author_birthY;
  197. PubDate = publishing_date;
  198. }
  199.  
  200. public override string ToString()
  201. {
  202. return string.Format("Название книги: {0} \nАвтор Книги: {1} {2}, {3} года рождения \nГод побликации: {4}",
  203. Name, Author.Name, Author.Sname, Author.BirthY, PubDate);
  204. }
  205.  
  206. public object DeepCopy()
  207. {
  208. return new Paper(Name, Author, PubDate);
  209. }
  210.  
  211. }
  212.  
  213. class ResearchTeam : Team, INameAndCopy
  214. {
  215. private string resName;
  216. private TimeFrame time;
  217. private List<Paper> pub = new List<Paper>();
  218. private List<Person> memb = new List<Person>();
  219.  
  220. public string ResName { get => resName; set => resName = value; }
  221. public List<Paper> Pub { get => pub; set => pub = value; }
  222. public List<Person> Memb { get => memb; set => memb = value; }
  223.  
  224. public TimeFrame Time
  225. {
  226. get => time;
  227. set
  228. {
  229. if (value >= TimeFrame.Year && value <= TimeFrame.Long)
  230. time = TimeFrame.Year;
  231. else
  232. Console.WriteLine("Ошибка");
  233. }
  234. }
  235.  
  236. public ResearchTeam()
  237. {
  238. ResName = "*Research Name";
  239. Name = "*Organisation Name";
  240. RegNum = -1;
  241. Time = TimeFrame.Year; // Дефолтное значение
  242. Pub = new List<Paper>();
  243. Memb = new List<Person>();
  244. }
  245.  
  246. public ResearchTeam(string researchName, Team team, TimeFrame researchTime)
  247. // с инициализированным объектом Team
  248. {
  249. ResName = researchName;
  250. Name = team.Name;
  251. RegNum = team.RegNum;
  252. Time = researchTime;
  253. Pub = new List<Paper>();
  254. Memb = new List<Person>();
  255. }
  256.  
  257. public ResearchTeam(string researchName, string organisationName, int registrationNum, TimeFrame researchTime)
  258. // с инициализацией списка публикаций и участников автоматически
  259. {
  260. ResName = researchName;
  261. Name = organisationName;
  262. RegNum = registrationNum;
  263. Time = researchTime;
  264. Pub = new List<Paper>();
  265. Memb = new List<Person>();
  266. }
  267.  
  268. public ResearchTeam(string researchName, string organisationName, int registrationNum, TimeFrame ResearchTime, List<Paper> publications, List<Person> members)
  269. // требуется инициализированный список публикаций и участников
  270. {
  271. ResName = researchName;
  272. Name = organisationName;
  273. regNum = registrationNum;
  274. time = ResearchTime;
  275. Pub = publications;
  276. Memb = members;
  277. }
  278.  
  279. public Team Team
  280. {
  281. get { Team t = new Team { Name = this.Name, RegNum = this.RegNum }; return t; }
  282. }
  283.  
  284. public Paper LastPubs
  285. {
  286. get
  287. {
  288. if (Pub != null)
  289. {
  290. Paper max = Pub[0];
  291. for (int i = 1; i < Pub.Count; i++)
  292. if (Pub[i].PubDate >= max.PubDate)
  293. max = Pub[i];
  294. return max;
  295. }
  296. return null;
  297. }
  298. }
  299. public bool this[TimeFrame index]
  300. {
  301. get
  302. {
  303. if (Time == index)
  304. return true;
  305. else
  306. return false;
  307. }
  308. }
  309.  
  310. public IEnumerable GetPersonsWithNoPubs()
  311. {
  312. for (int i = 0; i < Memb.Count; i++)
  313. {
  314. bool E = false;
  315. for (int j = 0; j < Pub.Count && !E; j++)
  316. {
  317. if (Memb[i] == Pub[j].Author)
  318. E = true;
  319. }
  320. if (!E) yield return Memb[i];
  321. }
  322. }
  323.  
  324. public IEnumerable GetLastNPaper(int n)
  325. {
  326. DateTime local_date = new DateTime(2019 - n, 1, 1);
  327. for (int i = 0; i < Pub.Count; i++)
  328. if (Pub[i].PubDate > local_date)
  329. yield return Pub[i];
  330. }
  331.  
  332. public void AddPaper(Paper newPaper)
  333. {
  334. pub.Add(newPaper);
  335. }
  336. public void AddPerson(Person newPerson)
  337. {
  338. memb.Add(newPerson);
  339. }
  340.  
  341. public override string ToString()
  342. {
  343. string local_papers_list = "";
  344. foreach (Paper i in pub)
  345. local_papers_list = local_papers_list + i.Name + ", ";
  346.  
  347. string local_person_list = "";
  348. foreach (Person i in memb)
  349. local_person_list = local_person_list + i.Name + " " + i.Sname + ", ";
  350.  
  351. return string.Format("{0} \n{1} \n{2} \n{3} \n{4} \n{5}", ResName, Name, RegNum, Time, local_papers_list, local_person_list);
  352. }
  353.  
  354. public string ToShortString()
  355. {
  356. return ResName + ", " + Name + ", " + RegNum + ", " + Time;
  357. }
  358.  
  359. public override object DeepCopy()
  360. {
  361. List<Paper> new_paper_list = Pub;
  362. List<Person> new_person_list = Memb;
  363. return new ResearchTeam(ResName, Name, RegNum, Time, new_paper_list, new_person_list);
  364. }
  365. }
  366.  
  367. class MainClass
  368. {
  369. public static object GetCopy(INameAndCopy obj)
  370. {
  371. return obj.DeepCopy();
  372. }
  373.  
  374. public static object GetName(INameAndCopy obj)
  375. {
  376. return obj.Name;
  377. }
  378.  
  379. public static int GetCode(INameAndCopy obj)
  380. {
  381. return obj.GetHashCode() + HashCode.GetRand();
  382. }
  383.  
  384. public static void Main(string[] args)
  385. {
  386. Team T1 = new Team("Team1", 333);
  387. Team T2 = new Team("Team1", 333);
  388. Console.WriteLine("T1: " + GetCode(T1) + ", T2: " + GetCode(T2));
  389. Console.WriteLine("_____________________");
  390. //
  391.  
  392. int regTest = int.Parse(Console.ReadLine());
  393. Team TTest = new Team("TeamTest", 10);
  394. TTest.RegNum = regTest;
  395. Console.WriteLine("_____________________");
  396.  
  397. //
  398.  
  399. DateTime default_Date = new DateTime(1890, 1, 1);
  400. Person M = new Person();
  401. Person M1 = new Person("M1", "_", default_Date);
  402. Person M2 = new Person("M2", "_", default_Date);
  403. Person M3 = new Person("M3", "_", default_Date);
  404. Person M4 = new Person("M4", "_", default_Date);
  405.  
  406. Paper P1 = new Paper("Book1", M1, new DateTime(1930, 1, 1));
  407. Paper P2 = new Paper("Book2", M2, new DateTime(1930, 1, 1));
  408. Paper P3 = new Paper("Book3", M2, new DateTime(1935, 1, 1));
  409. Paper P4 = new Paper("Book4", M3, new DateTime(1910, 1, 1));
  410. Paper P5 = new Paper("Book5", M4, new DateTime(1915, 1, 1));
  411.  
  412. ResearchTeam Rt = new ResearchTeam("Research1", T1, TimeFrame.Year);
  413. Rt.AddPerson(M);
  414. Rt.AddPerson(M1);
  415. Rt.AddPerson(M2);
  416. Rt.AddPerson(M3);
  417. Rt.AddPerson(M4);
  418. Rt.AddPaper(P1);
  419. Rt.AddPaper(P2);
  420. Rt.AddPaper(P3);
  421. Rt.AddPaper(P4);
  422. Rt.AddPaper(P5);
  423. Console.WriteLine(Rt.ToString());
  424. Console.WriteLine("_____________________");
  425. //
  426.  
  427. Team TLocal = Rt.Team;
  428. Console.WriteLine(TLocal.ToString());
  429. Console.WriteLine("_____________________");
  430. //
  431.  
  432. ResearchTeam Rt_Copy = (ResearchTeam)GetCopy(Rt);
  433. Rt.Time = TimeFrame.TwoYear;
  434. Console.WriteLine("Копия: \n" + Rt_Copy.ToString() + "\n \nОригинал измененный: \n" + Rt.ToString());
  435. Console.WriteLine("_____________________");
  436. //
  437.  
  438. Console.WriteLine("Не имеют публикаций: \n");
  439. foreach(Person i in Rt.GetPersonsWithNoPubs())
  440. {
  441. Console.WriteLine(i + " ");
  442. }
  443. Console.WriteLine("_____________________");
  444. //
  445.  
  446. Console.WriteLine("За последние 100 лет: \n");
  447. foreach(Paper i in Rt.GetLastNPaper(100))
  448. {
  449. Console.WriteLine(i + " ");
  450. }
  451.  
  452. }
  453. }
  454. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement