183 lines
5.7 KiB
C#
183 lines
5.7 KiB
C#
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<int> pushAction = (element) => ExecuteOperation(Operation.pushFront, element);
|
||
pushAction(element);
|
||
_Visualizer.SetAddRectangleUp(true);
|
||
}
|
||
|
||
|
||
private void buttonpushBack_Click(object sender, EventArgs e)
|
||
{
|
||
int element = 0; // <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>
|
||
Action<int> 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("<22><><EFBFBD> <20><><EFBFBD><EFBFBD>!");
|
||
}
|
||
else
|
||
{
|
||
MessageBox.Show("<22> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.");
|
||
}
|
||
}
|
||
|
||
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("<22><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
||
}
|
||
}
|
||
|
||
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);
|
||
|
||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||
ArrayDequeState? lastState = stackManager.storage.GetLastState();
|
||
if (lastState != null)
|
||
{
|
||
//stackManager.SetState(lastState);
|
||
//UpdateStackView();
|
||
}
|
||
|
||
MessageBox.Show("<22><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>!");
|
||
}
|
||
}
|
||
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);
|
||
}
|
||
|
||
}
|
||
}
|