/// <summary> /// Функция за изпълняване на Стъпка /// </summary> private void executeStep() { CStep cstep = ctrl.Steps[Step - 1]; Step step = null; if (CurrentStep == "next") { step = cstep.NextStep; } else { step = cstep.PrevStep; } Highlight(step.Highlight); DescriptionBox.Text = "Стъпка " + Step + ":\n\n" + step.Message; //Foreach цикъл за Командите които се разделят с "|" foreach (string command in step.Command.Split('|')) { //След като обходи командите влиза в switch стейтмънта, спрямо различната команда влиза в различните кейсове switch (command) { //Кейс Join case "Attach": int limit; if (step.Delimiter == -1 || step.UsedelimForUpdateInit) { limit = step.AttachNames.Count; } else { limit = step.Delimiter; } for (int i = 0; i < limit; i++) { int index = ctrl.attachmentList.getIndexByName(step.AttachNames[i]); if (index == -1) { AttachmentElement EA_ = new AttachmentElement(step.AttachNames[i]); ctrl.addAE(EA_); if (step.AttachTo[i] == ctrl.List.IntegerElements.Count) { Point l = new Point(ctrl.List.VisualElements[step.AttachTo[i] - 1].Block.Location.X + 52, ctrl.List.VisualElements[step.AttachTo[i] - 1].Block.Location.Y); EA_.setLocation(l, step.Rows[i]); } else { EA_.setLocation(ctrl.List.VisualElements[step.AttachTo[i]].Block.Location, step.Rows[i]); } panelAnimation.Controls.Add(EA_.Attachment); } else { if (step.AttachTo[i] == ctrl.List.IntegerElements.Count) { Point l = new Point(ctrl.List.VisualElements[step.AttachTo[i] - 1].Block.Location.X + 52, ctrl.List.VisualElements[step.AttachTo[i] - 1].Block.Location.Y); ctrl.attachmentList[ctrl.attachmentList.getIndexByName(step.AttachNames[i])].setLocation(l, step.Rows[i]); } else { ctrl.attachmentList[ctrl.attachmentList.getIndexByName(step.AttachNames[i])].setLocation(ctrl.List.VisualElements[step.AttachTo[i]].Block.Location, step.Rows[i]); } } } break; //Премахва I-тата и Poz label case "Detach": if (step.Delimiter == -1 || step.UsedelimForUpdateInit) { limit = 0; } else { limit = step.Delimiter; } for (int i = limit; i < step.AttachNames.Count; i++) { int index = ctrl.attachmentList.getIndexByName(step.AttachNames[i]); if (index != -1) { panelAnimation.Controls.Remove(ctrl.attachmentList[index].Attachment); ctrl.attachmentList[index].Dispose(); ctrl.attachmentList.remove(index); } } break; //Инциализира променлива и стойност редовете, т.де инциализира стойностите на поле variableDataGridView case "Init": if (step.Delimiter == -1) { limit = step.VariableIndexes.Count(); } else { limit = step.Delimiter; } for (int i = 0; i < limit; i++) { variableDataGridView.Rows.Add(); variableDataGridView.Rows[step.VariableIndexes[i]].Cells[0].Value = step.VariableNames[i]; variableDataGridView.Rows[step.VariableIndexes[i]].Cells[1].Value = step.Values[i]; } break; //Трие определени редове от variableDataGridView case "!Init": if (step.Delimiter == -1) { limit = 0; } else { limit = step.Delimiter; } for (int i = limit; i < step.VariableIndexes.Count(); i++) { variableDataGridView.Rows.RemoveAt(step.VariableIndexes[i]); } break; //Обновява стойностите в dataGridview case "Update": if (step.Delimiter == -1) { limit = step.VariableIndexes.Count(); } else { limit = step.Delimiter; } if (step.UsedelimForUpdateInit) { for (int i = limit; i < step.VariableIndexes.Count(); i++) { variableDataGridView.Rows[step.VariableIndexes[i]].Cells[1].Value = step.Values[i]; } } else { for (int i = 0; i < limit; i++) { variableDataGridView.Rows[step.VariableIndexes[i]].Cells[1].Value = step.Values[i]; } } break; //Кейс за оцветяване на числата case "Highlight": for (int i = 0; i < ctrl.List.VisualElements.Count; i++) { if (step.HighlightenedBlockIndexes.Contains(i)) { //Eлемента който е селектиран остава червен ctrl.List.VisualElements[i].Block.BackColor = Color.Red; } else { //Елементите които не са селектирани остават оранжеви; ctrl.List.VisualElements[i].Block.BackColor = Color.Orange; } } break; //Размества двете стоийности които са избрани спрямо тяхната позиция case "Swap": SortingElement aux = ctrl.List.VisualElements[step.Swit.Item1]; ctrl.List.VisualElements[step.Swit.Item1] = ctrl.List.VisualElements[step.Swit.Item2]; ctrl.List.VisualElements[step.Swit.Item2] = aux; Point auxLoc = ctrl.List.VisualElements[step.Swit.Item1].Location; ctrl.List.VisualElements[step.Swit.Item1].Location = ctrl.List.VisualElements[step.Swit.Item2].Location; ctrl.List.VisualElements[step.Swit.Item2].Location = auxLoc; break; case "IHighlight": for (int i = 0; i < ctrl.List.IntegerElements.Count; i++) { if (step.HighlightenedLabelIndexes.Contains(i)) { //Оцветява на коя позиция мести число ctrl.attachmentList[i].Attachment.ForeColor = Color.Red; } else { //всички останали позиции са в черно ctrl.attachmentList[i].Attachment.ForeColor = Color.Black; } } break; //оцветява елемента позиция 'poz' в червено когато се използва case "AEHighlight": for (int i = 0; i < step.AttachNames.Count; i++) { int index = ctrl.attachmentList.getIndexByName(step.AttachNames[i]); if (index != -1) { ctrl.attachmentList[index].Attachment.ForeColor = Color.Red; } } break; //Оцветява отново елемента 'poz'в черно когато се премине на следващия елемент case "!AEHighlight": for (int i = 0; i < step.AttachNames.Count; i++) { int index = ctrl.attachmentList.getIndexByName(step.AttachNames[i]); if (index != -1) { ctrl.attachmentList[index].Attachment.ForeColor = Color.Black; } } break; //Кейс за пренаписване на стойностите на блокчетата от стойности, Разменя стойностите на swit item1 и swit item2 case "BlockOverwrite": ctrl.List.VisualElements[step.Swit.Item1].ChangeValue(step.Swit.Item2); break; case "THighlight": for (int i = 0; i < variableDataGridView.Rows.Count; i++) { if (step.HighlightedTableIndexes.Contains(i)) { //Вдига флаг selected на редовете variableDataGridView.Rows[i].Selected = true; } } break; //Изчиства DataGridView case "THClear": variableDataGridView.ClearSelection(); break; } } }
//Метод Сортиране чрез вмъкване по стъпки internal void InsertionSort() { ///Insertion sort Stepping algorithm ///Алгоритъм по стъпки CStep cstep = new CStep(); Step step = new Step(); step.Highlight = new List <int>() { 1 }; //Стъпка 1 step.Message = "Получаваме като параметър масив от елементи и неговата дължина."; //Команди за инциализиране и изчистване step.Command = "Init|THClear"; //Име на променливата за масивa в DataGridView-a step.VariableNames = new List <string>() { "arr" }; //взима стойноста на листа,колко елемента има в него step.Values = new List <string>() { List.IntegerElements.Count().ToString() }; step.VariableIndexes = new List <int>() { 0 }; cstep.NextStep = step; step = step.Clone(); step.Command = "!Init|THClear"; step.VariableIndexes = new List <int> { 2, 1 }; cstep.PrevStep = step; Steps.Add(cstep); //Инциализиране на aux,poz //Стъпка 2 Декларация на променливи aux , poz cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 3 }; step.Message = "Декларираме две променливи:\n-'aux' която ще използваме за запаметяване на елемента в позиция i, докато преминаваме през масива\n-'poz' - друга спомагателна променлива " + "кoято ще използваме, за да можем да местим обекти отляво на позицията, в позицията 'i'"; step.Command = "Detach|!Init|THClear"; step.AttachNames = new List <string>() { "i" }; step.VariableIndexes = new List <int>() { 3 }; cstep.PrevStep = step; step = step.Clone(); step.VariableNames = new List <string>() { "aux", "poz" }; step.Values = new List <string>() { "-", "-" }; step.VariableIndexes = new List <int> { 1, 2 }; step.Command = "Init|THClear"; cstep.NextStep = step; Steps.Add(cstep); int aux, poz = -1; //Инциализиране във for цикъла cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 5 }; step.Message = "Нека започнем да разглеждаме масива. За това използваме индекс 'i' който започва от втората позиция до последната позиция в реда, така при всяка стъпка ще спестяваме " + "елемент на позицията 'i' в променливата 'aux' и ще преместваме всички елементи отляво, които са по-големи (в случая) от този елемент с позиция отдясно " + "(ако 'i' започне от първата позиция, тогава няма да имаме елементи отляво). При среща с по-малък елемент от този, който запазваме в 'aux' ще го вмъкнем в " + "'aux' пред този елемент (всички други елементи, по-големи от 'aux' ще бъдат преместени в по-правилна позиция, затова правим място за 'aux' пред първия " + "елемент по-малък от 'aux'). Ако всички елементи са по-големи от 'aux', тогава няма да срещнем елемент който е по-малък от случая, когато след преместване на всички елементи " + "по-големи с позиция отляво, 'aux' ще бъде вмъкнат на първа позиция в масива."; step.Command = "Update|THClear|Highlight"; step.HighlightenedBlockIndexes = new List <int>(); step.VariableIndexes = new List <int>() { 1 }; //заражда стойност "-" на poz и aux step.Values = new List <string>() { "-" }; cstep.PrevStep = step; step = step.Clone(); step.Command = "Attach|Init|THCLear"; step.AttachNames = new List <string>() { "i" }; step.AttachTo = new List <int>() { 1 }; step.Rows = new List <int>() { 2 }; step.VariableIndexes = new List <int>() { 3 }; step.VariableNames = new List <string>() { "i" }; //Зарежда стойност 1 на i step.Values = new List <string>() { "1" }; cstep.NextStep = step; Steps.Add(cstep); for (int i = 1; i < List.IntegerElements.Count; i++) { //Инциализиране в aux cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 7 }; step.Message = "Запазваме елемента от позицията 'i' в 'aux' защото, когато преместваме елементите по-големи от този отляво от него с позиция надясно, стойността на елемента от позицията 'i' ще бъде загубена."; step.Command = "Update|THighlight|Highlight"; step.HighlightenedBlockIndexes = new List <int>() { i }; step.HighlightedTableIndexes = new List <int>() { 1 }; step.AttachNames = new List <string>() { "poz" }; if (poz == -1) { step.Command += "|Detach"; //Зарежда - step.Values = new List <string>() { "-" }; } else { step.Command += "|Attach"; step.AttachTo = new List <int>() { poz }; step.Rows = new List <int>() { 3 }; step.Values = new List <string>() { poz.ToString() }; } step.VariableIndexes = new List <int>() { 2 }; cstep.PrevStep = step; step = step.Clone(); step.Command = "Update|THighlight|Highlight"; step.VariableIndexes = new List <int>() { 1 }; step.Values = new List <string>() { List.IntegerElements[i].ToString() }; cstep.NextStep = step; Steps.Add(cstep); aux = List.IntegerElements[i]; //Инциализиране в poz cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 8 }; step.Message = "Инициализираме променливата 'poz' c 'i' по този начин ние го използваме за препращане към елементите отляво на елемента от позицията 'i'. "; step.Command = "Highlight|!AEHighlight|THClear|IHighlight"; step.HighlightenedBlockIndexes = new List <int>(); step.HighlightenedLabelIndexes = new List <int>(); step.AttachNames = new List <string>() { "poz" }; cstep.PrevStep = step; step = step.Clone(); step.Command = "Attach|Update|THClear|Highlight"; step.AttachTo = new List <int>() { i }; step.Rows = new List <int>() { 3 }; step.VariableIndexes = new List <int>() { 2 }; step.Values = new List <string>() { i.ToString() }; cstep.NextStep = step; Steps.Add(cstep); poz = i; // Инциализиране в while cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 10 }; step.Message = "Тъй като по-горе инициализирахме 'poz' в 'i' и 'i' никога не става 0, 'poz' винаги ще бъде по-голяма от '0' при първото въвеждане по време на итерация на цикъла. Второто условие обаче може да е вярно или не. Сравняваме елемента на позиция 'poz-1' с 'aux'.\nПонастоящем:\narr[poz-1] = " + List.IntegerElements[poz - 1] + "\naux = " + aux; step.Command = "AEHighlight|Highlight|IHighlight|THighlight"; step.HighlightedTableIndexes = new List <int>() { 1 }; step.AttachNames = new List <string>() { "poz" }; step.HighlightenedBlockIndexes = new List <int>() { poz - 1 }; step.HighlightenedLabelIndexes = new List <int>() { 0 }; cstep.NextStep = step; step = step.Clone(); step.Command = "AEHighlight|Highlight|IHighlight|BlockOverwrite|THighlight"; step.Swit = new Tuple <int, int>(poz, List.IntegerElements[poz]); cstep.PrevStep = step; Steps.Add(cstep); while (poz > 0 && List.IntegerElements[poz - 1] > aux) { // arr[poz] = arr[poz-1] cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 12 }; step.Message = "Тъй като елементът в позиция 'poz -1' е по-голям от елемента, който запазихме в 'aux', преместваме този елемент в дясна позиция."; step.Command = "!AEHighlight|Highlight|BlockOverwrite|IHighlight|THClear"; step.HighlightenedLabelIndexes = new List <int>(); step.AttachNames = new List <string>() { "poz" }; step.HighlightenedBlockIndexes = new List <int>() { poz, poz - 1 }; step.Swit = new Tuple <int, int>(poz, List.IntegerElements[poz - 1]); cstep.NextStep = step; step = step.Clone(); step.Command = "Update|Highlight|Attach|THClear"; step.AttachTo = new List <int>() { poz }; step.Rows = new List <int>() { 3 }; step.Values = new List <string>() { poz.ToString() }; step.VariableIndexes = new List <int>() { 2 }; cstep.PrevStep = step; Steps.Add(cstep); List.IntegerElements[poz] = List.IntegerElements[poz - 1]; // poz-- cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 13 }; step.Message = "След това преместваме 'poz' с позиция вляво, за да извършим нова проверка."; step.Command = "Attach|Update|Highlight|THClear"; step.AttachNames = new List <string>() { "poz" }; step.AttachTo = new List <int>() { poz - 1 }; step.Rows = new List <int>() { 3 }; step.HighlightenedBlockIndexes = new List <int>(); step.VariableIndexes = new List <int>() { 2 }; step.Values = new List <string>() { (poz - 1).ToString() }; cstep.NextStep = step; step = step.Clone(); step.Command = "!AEHighlight|IHighlight|THClear"; if (poz != 0) { step.Command += "|Highlight"; } step.HighlightenedLabelIndexes = new List <int>(); cstep.PrevStep = step; Steps.Add(cstep); poz--; //условно while // conditional while cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 10 }; ; if (poz == 0) { step.Message = "'poz' приема позиция 0, което означава, че всички елементи отляво на записаните в 'aux' са по-големи от този."; step.Command = "AEHighlight|IHighlight|THClear"; } else { step.Message = "Тъй като 'poz' все още не е достигнал позиция 0, сравняваме елемента от позиция 'poz-1' с 'aux'.\nПонастоящем:\narr[poz-1] = " + List.IntegerElements[poz - 1] + "\naux = " + aux; if (List.IntegerElements[poz - 1] > aux) { step.Message += "\nТъй като arr[poz-1] е по-голям от aux"; } step.Command = "AEHighlight|Highlight|IHighlight|THighlight"; step.HighlightedTableIndexes = new List <int>() { 1 }; step.HighlightenedBlockIndexes = new List <int>() { poz - 1 }; } step.AttachNames = new List <string>() { "poz" }; step.HighlightenedLabelIndexes = new List <int>() { 0 }; cstep.NextStep = step; step = step.Clone(); if (poz == 0) { step.Command = "AEHighlight|IHighlight|BlockOverwrite|THClear|Highlight"; step.HighlightenedBlockIndexes = new List <int>(); } else { step.Command = "AEHighlight|Highlight|IHighlight|BlockOverwrite|THighlight"; } step.Swit = new Tuple <int, int>(poz, List.IntegerElements[poz]); cstep.PrevStep = step; Steps.Add(cstep); } // arr[poz] = aux cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 16 }; if (poz == 0) { step.Message = "Поставяме елементът 'aux' в позиция 0."; } else { step.Message = "Тъй като елементът в позиция 'poz-1' беше по-малък от 'aux', ние вмъкнахме 'aux' вдясно от този елемент (в позиция 'poz')."; } step.Command = "Attach|Update|Highlight|THighlight"; step.AttachNames = new List <string>() { "i" }; step.AttachTo = new List <int>() { i }; step.Rows = new List <int>() { 2 }; step.VariableIndexes = new List <int>() { 3 }; step.Values = new List <string>() { i.ToString() }; step.HighlightenedBlockIndexes = new List <int>() { poz }; step.HighlightedTableIndexes = new List <int>() { 1 }; cstep.PrevStep = step; step = step.Clone(); step.Command = "!AEHighlight|Highlight|BlockOverwrite|IHighlight|THighlight"; step.HighlightedTableIndexes = new List <int>() { 1 }; step.HighlightenedLabelIndexes = new List <int>(); step.AttachNames = new List <string>() { "poz" }; step.HighlightenedBlockIndexes = new List <int>() { poz }; step.Swit = new Tuple <int, int>(poz, aux); cstep.NextStep = step; Steps.Add(cstep); List.IntegerElements[poz] = aux; //i++ in for cstep = new CStep(); step = new Step(); step.Highlight = new List <int>() { 5 }; step.Message = "Увеличаваме 'i'-тата стойност .\ni = " + (i + 1).ToString() + "\narr = " + List.IntegerElements.Count.ToString() + "\n"; if (i + 1 < List.IntegerElements.Count) { step.Message += "Все още не сме стигнали до последния елемент, така че започваме нова итерация."; } else { step.Message += "i-тата стойност e елемент извън масива, така че за да завършваме итерациите и излизаме от цикъла."; } step.Command = "Attach|Update|Highlight|THClear"; step.AttachNames = new List <string>() { "i" }; step.AttachTo = new List <int>() { i + 1 }; step.Rows = new List <int>() { 2 }; step.VariableIndexes = new List <int>() { 3 }; step.Values = new List <string>() { (i + 1).ToString() }; step.HighlightenedBlockIndexes = new List <int>(); cstep.NextStep = step; step = step.Clone(); if (i + 1 == List.IntegerElements.Count) { step.Command = "Attach|Init|THClear"; step.AttachNames = new List <string>() { "i", "poz" }; step.AttachTo = new List <int>() { i + 1, poz }; step.Rows = new List <int>() { 2, 3 }; step.VariableNames = new List <string>() { "aux", "poz", "i" }; step.VariableIndexes = new List <int>() { 1, 2, 3 }; step.Values = new List <string>() { aux.ToString(), poz.ToString(), (i + 1).ToString() }; } else { step.Command = "Highlight|Update|THClear"; step.HighlightenedBlockIndexes = new List <int>(); step.VariableIndexes = new List <int>() { 1 }; step.Values = new List <string>() { aux.ToString() }; } cstep.PrevStep = step; Steps.Add(cstep); List.IntegerElements[poz] = aux; } //Стъпка край на алгоритъма step = new Step(); step.Highlight = new List <int>() { 18 }; step.Message = "Най-накрая! Масива е сортиран."; step.Command = "!Init|Detach|THClear"; step.VariableIndexes = new List <int>() { 3, 2, 1 }; step.AttachNames = new List <string>() { "i", "poz" }; Steps.Add(new CStep(step, null)); }
/// <summary> /// 设置测试项目 /// </summary> /// <param name="stepName"></param> /// <param name="itemVal"></param> /// <param name="step"></param> /// <param name="er"></param> /// <returns></returns> public static CStep IniStep(EStepName stepName, int stepNo) { try { CStep stepItem = new CStep(); stepItem.stepNo = stepNo; stepItem.name = stepName; switch (stepName) { case EStepName.AC: stepItem.des = "交流电压耐压(AC)测试"; for (int i = 0; i < C_ACItem.Length; i++) { CItem item = new CItem(); item.name = C_ACItem[i]; item.unitDes = C_ACUnit[i]; item.setVal = 0; stepItem.para.Add(item); } break; case EStepName.DC: stepItem.des = "直流电压耐压(DC)测试"; for (int i = 0; i < C_DCItem.Length; i++) { CItem item = new CItem(); item.name = C_DCItem[i]; item.unitDes = C_DCUnit[i]; item.setVal = 0; stepItem.para.Add(item); } break; case EStepName.IR: stepItem.des = "绝缘阻抗(IR)测试"; for (int i = 0; i < C_IRItem.Length; i++) { CItem item = new CItem(); item.name = C_IRItem[i]; item.unitDes = C_IRUnit[i]; item.setVal = 0; stepItem.para.Add(item); } break; case EStepName.GB: stepItem.des = "接地电阻(GB)测试"; for (int i = 0; i < C_GBItem.Length; i++) { CItem item = new CItem(); item.name = C_GBItem[i]; item.unitDes = C_GBUnit[i]; item.setVal = 0; stepItem.para.Add(item); } break; case EStepName.OSC: stepItem.des = "开短路侦测(OS)测试"; for (int i = 0; i < C_OSCItem.Length; i++) { CItem item = new CItem(); item.name = C_OSCItem[i]; item.unitDes = C_OSCUnit[i]; item.setVal = 0; stepItem.para.Add(item); } break; default: break; } return(stepItem); } catch (Exception) { return(null); } }