// Binary search demonstration public static void DemostrateBinarySearch(AbstrState[] abstrState) { bool ok; int pop; do { // Input of the wanted population Console.Write("Enter the population: "); ok = Int32.TryParse(Console.ReadLine(), out pop); if (!ok || pop < 1) { Console.WriteLine("Input error! Perhaps you didn't enter a natural number"); } } while (!ok || pop < 1); // Creating a buffer element to compare with (monarchy is just the easiest, that's why I choose it) Monarchy buffer = new Monarchy("", "", pop, 0, "", ""); // Binary search by population int index = Array.BinarySearch(abstrState, buffer, new ComparePopulation()); if (index == -1) { Console.WriteLine("State with the entered population doesn't exist in the array\n"); } else { Console.WriteLine("Found state's index: " + index + "\n"); abstrState[index].Show(); } }
public static void Main(string[] args) { Message mes, mes2, mes3; Republic ukraine = new Republic(); Monarchy spain = new Monarchy("Spain", 70000000); Kingdom uk = new Kingdom("United Kingdom", 140000000); ukraine.Name = "Ukraine"; ukraine.Population = 41000000; mes = ukraine.Info; mes2 = spain.Info; mes2 += uk.Info; mes3 = mes + mes2; mes3(); Console.WriteLine(); IDerzhava[] masiv = new IDerzhava[3]; masiv[0] = ukraine; masiv[1] = spain; masiv[2] = uk; foreach (IDerzhava derzh in masiv) { mes += derzh.Info; } mes.Invoke(); }
public static void Main(string[] args) { Republic ukraine = new Republic(); Monarchy spain = new Monarchy("Spain", 70000000); Kingdom uk = new Kingdom("United Kingdom", 140000000); ukraine.Name = "Ukraine"; ukraine.Population = 41000000; ukraine.Info(); spain.Info(); uk.Info(); Console.WriteLine(); IDerzhava[] masiv = new IDerzhava[3]; masiv[0] = ukraine; masiv[1] = spain; masiv[2] = uk; foreach (IDerzhava derzh in masiv) { derzh.Info(); } }
ContinentCountry(IEnumerable <Country> countries, string continent) { foreach (Country c in countries) { Monarchy t = c as Monarchy; if (t?.Continent == continent) { yield return(t.Name); } } }
ContinentMonarchs(IEnumerable <Country> countries, string continent) { foreach (Country c in countries) { Monarchy t = c as Monarchy; if (t?.Continent == continent) { yield return(t.Ruler); } } //return countries.Select(c => c as Monarchy).Select(t => t?.Ruler); }
static void Main(string[] args) { #region ArrayList WriteLine("ArrayList"); var Al = new ArrayList(5); Al.Add("123"); Al.Add(122); Al.Add('c'); Al.Add(new List <int>(0)); foreach (var v in Al) { WriteLine("{0}", v); } WriteLine(); Al[2] = 1; Al[1] = new DateTime(2017, 5, 25, 13, 49, 54); Al[3] = new int[0]; foreach (var v in Al) { WriteLine("{0}", v); } WriteLine(); try { WriteLine("индекс \'new int[0]\' - {0}, Обычный поиск\n", Al.IndexOf(new int[0])); } catch (Exception) { WriteLine("No"); } WriteLine(); try { Al.Sort(); } catch (Exception) { WriteLine("Not sorted"); } WriteLine(); foreach (var v in Al) { WriteLine("{0}", v); } WriteLine(); try { WriteLine("индекс \'new int[0]\' - {0}, Бинарный поиск\n", Al.BinarySearch(new int[0])); } catch (Exception) { WriteLine("Unable to find"); } WriteLine(); Al.Clear(); foreach (var v in Al) { WriteLine("{0}", v); } WriteLine(); #endregion #region Stack WriteLine(); WriteLine("Stack"); WriteLine(); var st = new Stack <string>(); st.Push("-0"); foreach (string s in st) { WriteLine(s); } WriteLine(); st.Push(st.Peek()); st.Push("12345"); st.Push(st.Count.ToString()); foreach (string s in st) { WriteLine(s); } WriteLine(); st.Push(st.Pop()); foreach (string s in st) { WriteLine(s); } WriteLine(); st.Clear(); foreach (string s in st) { WriteLine(s); } WriteLine(); #endregion Stack #region MyDictionary WriteLine(); WriteLine("MyDictionary"); WriteLine(); var md = new MyDictionary <Country>(3); md.Add(new Republic("1", 900, "C1", "R1", new [] { "P1" })); var mdCopy = (MyDictionary <Country>)(md.Clone()); foreach (Country c in mdCopy.Values) { WriteLine(c); } WriteLine(); var mon = new Monarchy("2", 5, "55", "R1"); md.Add(mon); mdCopy.Add(new Kingdom("2", 100, "C2", "K1")); foreach (Country c in mdCopy.Values) { WriteLine(c); } WriteLine(); foreach (Country c in md.Values) { WriteLine(c); } WriteLine(); md.AddRange(mdCopy); foreach (Country c in mdCopy.Values) { WriteLine(c); } WriteLine(); mdCopy.Clear(); WriteLine("md.comtains('{0}') == '{1}'", mon, md.ContainsValue(mon)); md.Remove(mon); #endregion ReadKey(true); }
public new object Clone() { var result = new Monarchy(Continent, Population, Name, Ruler); return(result); }
static void Main(string[] args) { // Republic class. Republic republic = new Republic("The Republic of the Congo", "Denis Sassou Nguesso", 5125821, 58, "Africa", 7, 72, 6); // Monarchy class. Monarchy monarchy = new Monarchy("Belgium", "Philippe", 11358357, 189, "Europe", "The House of Saxe-Coburg and Gotha"); // Kingdom class. Kingdom kingdom = new Kingdom("The Kingdom of Saudi Arabia", "Salman", 33000000, 87, "Asia", "The Sudairi Seven"); // Array of objects of classes that use IComparable interface IComparable[] comparable = new IComparable[3]; comparable[0] = republic; comparable[1] = monarchy; comparable[2] = kingdom; // Recreation of the array but with the AbstrState class AbstrState[] abstrState = new AbstrState[3]; for (int i = 0; i < comparable.Length; i++) { abstrState[i] = (AbstrState)comparable[i]; } Console.WriteLine("THE RESULT OF THE 3RD TASK:"); // Showing the array Console.WriteLine("CREATED ARRAY:"); foreach (AbstrState a in abstrState) { a.Show(); } // Sorting the array Array.Sort(abstrState, new ComparePopulation()); // Showing the sorted array Console.WriteLine("ARRAY AFTER BEING SORTED BY POPULATION:"); foreach (AbstrState a in abstrState) { a.Show(); } Array.Sort(abstrState, new CompareName()); Console.WriteLine("ARRAY AFTER BEING SORTED BY NAME:"); foreach (AbstrState a in abstrState) { a.Show(); } Array.Sort(abstrState, new CompareLeaderName()); Console.WriteLine("ARRAY AFTER BEING SORTED BY LEADER NAME:"); foreach (AbstrState a in abstrState) { a.Show(); } Array.Sort(abstrState, new CompareAge()); Console.WriteLine("ARRAY AFTER BEING SORTED BY AGE:"); foreach (AbstrState a in abstrState) { a.Show(); } Array.Sort(abstrState, new CompareContinent()); Console.WriteLine("ARRAY AFTER BEING SORTED BY CONTINENT:"); foreach (AbstrState a in abstrState) { a.Show(); } // Binary search demonstration DemostrateBinarySearch(abstrState); // Shallow clone Republic shallowClone = republic.ShallowCopy(); // Real clone Republic clone = (Republic)republic.Clone(); Console.WriteLine("Shallow clone of the Republic object:"); shallowClone.Show(); Console.WriteLine("Clone of the Republic object:"); clone.Show(); Console.WriteLine("Press ENTER to exit"); Console.ReadLine(); }
static void Main(string[] args) { var countryList = new List <Country>(0); WriteLine("Формирование массива"); var m = new Monarchy("1", 1110, "Россия", "Путин"); countryList.Add(m); WriteLine("\nСоздал: {0}", m); var m1 = (Monarchy)m.Clone(); countryList.Add(m1); WriteLine("Скопировал: {0}", m1); WriteLine(); WriteLine("\nИзменяю {0}", m1); m1.Population = 9000; m1.Name = "Украина"; m1.Ruler = "Порошенко"; WriteLine("\nИзменил (напрямую): {0}", m1); var m2 = new Kingdom("1", 890, "Люксембург", "Король Люксембурга"); countryList.Add(m2); WriteLine("\nСоздал: {0}", m2); WriteLine(); foreach (Country c in countryList) { WriteLine(" {0}", c); } WriteLine(); var uk = new Kingdom("2", 55555, "Соединённое королевство", "Елизавета 2"); WriteLine("\nСоздал: {0}", uk); countryList.Add(uk); WriteLine(); var new_uk = (Kingdom)uk.Clone(); WriteLine("\nСкопировал: {0}", new_uk); countryList.Add(new_uk); countryList[4].Ruler = "Чарльз???"; countryList[4].Population = 44445; WriteLine("\nИзменил (виртуальные методы): {0}", new_uk); WriteLine(); WriteLine(); foreach (Country c in countryList) { WriteLine(" {0}", c); } WriteLine(); var america = new Republic ("3", 12345, "америка", "Трамп", new [] { "номер 1", "номер 2", "номер 3" }); countryList.Add(america); WriteLine("\nСоздал: {0}", america); var brazil = new Monarchy ("3", 11111, "Бразилия", "Президент"); countryList.Add(brazil); WriteLine("\nСоздал: {0}", brazil); WriteLine(); foreach (Country c in countryList) { WriteLine(" {0}", c); } WriteLine(); WriteLine(); WriteLine("На континенте 1 ровно {0} людей", ContinentPeople(countryList, "1")); WriteLine("На континенте 2 ровно {0} людей", ContinentPeople(countryList, "2")); WriteLine("На континенте 3 ровно {0} людей", ContinentPeople(countryList, "3")); WriteLine(); WriteLine("Монархи континента 1"); foreach (string s in ContinentMonarchs(countryList, "1")) { WriteLine(s); } WriteLine(); WriteLine("Монархи континента 2"); foreach (string s in ContinentMonarchs(countryList, "2")) { WriteLine(s); } WriteLine(); WriteLine("Монархи континента 3"); foreach (string s in ContinentMonarchs(countryList, "3")) { WriteLine(s); } WriteLine(); WriteLine("Страны кортинента 1"); foreach (string s in ContinentCountry(countryList, "1")) { WriteLine(s); } WriteLine(); var countryArray = countryList.ToArray(); Array.Sort(countryArray, new Countries_Lab11.SortByName()); countryList = countryArray.ToList(); WriteLine(); foreach (Country c in countryList) { WriteLine(" {0}", c); } WriteLine(); ReadKey(true); }