// Построение идеально сбалансированного дерева public static PointTree IdealTree(int size, PointTree p) { PointTree r; int nl, nr; if (size == 0) { p = null; return(p); } nl = size / 2; nr = size - nl - 1; r = new PointTree(); int typeclass = random.Next(1, 4); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); r = first(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); r = first(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); r = first(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); r = first(megapolis); } break; } r.left = IdealTree(nl, r.left); r.right = IdealTree(nr, r.right); return(r); }
public void RandomGeneration(int size) { count = size; list = new ArrayList(size); for (int i = 0; i < size; i++) { int placeType = random.Next(1, 3); switch (placeType) { case 1: { Region region = new Region(); region.RandomCreate(); list[i] = region; } break; case 2: { City city = new City(); city.RandomCreate(); list[i] = city; } break; case 3: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); list[i] = megapolis; } break; } } }
public static MyCollection MakeList(int size) { int typeclass = random.Next(1, 4); MyCollection beg = new MyCollection(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); beg = MakeStack(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); beg = MakeStack(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); beg = MakeStack(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); beg = MakeStack(megapolis); } break; } for (int i = 1; i < size; i++) { typeclass = random.Next(1, 4); MyCollection point = new MyCollection(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); point = MakeStack(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); point = MakeStack(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); point = MakeStack(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); point = MakeStack(megapolis); } break; } point.next = beg; beg.pred = point; beg = point; } return(beg); }
// Добавление элемента в одноправленный список public static PointOne AddPoint(PointOne beg, int number) { int typeclass = random.Next(1, 4); PointOne point = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); point = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); point = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); point = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); point = MakePoint(megapolis); } break; } if (beg == null) { switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); beg = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); beg = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); beg = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); beg = MakePoint(megapolis); } break; } return(beg); } if (number == 1) { point.next = beg; beg = point; return(beg); } // Вспом. переменная для прохода по списку PointOne p = beg; // Идем по списку до нужного элемента for (int i = 1; i < number - 1 && p != null; i++) { p = p.next; } if (p == null) //элемент не найден { Console.WriteLine("Error! The size of List less than Number"); return(beg); } // Lобавляем новый элемент point.next = p.next; p.next = point; return(beg); }
// Добавление в конец однонаправленного списка public static PointOne MakeListToEnd(int size) { int typeclass = random.Next(1, 4); PointOne beg = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); beg = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); beg = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); beg = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); beg = MakePoint(megapolis); } break; } PointOne r = beg; for (int i = 1; i < size; i++) { typeclass = random.Next(1, 4); PointOne point = new PointOne(); switch (typeclass) { case 1: { Place place = new Place(); place.RandomCreate(); Console.WriteLine("The element {0} is adding...", place.ToString()); point = MakePoint(place); } break; case 2: { Region region = new Region(); region.RandomCreate(); Console.WriteLine("The element {0} is adding...", region.ToString()); point = MakePoint(region); } break; case 3: { City city = new City(); city.RandomCreate(); Console.WriteLine("The element {0} is adding...", city.ToString()); point = MakePoint(city); } break; case 4: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); Console.WriteLine("The element {0} is adding...", megapolis.ToString()); point = MakePoint(megapolis); } break; } r.next = point; r = point; } return(beg); }
static void Main(string[] args) { ArrayList placeList = new ArrayList(); Dictionary <string, Place> placeDictionary = new Dictionary <string, Place>(); TestCollections test = new TestCollections(); Random random = new Random(); int mode = InputMode("часть лабраторной работы", MainMenu(), 3); while (mode != 0) { int type; switch (mode) { case 1: { type = InputMode("номер задания", Chapter12Menu(), 7); while (type != 0) { switch (type) { case 1: { int createType = InputMode("тип ввода", CreateMenu(), 2); switch (createType) { case 1: { int placeType = InputClass(); switch (placeType) { case 1: { Region region = new Region(); region.InputCreate(); placeList.Add(region); } break; case 2: { City city = new City(); city.InputCreate(); placeList.Add(city); } break; case 3: { Megapolis megapolis = new Megapolis(); megapolis.InputCreate(); placeList.Add(megapolis); } break; } } break; case 2: { int placeType = random.Next(1, 3); switch (placeType) { case 1: { Region region = new Region(); region.RandomCreate(); placeList.Add(region); } break; case 2: { City city = new City(); city.RandomCreate(); placeList.Add(city); } break; case 3: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); placeList.Add(megapolis); } break; } } break; } } break; case 2: { Console.WriteLine("Введите имя объекта, которого хотите удалить"); string placeName = Console.ReadLine(); bool end = false; for (int i = 0; i < placeList.Count && !end; i++) { Place place = placeList[i] as Place; if (place.PlaceName == placeName) { end = true; placeList.Remove(place); Console.WriteLine("Объект по вашему запросу был удален"); } } if (!end) { Console.WriteLine("Удаление не возможно. В коллекции нет объекта, удовлетворяющему вашему запросу"); } } break; case 3: { int inquiry = InputMode("номер запроса", InquiryMenu(), 3); switch (inquiry) { case 1: { int quantity = 0; for (int i = 0; i < placeList.Count; i++) { if (placeList[i] is Region) { quantity++; } } Console.WriteLine("Кол-во объектов типа REgion: " + quantity); } break; case 2: { List <City> cities = new List <City>(); for (int i = 0; i < placeList.Count; i++) { if (placeList[i] is City) { cities.Add(placeList[i] as City); } } if (cities.Count != 0) { foreach (City city in cities) { city.Show(); } } else { Console.WriteLine("В коллекции нет объектов типа City"); } } break; case 3: { int quantity = 0; for (int i = 0; i < placeList.Count; i++) { if (placeList[i] is Megapolis) { quantity++; } } Console.WriteLine("Кол-во объектов типа Megapolis: " + quantity); } break; } } break; case 4: { Console.WriteLine("Элементы коллекции"); Place[] placies = new Place[placeList.Count]; placeList.CopyTo(placies); foreach (Place place in placies) { place.Show(); } } break; case 5: { ArrayList placeCloneList = (ArrayList)placeList.Clone(); Console.WriteLine("Сколинируемая коллекция"); Place[] placies = null; placeCloneList.CopyTo(placies); foreach (Place place in placies) { place.Show(); } } break; case 6: { placeList.Sort(new SortByName()); Console.WriteLine("Коллекция отсортирована по названию места в алфовитном порядке"); } break; case 7: { Console.WriteLine("Введите названия места, чтобы найти элемент с соответствующим названием"); string search = Console.ReadLine(); int pos = placeList.BinarySearch(search, new SearchByName()); if (pos != -1) { Console.WriteLine($"Объекст найден. Его позиция в коллекции: {pos + 1}"); } else { Console.WriteLine($"Объекст не найден."); } } break; } type = InputMode("номер задания", Chapter12Menu(), 7); } } break; case 2: { type = InputMode("номер задания", Chapter12Menu(), 7); while (type != 0) { switch (type) { case 1: { int createType = InputMode("тип ввода", CreateMenu(), 2); switch (createType) { case 1: { int placeType = InputClass(); switch (placeType) { case 1: { Region region = new Region(); region.InputCreate(); placeDictionary.Add(region.PlaceName, region); } break; case 2: { City city = new City(); city.InputCreate(); placeDictionary.Add(city.PlaceName, city); } break; case 3: { Megapolis megapolis = new Megapolis(); megapolis.InputCreate(); placeDictionary.Add(megapolis.PlaceName, megapolis); } break; } } break; case 2: { int placeType = random.Next(1, 3); switch (placeType) { case 1: { Region region = new Region(); region.RandomCreate(); placeDictionary.Add(region.PlaceName, region); } break; case 2: { City city = new City(); city.RandomCreate(); placeDictionary.Add(city.PlaceName, city); } break; case 3: { Megapolis megapolis = new Megapolis(); megapolis.RandomCreate(); placeDictionary.Add(megapolis.PlaceName, megapolis); } break; } } break; } Console.WriteLine("Объект добавлен"); } break; case 2: { Console.WriteLine("Введите имя объекта, которого хотите удалить"); string placeName = Console.ReadLine(); bool end = false; if (placeDictionary.ContainsKey(placeName)) { placeDictionary.Remove(placeName); } else { Console.WriteLine("Удаление не возможно. В коллекции нет объекта, удовлетворяющему вашему запросу"); } } break; case 3: { int inquiry = InputMode("номер запроса", InquiryMenu(), 3); switch (inquiry) { case 1: { int quantity = 0; foreach (string key in placeDictionary.Keys) { if (placeDictionary[key] is Region) { quantity++; } } Console.WriteLine("Кол-во объектов типа Region: " + quantity); } break; case 2: { List <City> cities = new List <City>(); foreach (string key in placeDictionary.Keys) { if (placeDictionary[key] is City) { City city = placeDictionary[key] as City; cities.Add(city); } } if (cities.Count != 0) { foreach (City city in cities) { city.Show(); } } else { Console.WriteLine("В коллекции нет объектов типа City"); } } break; case 3: { int quantity = 0; foreach (string key in placeDictionary.Keys) { if (placeDictionary[key] is Megapolis) { quantity++; } } Console.WriteLine("Кол-во объектов типа Megapolis: " + quantity); } break; } } break; case 4: { Console.WriteLine("Элементы коллекции"); foreach (string key in placeDictionary.Keys) { placeDictionary[key].Show(); } } break; case 5: { Dictionary <string, Place> copy = new Dictionary <string, Place>(placeDictionary); Console.WriteLine("Сколинируемая коллекция"); foreach (string key in copy.Keys) { copy[key].Show(); } } break; case 6: { var list = placeDictionary.Keys.ToList(); list.Sort(); Dictionary <string, Place> tmp = new Dictionary <string, Place>(); foreach (var key in list) { tmp.Add(key, placeDictionary[key]); } placeDictionary = tmp; Console.WriteLine("Коллекция отсортирована по названию места в алфовитном порядке"); } break; case 7: { Console.WriteLine("Введите названия места, чтобы найти элемент с соответствующим названием"); string search = Console.ReadLine(); var list = placeDictionary.Keys.ToList(); list.Sort(); if (list.IndexOf(search) != 0) { Console.WriteLine("Объект найден"); placeDictionary[search].Show(); } else { Console.WriteLine("Объект не найден"); } } break; } type = InputMode("номер задания", Chapter12Menu(), 7); } } break; case 3: { type = InputMode("номер задания", Chapter3Menu(), 4); while (type != 0) { switch (type) { case 1: { int createType = InputMode("тип ввода", CreateMenu(), 2); switch (createType) { case 1: { Region region = new Region(); region.InputCreate(); test.Add(region); } break; case 2: { Region region = new Region(); region.RandomCreate(); test.Add(region); } break; } } break; case 2: { Console.WriteLine("Введите имя объекта, которого хотите удалить"); string placeName = Console.ReadLine(); if (test.RegionString.ContainsKey(placeName)) { test.Remove(test.RegionString[placeName]); } else { Console.WriteLine("Удаление не возможно. В коллекции нет объекта, удовлетворяющему вашему запросу"); } } break; case 3: { if (test.Size >= 4) { SearchTestCollections(test); } else { test = new TestCollections(); for (int i = 0; i < random.Next(1, 10); i++) { Region region = new Region(); region.RandomCreate(); test.Add(region); } SearchTestCollections(test); } } break; case 4: { Console.WriteLine("Выберете какую именно коллекцию хотите распечатать из объекта типа TestCollections"); Console.WriteLine("1) Regions\n2) Regions names\n3) Region Place\n4) Region String"); int print; while (!(int.TryParse(Console.ReadLine(), out print)) || print < 1 || print > 4) { Console.WriteLine("Ошибка. Тип печати с таким номером не существует. Повторите ввод"); } switch (print) { case 1: { test.PrintList(new List <Region>()); } break; case 2: { test.PrintList(new List <string>()); } break; case 3: { test.PrintDictionary(new SortedDictionary <Place, Region>()); } break; case 4: { test.PrintDictionary(new SortedDictionary <string, Region>()); } break; } } break; } type = InputMode("номер задания", Chapter3Menu(), 4); } } break; } } }