//Изпълнява Алгоритъма private void initAlgorithm() { for (int i = 0; i < ctrl.List.VisualElements.Count; i++) { panelAnimation.Controls.Add(ctrl.List.VisualElements[i].Block); ctrl.List.VisualElements[i].Value.BringToFront(); ctrl.List.VisualElements[i].Location = new Point((panelAnimation.Width - (40 * ctrl.List.VisualElements.Count + 12 * (ctrl.List.VisualElements.Count - 1))) / 2 + i * (ctrl.List.VisualElements[i].Block.Width + 12), 100 + ctrl.List.VisualElements[i].Location.Y); AttachmentElement EA = new AttachmentElement(i.ToString()); //Добавя елемент с колонка и стойност ctrl.attachmentList.add(EA); panelAnimation.Controls.Add(EA.Attachment); //Задава локацията на EA attachment element EA.setLocation(ctrl.List.VisualElements[i].Block.Location, 1); } //S e лейбъла който показва масива от числата AttachmentElement AE = new AttachmentElement("S"); ctrl.attachmentList.add(AE); panelAnimation.Controls.Add(AE.Attachment); AE.setAsArrayName(ctrl.List.VisualElements[0].Block.Location); //Прочита алгоритъма от файл InsertionSort.txt codeDesription.Text = System.IO.File.ReadAllText(@".\InsertionSort.txt"); //Изпълнява метод InsertionSort от Controller ctrl.InsertionSort(); }
/// <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; } } }