Advertisement
Guest User

Untitled

a guest
Nov 29th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.62 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using static System.Console;
  4.  
  5. namespace zad1
  6. {
  7. class Person:IComparable
  8. {
  9. public string FirstName { get; set; }
  10. public string LastName { get; set; }
  11.  
  12. public DateTime BirthDay { get; set; }
  13.  
  14. public int CompareTo(object obj)
  15. {
  16. throw new NotImplementedException();
  17. }
  18.  
  19. public override string ToString()
  20. {
  21. return String.Format($"{FirstName} {LastName} {BirthDay.ToString("yyyy-MM-dd")}");
  22. }
  23. }
  24.  
  25. class Program
  26. {
  27. private static Random random = new Random();
  28. static void Main(string[] args)
  29. {
  30. const string ENTER = "Naciśnij ENTER, aby kontynuować";
  31. const int SIZE = 5;
  32. Person[] a = new Person[SIZE];
  33. Person[] b = new Person[SIZE];
  34.  
  35. WriteLine("Naciśnij ESC, aby zakończyć działanie programu.");
  36. WriteLine(ENTER);
  37. while (ReadKey().Key != ConsoleKey.Escape)
  38. {
  39. WriteLine("\nMenu: ");
  40. WriteLine("1 - dodanie obiektu/-ów do tablicy A");
  41. WriteLine("2 - usuwanie obiektu/-ów z tablicy A");
  42. WriteLine("3 - sortowanie obiektów w tablicy A");
  43. WriteLine("4 - kopiowanie wycinka z tablicy A do tablicy B");
  44. WriteLine("5 - zmiana rozmiaru tablicy A");
  45. WriteLine("6 - dodanie obiektu/-ów do tablicy B");
  46. WriteLine("7 - usuwanie obiektu/-ów z tablicy B");
  47. WriteLine("8 - sortowanie obiektów w tablicy B");
  48. WriteLine("9 - zmiana rozmiaru tablicy B");
  49. WriteLine("a - wyprowadzenie zawartości tablicy A na monitor");
  50. WriteLine("b - wyprowadzenie zawartości tablicy B na monitor");
  51. WriteLine("r - randomizuj wartości obu tablic");
  52. WriteLine("z - Zakończ program");
  53.  
  54. string choice;
  55. choice = ReadLine();
  56.  
  57. if (choice.Length > 1 && choice != "")
  58. {
  59. choice = choice.Substring(0, 1);
  60. }
  61. if(choice == "1")
  62. {
  63. a = addToTable(a);
  64. }
  65. if (choice == "2")
  66. {
  67. a = deleteFromTable(a);
  68. }
  69. if (choice == "3")
  70. {
  71. Array.Sort(a);
  72. }
  73. if (choice == "4")
  74. {
  75. b = copyPartOfTableToTable(a, b);
  76. }
  77. if (choice == "5")
  78. {
  79. a = changeTableSize(a);
  80. }
  81. if (choice == "6")
  82. {
  83. b = addToTable(b);
  84. }
  85. if (choice == "7")
  86. {
  87. b = deleteFromTable(b);
  88. }
  89. if (choice == "8")
  90. {
  91. Array.Sort(b);
  92. }
  93. if (choice == "9")
  94. {
  95. b = changeTableSize(b);
  96. }
  97. if (choice == "a" || choice == "A")
  98. {
  99. showTable(a);
  100. }
  101. if (choice == "b" || choice == "A")
  102. {
  103. showTable(b);
  104. }
  105. if (choice == "r" || choice == "R")
  106. {
  107. a = randomValuesInTable(a);
  108. b = randomValuesInTable(b);
  109. WriteLine("ZRANDOMIZOWANO !!");
  110. }
  111. if (choice == "z" || choice == "Z")
  112. {
  113. break;
  114. }
  115. }
  116. WriteLine("Do widzenia !!!");
  117. }
  118. public static Person[] changeTableSize(Person[] p)
  119. {
  120. WriteLine("Proszę wpisać nowy rozmiar tablicy ");
  121. int size = Int32.Parse(ReadLine());
  122. Person[] temp = new Person[size];
  123. if (size < p.Length)
  124. {
  125. WriteLine("Wpisany rozmiar jest mniejszy od dotychczasowego! Niektóre elementy zostaną usunięte!");
  126. for (int i = 0; i < size; i++)
  127. {
  128. temp[i] = p[i];
  129. }
  130. }
  131. else
  132. {
  133. for (int i = 0; i < p.Length; i++)
  134. {
  135. temp[i] = p[i];
  136. }
  137. }
  138. return temp;
  139. }
  140.  
  141. public static Person[] addToTable(Person[] p)
  142. {
  143. WriteLine($"Od którego indexu chcesz dodawać? - możesz od 0 do {p.Length - 1}");
  144. int index = Int32.Parse(ReadLine());
  145. if(index < 0 || index > (p.Length - 1))
  146. {
  147. WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
  148. index = 0;
  149. }
  150. int people;
  151. int max = p.Length - index;
  152. WriteLine($"Ile osób chcesz dodać do tablicy? - max:{max}");
  153. people = Int32.Parse(ReadLine());
  154.  
  155. if (people > max)
  156. {
  157. WriteLine("Niepoprawna liczba osób!");
  158. people = 0;
  159. }
  160. for (int i = 0; i < people; i++)
  161. {
  162. WriteLine($"\n Dodanie osoby {i+1}");
  163. WriteLine("Proszę podać imię:");
  164. string name = ReadLine();
  165. WriteLine("Proszę podać nazwisko:");
  166. string surname = ReadLine();
  167. WriteLine("Proszę podać datę urodzenia:");
  168. DateTime date = DateTime.Parse(ReadLine());
  169. p[index] = new Person { LastName = surname, FirstName = name, BirthDay = date };
  170. index++;
  171. }
  172. return p;
  173. }
  174. public static Person[] deleteFromTable(Person[] p)
  175. {
  176. WriteLine($"Od którego indexu chcesz usuwać? - możesz od 0 do {p.Length - 1}");
  177. int index = Int32.Parse(ReadLine());
  178. if (index < 0 || index > (p.Length - 1))
  179. {
  180. WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
  181. index = 0;
  182. }
  183. int people;
  184. int max = p.Length - index;
  185. WriteLine($"Ile osób chcesz usunąć z tablicy? - max:{max}");
  186. people = Int32.Parse(ReadLine());
  187.  
  188. if (people > max)
  189. {
  190. WriteLine("Niepoprawna liczba osób!");
  191. people = 0;
  192. }
  193. for (int i = 0; i < people; i++)
  194. {
  195. p[index] = null;
  196. index++;
  197. }
  198. return p;
  199. }
  200.  
  201. public static Person[] copyPartOfTableToTable(Person[] a, Person[] b)
  202. {
  203. WriteLine($"Od którego indexu chcesz skopiować tablicę? - możesz od 0 do {a.Length - 1}");
  204. int index = Int32.Parse(ReadLine());
  205. if (index < 0 || index > (a.Length - 1))
  206. {
  207. WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
  208. index = 0;
  209. }
  210. int people;
  211. int max = a.Length - index;
  212. WriteLine($"Ile osób chcesz przenieść? - max:{max}");
  213. people = Int32.Parse(ReadLine());
  214.  
  215. if (people > max)
  216. {
  217. WriteLine("Niepoprawna liczba osób!");
  218. people = 0;
  219. }
  220. Person[] temp = new Person[people];
  221. for(int i = 0; i < temp.Length; i++)
  222. {
  223. temp[i] = a[index];
  224. index++;
  225. }
  226. WriteLine($"Od którego indexu chcesz wkleić skopiowaną część tablicy? - możesz od 0 do {b.Length - 1}");
  227. int indexB = Int32.Parse(ReadLine());
  228. if (indexB < 0 || indexB > (b.Length - 1))
  229. {
  230. WriteLine("Wpisano zły index! Automatycznie przypisano index = 0 !!!");
  231. indexB = 0;
  232. }
  233. for (int j = 0; j < people; j++)
  234. {
  235. b[indexB] = temp[j];
  236. indexB++;
  237. }
  238. return b;
  239. }
  240.  
  241. public static Person[] randomValuesInTable(Person[] p)
  242. {
  243. for(int i = 0; i < p.Length; i++)
  244. {
  245. string name = randomString(5);
  246. string surname = randomString(10);
  247. DateTime date = randomDay();
  248. p[i] = new Person {LastName = surname, FirstName = name, BirthDay = date };
  249. }
  250. return p;
  251. }
  252. public static DateTime randomDay()
  253. {
  254. DateTime start = new DateTime(1995, 1, 1);
  255. int range = (DateTime.Today - start).Days;
  256. return start.AddDays(random.Next(range));
  257. }
  258. public static string randomString(int length)
  259. {
  260. const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZqwertyuiopasdfghjklzxcvbnm";
  261. return new string(Enumerable.Repeat(chars, length)
  262. .Select(s => s[random.Next(s.Length)]).ToArray());
  263. }
  264.  
  265. public static void showTable(Person[] p)
  266. {
  267. for(int i = 0; i < p.Length; i++)
  268. {
  269. if (p[i] == null)
  270. {
  271. WriteLine($"osoba - {i}");
  272. }
  273. else {
  274. WriteLine(p[i].ToString());
  275. }
  276. }
  277. }
  278. }
  279. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement