public List <Deliverable> FindAllDeliverables(String buyersName) { Deliverable d = FindDeliverable(buyersName); List <Deliverable> temp = new List <Deliverable>(); int lowindex, highindex, m; lowindex = 0; highindex = myDeliverables.Count - 1; while (lowindex <= highindex) { m = (lowindex + highindex) / 2; if (myDeliverables[m].Buyer.Name == d.Buyer.Name && !temp.Contains(myDeliverables[m])) { temp.Add(myDeliverables[m]); } if (myDeliverables[m].Buyer.Name.CompareTo(d.Buyer.Name) == -1 || (myDeliverables[m + 1].Buyer.Name == d.Buyer.Name && !temp.Contains(myDeliverables[m + 1]) && !temp.Contains(myDeliverables[m + 1]))) { lowindex = m + 1; } if (myDeliverables[m].Buyer.Name.CompareTo(d.Buyer.Name) == 1 || (myDeliverables[m - 1].Buyer.Name == d.Buyer.Name && !temp.Contains(myDeliverables[m - 1]) && temp.Contains(myDeliverables[m - 1]))) { highindex = m - 1; } } return(temp); }
/// <summary> /// SortByName sorts the elements of the deliverables-list alfabetically by buyer's name. /// </summary> public void SortByName() { //todo Deliverable[] tempArray = myDeliverables.ToArray(); for (int i = 0; i < tempArray.Length; i++) { Deliverable a = tempArray[i]; if (i + 1 < tempArray.Length) { Deliverable b = tempArray[i + 1]; int result = String.Compare(a.Buyer.Name, b.Buyer.Name); if (result > 0) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } } else { break; } } myDeliverables = tempArray.ToList(); }
public void SortByAddressOwn() { //todo Deliverable[] tempArray = myDeliverables.ToArray(); for (int i = 0; i < tempArray.Length; i++) { Deliverable a = tempArray[i]; if (i + 1 != tempArray.Length) { Deliverable b = tempArray[i + 1]; int result = String.Compare(a.Buyer.Street, b.Buyer.Street); if (result > 0) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } else if (result == 0) { if (a.Buyer.Housenumber > b.Buyer.Housenumber) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } } } else { break;; } } myDeliverables = tempArray.ToList(); }
private void swap(int i, int j) { Deliverable temp = myDeliverables[i]; myDeliverables[i] = myDeliverables[j]; myDeliverables[j] = temp; }
public int PostmanComparison(Deliverable obj1, Deliverable obj2) { if (obj1.Buyer.Street != obj2.Buyer.Street) { //First by Street Address return(obj1.Buyer.Street.CompareTo(obj2.Buyer.Street)); } else { int r1 = obj1.Buyer.Housenumber % 2; int r2 = obj2.Buyer.Housenumber % 2; if (r1 != r2) { //Odd Numbers first and then Even Numbers return(r2.CompareTo(r1)); } else { if (r1 == 1) { //Odd Numbers from Small to Large return(obj1.Buyer.Housenumber.CompareTo(obj2.Buyer.Housenumber)); } else { //Even Numbers from Large to Small return(obj2.Buyer.Housenumber.CompareTo(obj1.Buyer.Housenumber)); } } } }
private void btnnamesearch_Click(object sender, EventArgs e) { ts.SortByName(); Deliverable d = ts.BinarySearchByName((tbname.Text), 0, ts.SortedList.Count - 1); MessageBox.Show(d != null ? d.ToString() : "Delivarable not found"); }
private void Swap(int i, int j) { Deliverable temp = Deliverables[i]; Deliverables[i] = Deliverables[j]; Deliverables[j] = temp; }
private void LoadDeliverables() { string [] words; FileStream fs = null; StreamReader sr = null; try { fs = new FileStream("deliverables.txt", FileMode.Open, FileAccess.Read); sr = new StreamReader(fs); string s = sr.ReadLine(); while (s != null) { words = s.Split(' '); int index = Convert.ToInt32(words[2]); Person newper = find(index); Deliverable d = new Deliverable(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]), newper); Deliverables.Add(d); s = sr.ReadLine(); } } catch (IOException) { MessageBox.Show("Error reading file"); } finally { if (sr != null) { sr.Close(); } } }
/// <summary> /// SortByWeight sorts the elements of the deliverables-list from a low weight to a high weight. /// </summary> /// <summary> /// SortByName sorts the elements of the deliverables-list alfabetically by buyer's name. /// </summary> public void SortByName() { //todo for (int i = 0; i < Deliverables.Count; i++) { int indexmin = i; for (int j = 0; j < Deliverables.Count - 1; j++) { //if ((Deliverables[indexmin]).Buyer.Name < (Deliverables[j]).Buyer.Name) //{ // Deliverable temp = Deliverables[j]; // Deliverables[j] = Deliverables[indexmin]; // Deliverables[indexmin] = temp; //} int result = string.Compare((Deliverables[indexmin]).Buyer.Name, (Deliverables[j]).Buyer.Name); if (result < 0) { Deliverable temp = Deliverables[j]; Deliverables[j] = Deliverables[indexmin]; Deliverables[indexmin] = temp; } { } } } }
public Deliverable[] BubbleArraySwap(Deliverable[] tempArray, int a) { Deliverable temp = tempArray[a]; tempArray[a] = tempArray[a + 1]; tempArray[a + 1] = temp; return(tempArray); }
public Deliverable[] ArraySwap(Deliverable[] tempArray, int a, int b) { Deliverable temp = tempArray[a]; tempArray[a] = tempArray[b]; tempArray[b] = temp; return(tempArray); }
public void LoadDeliverablesFromFile(String filename) { //todo StreamReader sr = null; try { sr = new StreamReader(new FileStream(filename, FileMode.Open, FileAccess.Read)); this.myDeliverables.Clear(); int id, weight, buyer; String line = sr.ReadLine(); while (line != null) { string[] info = line.Split(' '); id = Convert.ToInt32(info[0]); weight = Convert.ToInt32(info[1]); buyer = Convert.ToInt32(info[2]); int max = 0; int index = 0; for (int i = 0; i < myDeliverables.Count; i++) { if (myDeliverables[i].Weight >= weight) { if (myDeliverables[i].Weight < max || max == 0) { max = myDeliverables[i].Weight; index = i; } } } Deliverable d = new Deliverable(id, weight, FindPerson(buyer)); if (weight > max) { myDeliverables.Add(d); } else { myDeliverables.Insert(index, d); } line = sr.ReadLine(); } } catch (IOException e) { throw new Exception("Input/Output exception with message \"" + e.Message + "\" occurred", e); } finally { if (sr != null) { sr.Close(); } } }
public void AddDeliverable(Deliverable d) { if (FindDeliverable(d.ID) == null) { myDeliverables.Add(d); } else { throw new Exception("Person not added, since there is already a deliverable with id " + d.ID.ToString()); } }
/// <summary> /// SortForPostman sorts the elements of the deliverables-list alfabetically by street, /// then by postman-ordered house-numbers. /// The postman walks along the street, first the side of the street with the odd numbers (in an increasing order), /// then back on the other side of the street for the even house-numbers (in a decreasing order). /// </summary> public void SortForPostmanOwn() { //todo Deliverable[] tempArray = myDeliverables.ToArray(); for (int i = 0; i < tempArray.Length; i++) { Deliverable a = tempArray[i]; if (i + 1 != tempArray.Length) { Deliverable b = tempArray[i + 1]; int result = String.Compare(a.Buyer.Street, b.Buyer.Street); //Intially sort by street if (result > 0) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } else if (result == 0) { //First Sort Odd first then Even for same street if ((a.Buyer.Housenumber % 2 == 0) && (b.Buyer.Housenumber % 2 != 0)) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } //Second if two house numbers being odd, from small to large else if ((a.Buyer.Housenumber % 2 != 0 && b.Buyer.Housenumber % 2 != 0)) { if (a.Buyer.Housenumber > b.Buyer.Housenumber) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } //Third if two house numbers being even, from large to small } else if ((a.Buyer.Housenumber % 2 == 0 && b.Buyer.Housenumber % 2 == 0)) { if (a.Buyer.Housenumber < b.Buyer.Housenumber) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } } } } else { break;; } } myDeliverables = tempArray.ToList(); }
public int CompareDrumblesByStreet(Deliverable firstDrumble, Deliverable secondDrumble) { if (string.Compare((firstDrumble.Buyer.Street), (secondDrumble.Buyer.Street)) == 0) { return(firstDrumble.Buyer.Housenumber.CompareTo(secondDrumble.Buyer.Housenumber)); } else { return(firstDrumble.Buyer.Street.CompareTo(secondDrumble.Buyer.Street)); } }
private void btnsearchbinary_Click(object sender, EventArgs e) { ts.SortById(); Deliverable d = ts.BinarySearchById(Convert.ToInt32(tbid.Text), 0, ts.SortedList.Count - 1); if (d != null) { MessageBox.Show(d.ToString()); } else { MessageBox.Show("Delivarable not found"); } }
public int CompareAddress(Deliverable s, Deliverable d) { if (s.Buyer.Name.CompareTo(d.Buyer.Name) == 1) { return(1); } if (s.Buyer.Name.CompareTo(d.Buyer.Name) == -1) { return(-1); } else { return(0); } }
private void LoadDeliverablesImmediately() { string[] words; FileStream fs = null; StreamReader sr = null; try { fs = new FileStream("deliverables.txt", FileMode.Open, FileAccess.Read); sr = new StreamReader(fs); string s = sr.ReadLine(); while (s != null) { words = s.Split(' '); int index = Convert.ToInt32(words[2]); Person newper = find(index); Deliverable d = new Deliverable(Convert.ToInt32(words[0]), Convert.ToInt32(words[1]), newper); int indexOfSmallest = Deliverables.Count; if (Deliverables.Count != 0) { for (int j = Deliverables.Count; j < 0; j--) { if (Deliverables[j].Weight > d.Weight) { indexOfSmallest = j; } } Deliverables.Insert(indexOfSmallest, d); } else { Deliverables.Add(d); } s = sr.ReadLine(); } } catch (IOException) { MessageBox.Show("Error reading file"); } finally { if (sr != null) { sr.Close(); } } }
public int ComparePostMan(Deliverable s, Deliverable d) { if (s.Buyer.Street.CompareTo(d.Buyer.Street) == 1) { return(1); } if (s.Buyer.Street.CompareTo(d.Buyer.Street) == -1) { return(-1); } if (d.Buyer.Street.CompareTo(s.Buyer.Street) == 0) { return(0); } return(0); }
private void button1_Click(object sender, EventArgs e) { DeliverableToShow.Clear(); listBox2.Items.Clear(); List <Deliverable> Toshow = Deliverable.SearchPerson(Deliverables, DeliverableToShow, textBox1.Text, 0, Deliverables.Count); if (Toshow != null) { foreach (Deliverable d in Toshow) { listBox2.Items.Add("" + d.ToString()); } } else { MessageBox.Show("Not Found"); } }
/// <summary> /// SortById sorts the elements of the deliverables-list from a low id to a high id. /// </summary> public void SortById() { //todo for (int i = 0; i < Deliverables.Count; i++) { int indexmin = i; for (int j = 0; j < Deliverables.Count - 1; j++) { if ((Deliverables[indexmin]).ID < (Deliverables[j]).ID) { Deliverable temp = Deliverables[j]; Deliverables[j] = Deliverables[indexmin]; Deliverables[indexmin] = temp; } } } }
private void BtnSearchByName_Click_1(object sender, EventArgs e) { int Nr = 0; string SearchKey = TbSearch.Text; tc.SortByName(); ShowInfo(); Deliverable d = RecursiveBinarySearchByName(tc.Deliverables, 0, tc.Deliverables.Count - 1, SearchKey, ref Nr); listBox2.Items.Clear(); if (d != null) { listBox2.Items.Add(d); } else { listBox2.Items.Add($"There is no Deliverable with given Name of {SearchKey}"); } listBox2.Items.Add($"Number of Recursive Calls : {Nr}"); }
private void btnSearch_Click(object sender, EventArgs e) { listBox1.Items.Clear(); Deliverable d = null; if (rbtnID.Checked) { int counter = 0; d = myCompany.FindDeliverable(Convert.ToInt32(tbxSearch.Text), ref counter); if (d != null) { listBox1.Items.Add(d); } else { MessageBox.Show("No deliverables found."); } MessageBox.Show("Recursive calls: " + counter); } else { //List<Deliverable> buyersDeliverables = myCompany.FindAllDeliverables(tbxSearch.Text); //if(buyersDeliverables.Count != 0) //{ // foreach (Deliverable deliverable in buyersDeliverables) // { // listBox1.Items.Add(deliverable); // } //} //else MessageBox.Show("No deliverables found."); d = myCompany.FindDeliverable(tbxSearch.Text); if (d != null) { listBox1.Items.Add(d); } else { MessageBox.Show("No deliverables found."); } } }
private void BtnSearchById_Click_1(object sender, EventArgs e) { int Nr = 0; int SearchKey = Convert.ToInt32(TbSearch.Text); SortById sortId = new SortById(); tc.Deliverables.Sort(sortId); ShowInfo(); Deliverable d = RecursiveBinarySearchById(tc.Deliverables, 0, tc.Deliverables.Count - 1, SearchKey, ref Nr); listBox2.Items.Clear(); if (d != null) { listBox2.Items.Add(d); } else { listBox2.Items.Add($"There is no Deliverable with given Id of {SearchKey}"); } listBox2.Items.Add($"Number of Recursive Calls : {Nr}"); }
/// <summary> /// SortById sorts the elements of the deliverables-list from a low id to a high id. /// </summary> public void SortById() { Deliverable[] tempArray = myDeliverables.ToArray(); for (int i = 0; i < tempArray.Length; i++) { Deliverable a = tempArray[i]; if (i + 1 != tempArray.Length) { Deliverable b = tempArray[i + 1]; if (b.ID < a.ID) { tempArray = BubbleArraySwap(tempArray, i); i = -1; } } else { break;; } } myDeliverables = tempArray.ToList(); }
public List <Deliverable> FindAllDeliverables(String buyersName) { List <Deliverable> temp = new List <Deliverable>(); SortByName(); Deliverable d = BinarySearchByName(buyersName, 0, SortedList.Count - 1); if (d != null) { temp.Add(d); sortedlist.Remove(d); d = BinarySearchByName(buyersName, 0, SortedList.Count - 1); while (d != null) { temp.Add(d); sortedlist.Remove(d); d = BinarySearchByName(buyersName, 0, SortedList.Count - 1); } return(temp); } return(null); }
///Merge Sorting by comparing first elements of both list and add the least /// to the merged list and remove that from it coming list. private void button2_Click(object sender, EventArgs e) { List <Deliverable> list3 = new List <Deliverable>(); int a1, a2; void NumberOfElements() { a1 = list1.Count; a2 = list2.Count; } NumberOfElements(); while (a1 != 0 && a2 != 0) { if (list1[0].Buyer.Street.CompareTo(list2[0].Buyer.Street) == 0) { if (list1[0].Buyer.Housenumber <= list2[0].Buyer.Housenumber) { list3.Add(list1[0]); list1.RemoveAt(0); } else { list3.Add(list2[0]); list2.RemoveAt(0); } } else if (list1[0].Buyer.Street.CompareTo(list2[0].Buyer.Street) > 0) { list3.Add(list2[0]); list2.RemoveAt(0); } else { list3.Add(list1[0]); list1.RemoveAt(0); } NumberOfElements(); } if (a1 == 0) { foreach (var a in list2) { Deliverable temp = null; for (int k = 0; k < list3.Count; k++) { if (a.Buyer.Street.CompareTo(list3[k].Buyer.Street) == 0) { if (a.Buyer.Housenumber <= list3[k].Buyer.Housenumber) { temp = list3[k]; break; } } else if (a.Buyer.Street.CompareTo(list3[k].Buyer.Street) < 0) { temp = list3[k]; break; } } if (temp != null) { list3.Insert(list3.IndexOf(temp), a); } else { list3.Add(a); } } list2.Clear(); } else if (a2 == 0) { foreach (var a in list1) { Deliverable temp = null; for (int k = 0; k < list3.Count; k++) { if (a.Buyer.Street.CompareTo(list3[k].Buyer.Street) == 0) { if (a.Buyer.Housenumber <= list3[k].Buyer.Housenumber) { temp = list3[k]; break; } else { temp = list3[k]; break; } } else if (a.Buyer.Street.CompareTo(list3[k].Buyer.Street) < 0) { temp = list3[k]; break; } } if (temp != null) { list3.Insert(list3.IndexOf(temp), a); } else { list3.Add(a); } } list1.Clear(); } listBox1.Items.Clear(); listBox2.Items.Clear(); tc.SortByAddress(); foreach (var v in tc.Deliverables) { listBox1.Items.Add(v); } //listBox2.Items.Add($"{list1.Count}"); //listBox2.Items.Add($"{list2.Count}"); foreach (var v in list3) { listBox2.Items.Add(v); } }
/// <summary> /// SortForPostman sorts the elements of the deliverables-list alfabetically by street, /// then by postman-ordered house-numbers. /// The postman walks along the street, first the side of the street with the odd numbers (in an increasing order), /// then back on the other side of the street for the even house-numbers (in a decreasing order). /// </summary> public void SortForPostman() { //todo //use insertion sort for this one for (int i = 0; i < Deliverables.Count; i++) { int indexmin = i; for (int j = 0; j < Deliverables.Count - 1; j++) { int result = string.Compare((Deliverables[indexmin]).Buyer.Street, (Deliverables[j]).Buyer.Street); if (result < 0) { Deliverable temp = Deliverables[j]; Deliverables[j] = Deliverables[indexmin]; Deliverables[indexmin] = temp; } else if (result == 0) { //if it's odd int remaindermin = (Deliverables[indexmin]).Buyer.Housenumber % 2; int remainderj = (Deliverables[j]).Buyer.Housenumber % 2; if (remaindermin == 0 && remainderj == 0) { if ((Deliverables[indexmin]).Buyer.Housenumber < (Deliverables[j]).Buyer.Housenumber) { Deliverable temp = Deliverables[j]; Deliverables[j] = Deliverables[indexmin]; Deliverables[indexmin] = temp; } } else if ((remaindermin == 0 && remainderj == 1)) { Deliverable temp = Deliverables[j]; Deliverables[j] = Deliverables[indexmin]; Deliverables[indexmin] = temp; } else if (remaindermin == 1 && remainderj == 0) { } else if (remaindermin == 1 && remainderj == 0) { } //if both are odd // if ((((Deliverables[indexmin]).Buyer.Housenumber < (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 1 && remainder2 == 1) // { // Deliverable temp = Deliverables[j]; // Deliverables[j] = Deliverables[indexmin]; // Deliverables[indexmin] = temp; // } // //if first one is odd // else if ((((Deliverables[indexmin]).Buyer.Housenumber < (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 1 && remainder2 == 0) // { // } // else if ((((Deliverables[indexmin]).Buyer.Housenumber > (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 1 && remainder2 == 0) // { // } // //if the second one is odd //else if ((((Deliverables[indexmin]).Buyer.Housenumber < (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 0 && remainder2 == 1) // { // Deliverable temp = Deliverables[j]; // Deliverables[j] = Deliverables[indexmin]; // Deliverables[indexmin] = temp; // } // else if ((((Deliverables[indexmin]).Buyer.Housenumber > (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 0 && remainder2 == 1) // { // Deliverable temp = Deliverables[j]; // Deliverables[j] = Deliverables[indexmin]; // Deliverables[indexmin] = temp; // } // //if both are even // else if ((((Deliverables[indexmin]).Buyer.Housenumber > (Deliverables[j]).Buyer.Housenumber)) && remainder1 == 0 && remainder2 == 0) // { // Deliverable temp = Deliverables[j]; // Deliverables[j] = Deliverables[indexmin]; // Deliverables[indexmin] = temp; // } } } } }
/// <summary> /// SortByAddress sorts the elements of the deliverables-list "alfabetically by street; then by increasing house-number". /// So elements with a different street are sorted alfabetically by street. /// Elements with the same street are sorted by increasing house-number. /// </summary> private static int CompareByAddress(Deliverable a, Deliverable b) { return((a.Buyer.Street + a.Buyer.Housenumber).CompareTo(b.Buyer.Street + b.Buyer.Housenumber)); }
public int SortByNameComparisonDelegate(Deliverable x, Deliverable y) { return(x.Buyer.Name.CompareTo(y.Buyer.Name)); }