using CourseWork_EredavkinRA; using System.Windows.Forms; namespace CourseWorkEreda { public partial class FormMain : Form { private ArrayManager stackManager; private Visualizator _Visualizer; public FormMain() { InitializeComponent(); _Visualizer = new Visualizator(); stackManager = new ArrayManager(new ArrayDequeParameters(10)); } public void ExecuteOperation(Operation operation, int? element = null) { if (operation == Operation.pushFront && element.HasValue) { FormInput pushForm = new FormInput(); pushForm.PushClicked += HandlePushClicked; pushForm.Show(); } else if (operation == Operation.popFront) { stackManager.PopFront(); } else if (operation == Operation.pushBack && element.HasValue) { FormInput pushForm = new FormInput(); pushForm.PushClicked += HandleBackPushClicked; pushForm.Show(); } else if (operation == Operation.popBack) { stackManager.PopBack(); } UpdateStackView(stackManager.GetStorage()); } public void HandlePushClicked(int element) { stackManager.FrontPush(element); UpdateStackView(stackManager.GetStorage()); } public void HandleBackPushClicked(int element) { stackManager.BackPush(element); UpdateStackView(stackManager.GetStorage()); } private void buttonPushFront_Click(object sender, EventArgs e) { int element = 0; Action pushAction = (element) => ExecuteOperation(Operation.pushFront, element); pushAction(element); _Visualizer.SetAddRectangleUp(true); } private void buttonpushBack_Click(object sender, EventArgs e) { int element = 0; // Здесь можно задать значение элемента для добавления в конец стека Action pushAction = (element) => ExecuteOperation(Operation.pushBack, element); pushAction(element); _Visualizer.SetAddRectangleUp(false); } private void buttonpopFront_Click(object sender, EventArgs e) { ExecuteOperation(Operation.popFront); UpdateStackView(stackManager.GetStorage()); } private void buttonpopBack_Click(object sender, EventArgs e) { ExecuteOperation(Operation.popBack); UpdateStackView(stackManager.GetStorage()); } private void buttonEmpty_Click(object sender, EventArgs e) { if (stackManager.IsEmpty()) { MessageBox.Show("Дек пуст!"); } else { MessageBox.Show("В Деке есть элементы."); } } private void buttonInfo_Click(object sender, EventArgs e) { FormInfo informationForm = new FormInfo(); informationForm.Show(); UpdateStackView(stackManager.GetStorage()); } private void buttonSave_Click(object sender, EventArgs e) { SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { stackManager.storage.SaveToFile(saveFileDialog.FileName); MessageBox.Show("Состояния успешно сохранены!"); } } private void buttonLoad_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { stackManager.ClearStates(); stackManager.storage.LoadFromFile(openFileDialog.FileName); // Присвоение последнего состояния стека после загрузки ArrayDequeState? lastState = stackManager.storage.GetLastState(); if (lastState != null) { //stackManager.SetState(lastState); //UpdateStackView(); } MessageBox.Show("Файл успешно загружен!"); } } public void UpdateStackView(StateStorage stateStorage) { ArrayDequeState? currentState = stackManager.GetStates().LastOrDefault(); if (currentState != null) { _Visualizer.Visualize(currentState, this); UpdateStateList(); } } private void UpdateStateList() { lstStackStates.Items.Clear(); foreach (var state in stackManager.GetStates()) { lstStackStates.Items.Add(string.Join(",", state.Array)); } } private void button1_Click(object sender, EventArgs e) { ArrayDequeState? currentState = stackManager.GetStates().LastOrDefault(); if (currentState != null) { _Visualizer.Visualize(currentState, this); } } private void buttonStepForward_Click(object sender, EventArgs e) { var storage = stackManager.StepForward(); UpdateStackView(storage); } private void buttonStepBack_Click(object sender, EventArgs e) { var storage = stackManager.StepBackward(); UpdateStackView(storage); } } }