public static List <elem> DobozCsinal(int darabszam) { //létrehoz egy dobozt, benne x egyforma golyóval List <elem> doboz = new List <elem>(); for (int i = 0; i < darabszam; i++) { elem peldany = new elem(); peldany.suly = 1; peldany.index = i; doboz.Add(peldany); } return(doboz); }
public elem(elem source) { value = source.value; term = source.term; altIndex = source.altIndex; }
elem[]/*string[]*/ SortByType(elem[] mas/*string[] x*/, int size) { #region переменные elem[] one = new elem[100]; // первый список: незавершенные ранние задачи, незавершенные задачи for (int i = 0; i < 100; i++) one[i].prior = 0; elem[] two = new elem[100]; // второй список: завершенные задачи for (int i = 0; i < 100; i++) two[i].prior = 0; int tek_one = 0; int tek_two = 0; DateTime now_smaller = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0); DateTime chosen_day_bigger = new DateTime(Portrait.chosen_day.Year, Portrait.chosen_day.Month, Portrait.chosen_day.Day, 23, 59, 59); DateTime chosen_day_smaller = new DateTime(Portrait.chosen_day.Year, Portrait.chosen_day.Month, Portrait.chosen_day.Day, 0, 0, 0); #endregion #region разбивка начального массива на другие по важности(незавершен ранние, незавершен и завершен) che = 0; for (int i = 0; i < 100; i++) if (mas[i].prior == 0) break; else { if (mas[i].date2 < now_smaller && !mas[i].ended && mas[i].date2 <= chosen_day_bigger) { one[tek_one] = mas[i]; one[tek_one].prior = 4;//макимальный приоритете!!! one[tek_one].sou = icon(one[tek_one].type, one[tek_one].prior); tek_one++; } if (mas[i].date2 >= chosen_day_smaller && mas[i].date1 <= chosen_day_bigger) if (mas[i].ended) { two[tek_two] = mas[i]; two[tek_two].prior = 1;//низший приоритет!!! two[tek_two].sou = icon(two[tek_two].type, two[tek_two].prior); tek_two++; } else { one[tek_one] = mas[i]; tek_one++; } } #endregion #region упорядочиваем список one и список two...:) elem y = new elem(); for (int i = 0; i < tek_one; i++) // i - номер прохода { for (int j = tek_one - 1; j > i; j--) { // внутренний цикл прохода if (one[j - 1].type < one[j].type) { y = one[j - 1]; one[j - 1] = one[j]; one[j] = y; } } } y = new elem(); for (int i = 0; i < tek_two; i++) // i - номер прохода { for (int j = tek_two - 1; j > i; j--) { // внутренний цикл прохода if (two[j - 1].type < two[j].type) { y = two[j - 1]; two[j - 1] = two[j]; two[j] = y; } } } #endregion #region собираем массивы вместе int tek_mas = 0; for (int i = 0; i < 100; i++) if (one[i].prior == 0) break; else { mas[tek_mas] = one[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (two[i].prior == 0) break; else { mas[tek_mas] = two[i]; tek_mas++; } che = tek_mas; #endregion return mas; }
elem[] default_sort(elem[] mas) { #region переменные elem[] three = new elem[100]; // третий список: завершенные задачи for (int i = 0; i < 100; i++) three[i].prior = 0; elem[] two = new elem[100]; // второй список: незавершенные задачи for (int i = 0; i < 100; i++) two[i].prior = 0; elem[] one = new elem[100]; // первый список: незавершенные ранние задачи for (int i = 0; i < 100; i++) one[i].prior = 0; elem[] p4 = new elem[100]; // макс приор for (int i = 0; i < 100; i++) p4[i].prior = 0; elem[] p3 = new elem[100]; // высок приор for (int i = 0; i < 100; i++) p3[i].prior = 0; elem[] p2 = new elem[100]; // средн приор for (int i = 0; i < 100; i++) p2[i].prior = 0; elem[] p1 = new elem[100]; // мин приор for (int i = 0; i < 100; i++) p1[i].prior = 0; int tek_one = 0; int tek_two = 0; int tek_three = 0; int tek_p4 = 0; int tek_p3 = 0; int tek_p2 = 0; int tek_p1 = 0; DateTime now_smaller = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 0, 0, 0); DateTime chosen_day_bigger = new DateTime(Portrait.chosen_day.Year, Portrait.chosen_day.Month, Portrait.chosen_day.Day, 23, 59, 59); DateTime chosen_day_smaller = new DateTime(Portrait.chosen_day.Year, Portrait.chosen_day.Month, Portrait.chosen_day.Day, 0, 0, 0); che = 0; #endregion #region разбивка начального массива на другие по важности(незавершен ранние, незавершен(с 4 по 1 приоритет) и завершен) for (int i = 0; i < 100; i++) if (mas[i].prior == 0) break; else { if (mas[i].date2 < now_smaller && !mas[i].ended && mas[i].date2 <= chosen_day_bigger) { one[tek_one] = mas[i]; one[tek_one].prior = 4;//макимальный приоритете!!! one[tek_one].sou = icon(one[tek_one].type, one[tek_one].prior); tek_one++; } if (mas[i].date2 >= chosen_day_smaller && mas[i].date1 <= chosen_day_bigger) if (mas[i].ended) { three[tek_three] = mas[i]; three[tek_three].prior = 1;//низший приоритет!!! three[tek_three].sou = icon(three[tek_three].type, three[tek_three].prior); tek_three++; } else { two[tek_two] = mas[i]; tek_two++; } } for (int i = 0; i < 100; i++) if (two[i].prior == 0) break; else if (two[i].prior == 4) { p4[tek_p4] = two[i]; tek_p4++; } else if (two[i].prior == 3) { p3[tek_p3] = two[i]; tek_p3++; } else if (two[i].prior == 2) { p2[tek_p2] = two[i]; tek_p2++; } else if (two[i].prior == 1) { p1[tek_p1] = two[i]; tek_p1++; } #endregion #region сборка массивов в один конечный(будущего списка) //one, p4, p3, p2, p1 ,three int tek_mas = 0; for (int i = 0; i < 100; i++) if (one[i].prior == 0) break; else { mas[tek_mas] = one[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (p4[i].prior == 0) break; else { mas[tek_mas] = p4[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (p3[i].prior == 0) break; else { mas[tek_mas] = p3[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (p2[i].prior == 0) break; else { mas[tek_mas] = p2[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (p1[i].prior == 0) break; else { mas[tek_mas] = p1[i]; tek_mas++; } for (int i = 0; i < 100; i++) if (three[i].prior == 0) break; else { mas[tek_mas] = three[i]; tek_mas++; } che = tek_mas; #endregion return mas; }
elem[] upor_elem(elem[] mas, int ttype) { switch (ttype) { case 0: return default_sort(mas); case 1: return SortByName(mas, 100); case 2: return SortByType(mas, 100); case 3: return SortByDateBegin(mas, 100); case 4: return SortByDateEnd(mas, 100); default: return mas; } }
public void Get_all(int ttype) { if (grid10.Visibility == Visibility.Visible && move_month) move_month = false; else { elem[] elemArray = new elem[100]; //например, возьмем длину массива = 100 for (int i = 0; i < 100; i++) elemArray[i].prior = 0; IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication(); StreamReader fileReader = null; List<Transaction> transactionList = new List<Transaction>(); List<String> fileList = new List<String>(fileStorage.GetFileNames("*")); string fileString = System.IO.Path.GetFileName("*"); foreach (string file in fileStorage.GetFileNames(App.user_folder + fileString)) fileList.Add((App.user_folder + file)); #region добавление элементов в массив for (int i = 0; i < fileList.Count; i++ ) if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(fileList[i]) == true && fileList[i] != "__ApplicationSettings" && fileList[i].IndexOf("___") == -1) { //time_beg,..end!!! string n = fileList[i].Substring(App.user_folder.Length); #region получение времени открытого файла int sch = 0; string n_y = ""; string n_mn = ""; string n_d = ""; string n_h = ""; string n_m = ""; string e_y = ""; string e_mn = ""; string e_d = ""; string e_h = ""; string e_m = ""; while (n[sch].ToString() != "@") { n_mn += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "@") { n_d += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != " ") { n_y += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { n_h += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { n_m += n[sch]; sch++; } n = n.Substring(sch + 3); sch = 0; while (n[sch].ToString() != "@") { e_mn += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "@") { e_d += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != " ") { e_y += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { e_h += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { e_m += n[sch]; sch++; } #endregion DateTime time_nach_zad = new DateTime(Convert.ToInt32(n_y), Convert.ToInt32(n_mn), Convert.ToInt32(n_d)); DateTime time_konec_zad = new DateTime(Convert.ToInt32(e_y), Convert.ToInt32(e_mn), Convert.ToInt32(e_d)); DateTime chosen_day_date = new DateTime(chosen_day.Year, chosen_day.Month, chosen_day.Day); //if (chosen_day_date >= time_nach_zad && chosen_day_date <= time_konec_zad) //{ fileReader = new StreamReader(new IsolatedStorageFileStream( fileList[i], FileMode.Open, fileStorage)); #region name,type,prior,ended,opisanie string name = ""; int prior = 0; int type = 0; bool ended = false; string opisanie = ""; try { name = fileReader.ReadLine(); if (name.Length > 8) name = name.Substring(0, 7) + ".."; } catch { MessageBox.Show("неверное значение имени!"); } try { prior = Convert.ToInt32(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение приоритета!"); } try { type = Convert.ToInt32(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение типа!"); } try { ended = Convert.ToBoolean(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение выполненности задачи!"); } try { opisanie = fileReader.ReadLine(); if (opisanie.Length > 10) opisanie = opisanie.Substring(0, 9) + ".."; } catch { MessageBox.Show("неверное значение описания!"); } #endregion string sou = icon(type, prior); //way to picture DateTime d1 = new DateTime(Convert.ToInt32(n_y), Convert.ToInt32(n_mn), Convert.ToInt32(n_d), Convert.ToInt32(n_h), Convert.ToInt32(n_m), 0); DateTime d2 = new DateTime(Convert.ToInt32(e_y), Convert.ToInt32(e_mn), Convert.ToInt32(e_d), Convert.ToInt32(e_h), Convert.ToInt32(e_m), 0); elem el = new elem(); el.name = name; el.prior = prior; el.date1 = d1; el.date2 = d2; el.sou = sou; el.opisanie = opisanie; el.n_y = n_y; el.n_mn = n_mn; el.n_d = n_d; el.n_h = n_h; el.n_m = n_m; el.e_y = e_y; el.e_mn = e_mn; el.e_d = e_d; el.e_h = e_h; el.e_m = e_m; el.prior = prior; el.ended = ended; el.type = type; elemArray[che] = el; che++; // количество элементов в elemArray /*if (fileReader != null)*/fileReader.Close(); //} } #endregion elemArray = upor_elem(elemArray, ttype); //упорядочивание элементов массива for (int i = 0; i < che; i++) transactionList.Add(new Transaction("Red", elemArray[i].ended, elemArray[i].sou, elemArray[i].name, elemArray[i].opisanie, elemArray[i].n_d + "." + elemArray[i].n_mn + "." + elemArray[i].n_y.Substring(2) + " " + elemArray[i].n_h + ":" + elemArray[i].n_m, elemArray[i].e_d + "." + elemArray[i].e_mn + "." + elemArray[i].e_y.Substring(2) + " " + elemArray[i].e_h + ":" + elemArray[i].e_m)); che = 0; TransactionList.ItemsSource = transactionList; if (TransactionList.Items.Count > 0) TransactionList.SelectedIndex = 0; } }
public static void Keres(List <elem> doboz) { List <elem> elsodoboz = new List <elem>(); List <elem> masodikdoboz = new List <elem>(); List <elem> harmadikdoboz = new List <elem>(); int index = 0; int maradek = doboz.Count - doboz.Count / 3 - doboz.Count / 3; Console.WriteLine("A nem vizsgált dobozba került ennyi golyó:{0}", maradek); //két dobozba belerakok ugyanannyit for (int i = 0; i < doboz.Count / 3; i++) { elem peldany = new elem(); peldany.suly = doboz[index].suly; peldany.index = doboz[index].index; elsodoboz.Add(peldany); index++; } for (int i = 0; i < doboz.Count / 3; i++) { elem peldany = new elem(); peldany.suly = doboz[index].suly; peldany.index = doboz[index].index; masodikdoboz.Add(peldany); index++; } //egy harmadik dobozba megy a maradék for (int i = 0; i < maradek; i++) { elem peldany = new elem(); peldany.suly = doboz[index].suly; peldany.index = doboz[index].index; harmadikdoboz.Add(peldany); index++; } Console.WriteLine("\nEzt elvileg nem láthatom, csak mégis, hogy valami történjen futás közben:"); Console.WriteLine("{0}\tennyi golyó van benne:{1}", Meres(elsodoboz), elsodoboz.Count); Console.WriteLine("{0}\tennyi golyó van benne:{1}", Meres(masodikdoboz), masodikdoboz.Count); Console.WriteLine("{0}\tennyi golyó van benne:{1}", Meres(harmadikdoboz), harmadikdoboz.Count); /* * Kétkarú mérlegem van * Ha az első két dobozom súlya megegyezik, biztos, hogy a harmadik dobozban van a nehezebb golyó * Ha az első dobozban van a nehezebb golyó, arra billen a kétkarú mérlegem * Ha a második dobozban, akkor abban * Ráeresztem ezt a függvényt a kiválasztott dobozra * * * Itt jönne a lényeg, az eszképszekvencia, meg a rekurzió, de ezt még tökéletesíteni kell, holnap... * Megvan a hiba, ha az első dobozban már csak két golyó van, és azok közül az egyik nehezebb, nem tudja a harmadát belerakni egy dobozba * és így mindig a harmadik dobozba kerül nehezebb golyó->végtelen ciklus->stack overflow * Megoldás: le kéne kezelni, hogy az első két dobozba kerüljön golyó, hogy a kétkarú mérlegemmel el tudjam dönteni, melyik a hunyó * És akkor már szépen is meg lehetne oldani a feladatot * Hát, elvileg kész */ if (doboz.Count <= 3) { if (doboz.Count == 1) { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, egy golyó maradt az utolsó dobozra", doboz[0].index); } if (doboz.Count == 2) { if (doboz[0].suly > doboz[1].suly) { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, két golyó maradt az utolsó dobozra, az elsőben volt", doboz[0].index); } else { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, két golyó maradt az utolsó dobozra, a másodikban volt", doboz[1].index); } } if (doboz.Count == 3) { if (doboz[0].suly == doboz[1].suly) { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, három golyó maradt az utolsó dobozra, a harmadikban volt", doboz[2].index); } else if (doboz[0].suly < doboz[1].suly) { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, három golyó maradt az utolsó dobozra, a másodikban volt", doboz[1].index); } else { Console.WriteLine("{0}. sorszámú golyó volt nehezebb, három golyó maradt az utolsó dobozra, az elsőben volt", doboz[0].index); } } } else { if (Meres(elsodoboz) == Meres(masodikdoboz)) { Keres(harmadikdoboz); } else if (Meres(elsodoboz) < Meres(masodikdoboz)) { Keres(masodikdoboz); } else { Keres(elsodoboz); } } }
private void button1_Click(object sender, RoutedEventArgs e) { elem[] elemArray = new elem[100]; //например, возьмем длину массива = 100 for (int i = 0; i < 100; i++) elemArray[i].prior = 0; //Obtain a virtual store for application IsolatedStorageFile fileStorage = IsolatedStorageFile.GetUserStoreForApplication(); //Create a new StreamReader StreamReader fileReader = null; List<Transaction> transactionList = new List<Transaction>(); List<String> fileList = new List<String>(fileStorage.GetFileNames("*")); string fileString = System.IO.Path.GetFileName("*"); #region поиск задач на тек день и запись их в "elemArray" foreach (string file in fileStorage.GetFileNames(App.user_folder + fileString)) fileList.Add((App.user_folder + file)); for (int i = 0; i < fileList.Count; i++ ) if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(fileList[i]) == true && fileList[i] != "__ApplicationSettings" && fileList[i].IndexOf("___") == -1) { //time_beg,..end!!! string n = fileList[i].Substring(App.user_folder.Length); #region получение даты просматриевомого файла int sch = 0; string n_y = ""; string n_mn = ""; string n_d = ""; string n_h = ""; string n_m = ""; string e_y = ""; string e_mn = ""; string e_d = ""; string e_h = ""; string e_m = ""; while (n[sch].ToString() != "@") { n_mn += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "@") { n_d += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != " ") { n_y += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { n_h += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { n_m += n[sch]; sch++; } n = n.Substring(sch + 3); sch = 0; while (n[sch].ToString() != "@") { e_mn += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "@") { e_d += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != " ") { e_y += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { e_h += n[sch]; sch++; } n = n.Substring(sch + 1); sch = 0; while (n[sch].ToString() != "#") { e_m += n[sch]; sch++; } #endregion DateTime time_nach_zad = new DateTime(Convert.ToInt32(n_y), Convert.ToInt32(n_mn), Convert.ToInt32(n_d)); DateTime time_konec_zad = new DateTime(Convert.ToInt32(e_y), Convert.ToInt32(e_mn), Convert.ToInt32(e_d)); DateTime chosen_day_date = new DateTime(Portrait.chosen_day.Year, Portrait.chosen_day.Month, Portrait.chosen_day.Day); //if (chosen_day_date >= time_nach_zad && chosen_day_date <= time_konec_zad) //{ fileReader = new StreamReader(new IsolatedStorageFileStream( fileList[i], FileMode.Open, fileStorage)); #region name,type,prior,ended,opisanie string name = ""; int prior = 0; int type = 0; bool ended = false; string opisanie = ""; try { name = fileReader.ReadLine(); } catch { MessageBox.Show("неверное значение имени!"); } try { prior = Convert.ToInt32(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение приоритета!"); } try { type = Convert.ToInt32(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение типа!"); } try { ended = Convert.ToBoolean(fileReader.ReadLine()); } catch { MessageBox.Show("неверное значение выполненности задачи!"); } try { opisanie = fileReader.ReadLine(); } catch { MessageBox.Show("неверное значение описания!"); } string colo = ""; switch (prior) { case 4: colo = "Red"; break; case 3: colo = "Orange"; break; case 2: colo = "YellowGreen"; break; case 1: colo = "Green"; break; default: colo = "Navy"; break; } string txt = name+": "+opisanie; #endregion #region nac+kon DateTime d1 = new DateTime(Convert.ToInt32(n_y), Convert.ToInt32(n_mn), Convert.ToInt32(n_d), Convert.ToInt32(n_h), Convert.ToInt32(n_m), 0); DateTime d2 = new DateTime(Convert.ToInt32(e_y), Convert.ToInt32(e_mn), Convert.ToInt32(e_d), Convert.ToInt32(e_h), Convert.ToInt32(e_m), 0); int time_nach = 0; int time_kon = 0; if (chosen_day_date > time_nach_zad) time_nach = 0; else time_nach = ((Convert.ToInt32(d1.Hour) - 5) * 60 + Convert.ToInt32(d1.Minute)) * 776 / 60 / 24; if (chosen_day_date < time_konec_zad) time_kon = 800 - time_nach; else time_kon = ((Convert.ToInt32(d2.Hour) - 5) * 60 + Convert.ToInt32(d2.Minute)) * 776 / 60 / 24 - time_nach; #endregion if (txt.Length * 16 <= 800 - time_nach) { } else { txt = txt.Substring(0, (int)((800 - time_nach) / 16 - 2)); txt += ".."; } if (ended) txt += " [ЗАВЕРШЕНО]"; else txt += " [НЕ ЗАВЕРШЕНО]"; elem el = new elem(); el.name = name; el.prior = prior; el.date1 = d1; el.date2 = d2; el.colo = colo; el.txt = txt; el.time_kon = time_kon; el.time_nach = time_nach; el.ended = ended; elemArray[che] = el; che++; // количество элементов в elemArray fileReader.Close(); //} } #endregion elemArray = upor_elem(elemArray); //упорядочивание элементов массива for (int i = 0; i < che; i++ ) transactionList.Add(new Transaction(elemArray[i].colo, elemArray[i].txt, elemArray[i].time_kon, new Thickness(elemArray[i].time_nach, 0, 0, 0))); che = 0; listBox1.ItemsSource = transactionList; if (listBox1.Items.Count > 0) listBox1.SelectedIndex = 0; }