From 60380d526295ff243fef62d4fd1de767f252709a Mon Sep 17 00:00:00 2001 From: LivelyPuer Date: Wed, 22 May 2024 20:29:11 +0400 Subject: [PATCH] Ready 1v --- Cursach/FormCreate.cs | 4 - Cursach/InfoForm.cs | 12 +- Cursach/MainForm.cs | 36 +-- Cursach/Manager.cs | 15 +- Cursach/Realisations/BFS.cs | 7 +- Cursach/States/State.cs | 7 - .../{StatesManager.cs => StatesStorage.cs} | 32 ++- Cursach/Visualizator.cs | 5 +- Cursach/data/4ver.states | Bin 0 -> 893 bytes Cursach/data/6ver.states | 221 ++++++++++++++++++ 10 files changed, 265 insertions(+), 74 deletions(-) rename Cursach/States/{StatesManager.cs => StatesStorage.cs} (83%) create mode 100644 Cursach/data/4ver.states create mode 100644 Cursach/data/6ver.states diff --git a/Cursach/FormCreate.cs b/Cursach/FormCreate.cs index 242094d..4d1e525 100644 --- a/Cursach/FormCreate.cs +++ b/Cursach/FormCreate.cs @@ -19,10 +19,6 @@ namespace Cursach Draw(); } - private void buttonAddNode_Click(object sender, EventArgs e) - { - } - private void buttonAddEdge_Click(object sender, EventArgs e) { if (!_adjacencyList.AddBind(new Bind( diff --git a/Cursach/InfoForm.cs b/Cursach/InfoForm.cs index c45d75b..bcef37d 100644 --- a/Cursach/InfoForm.cs +++ b/Cursach/InfoForm.cs @@ -1,14 +1,4 @@ -using System; -using System.Collections.Generic; -using System.ComponentModel; -using System.Data; -using System.Drawing; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace Cursach +namespace Cursach { public partial class InfoForm : Form { diff --git a/Cursach/MainForm.cs b/Cursach/MainForm.cs index 6910762..419502c 100644 --- a/Cursach/MainForm.cs +++ b/Cursach/MainForm.cs @@ -7,7 +7,7 @@ namespace Cursach public partial class MainForm : Form { private Visualizator? visualizator; - private StatesManager? _storage; + private StatesStorage? _manager; public MainForm() { @@ -28,13 +28,13 @@ namespace Cursach private void buttonNext_Click(object sender, EventArgs e) { - if (_storage == null) + if (_manager == null) { MessageBox.Show("Сначала нужно создать граф"); return; } - if (_storage.NextState()) + if (_manager.NextState()) { UpdateVizualization(); } @@ -43,7 +43,7 @@ namespace Cursach StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Обход графа в ширину завершен\n"); stringBuilder.Append("Результат:\n"); - foreach (var node in _storage.GetCurrentState().visited) + foreach (var node in _manager.GetCurrentState().visited) { stringBuilder.Append("- " + node + "\n"); } @@ -54,13 +54,13 @@ namespace Cursach private void buttonPrev_Click(object sender, EventArgs e) { - if (_storage == null) + if (_manager == null) { MessageBox.Show("Сначала нужно создать граф"); return; } - if (_storage.PrevState()) + if (_manager.PrevState()) { UpdateVizualization(); } @@ -79,27 +79,27 @@ namespace Cursach return; } - _storage = new Manager(paramter).StartBFS(); + _manager = new Manager(paramter).StartBFS(); MessageBox.Show("Обход графа в ширину запущен"); UpdateVizualization(); } private void Draw() { - if (visualizator == null || _storage == null || _storage.GetCurrentState() == null) + if (visualizator == null || _manager == null || _manager.GetCurrentState() == null) { return; } Bitmap bmp = new(pictureBox1.Width, pictureBox1.Height); Graphics gr = Graphics.FromImage(bmp); - visualizator.Draw(gr, _storage.GetCurrentState()); + visualizator.Draw(gr, _manager.GetCurrentState()); pictureBox1.Image = bmp; } private void SaveToolStripMenuItem1_Click(object sender, EventArgs e) { - if (_storage == null) + if (_manager == null) { MessageBox.Show("Сначала нужно создать граф"); return; @@ -107,7 +107,7 @@ namespace Cursach if (saveFileDialog.ShowDialog() == DialogResult.OK) { - MessageBox.Show(_storage.SaveStateList(saveFileDialog.FileName) + MessageBox.Show(_manager.SaveStateList(saveFileDialog.FileName) ? $"Список состояний успешно сохранён в {saveFileDialog.FileName}" : $"Ошибка при сохранении файла {saveFileDialog.FileName}"); } @@ -115,12 +115,12 @@ namespace Cursach private void LoadToolStripMenuItem_Click(object sender, EventArgs e) { - _storage ??= new StatesManager(); + _manager ??= new StatesStorage(); if (openFileDialog.ShowDialog() == DialogResult.OK) { string filePath = openFileDialog.FileName; - if (_storage.LoadStateList(filePath)) + if (_manager.LoadStateList(filePath)) { UpdateVizualization(); MessageBox.Show("Файл загружен успешно"); @@ -140,18 +140,18 @@ namespace Cursach private void UpdateVizualization() { - startPointLabel.Text = "Отсчёт от: " + _storage.GetStartNode(); - progressBar1.Maximum = _storage.Count; - progressBar1.Value = _storage.CurrentStateIndex + 1; + startPointLabel.Text = "Отсчёт от: " + _manager.GetStartNode(); + progressBar1.Maximum = _manager.Count; + progressBar1.Value = _manager.CurrentStateIndex + 1; listBox1.Items.Clear(); - foreach (var node in _storage.GetCurrentState().queue) + foreach (var node in _manager.GetCurrentState().queue) { listBox1.Items.Add(node.ToString()); } listBox2.Items.Clear(); - foreach (var node in _storage.GetCurrentState().visited) + foreach (var node in _manager.GetCurrentState().visited) { listBox2.Items.Add(node.ToString()); } diff --git a/Cursach/Manager.cs b/Cursach/Manager.cs index 63d45a3..879bbf6 100644 --- a/Cursach/Manager.cs +++ b/Cursach/Manager.cs @@ -1,5 +1,4 @@ -using System.Reflection.Metadata; -using Cursach.Parameters; +using Cursach.Parameters; using Cursach.Realisations; using Cursach.States; @@ -7,16 +6,16 @@ namespace Cursach; public class Manager(BFSParameters parameter) { - public BFS BFS = new(parameter); + private BFS BFS = new(parameter); - public StatesManager StartBFS() + public StatesStorage StartBFS() { - StatesManager statesManager = new(); - statesManager.AddState(BFS.GetState()); + StatesStorage statesStorage = new(); + statesStorage.AddState(BFS.GetState()); while (BFS.Step()) { - statesManager.AddState(BFS.GetState()); + statesStorage.AddState(BFS.GetState()); } - return statesManager; + return statesStorage; } } \ No newline at end of file diff --git a/Cursach/Realisations/BFS.cs b/Cursach/Realisations/BFS.cs index 2a0a0c2..c91703a 100644 --- a/Cursach/Realisations/BFS.cs +++ b/Cursach/Realisations/BFS.cs @@ -1,5 +1,4 @@ -using System.Reflection.Metadata; -using Cursach.Parameters; +using Cursach.Parameters; using Cursach.States; namespace Cursach.Realisations; @@ -7,7 +6,6 @@ namespace Cursach.Realisations; public class BFS { public Node StartNode { private set; get; } - public bool IsEnd { private set; get; } private AdjacencyList adjacencyList; private bool isCompleted = false; @@ -21,13 +19,13 @@ public class BFS visited = new(); queue = new(); queue.Enqueue(StartNode); - IsEnd = false; isCompleted = false; } public State GetState() { return new State(adjacencyList, new List(visited), new List(queue)); + } public void SetState(State state) @@ -36,6 +34,7 @@ public class BFS adjacencyList = state.AdjacencyList; visited = state.visited; queue = new Queue(state.queue); + isCompleted = queue.Count == 0; } public bool Step() diff --git a/Cursach/States/State.cs b/Cursach/States/State.cs index 8c08a11..d8c3bfa 100644 --- a/Cursach/States/State.cs +++ b/Cursach/States/State.cs @@ -1,9 +1,4 @@ using Cursach.Realisations; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; using ProtoBuf; namespace Cursach.States; @@ -15,8 +10,6 @@ public class State() [ProtoMember(1)] public List visited = []; [ProtoMember(2)] public List queue = []; [ProtoMember(3)] public AdjacencyList AdjacencyList = new(); - public bool IsCompleted => !queue.Any(); - public State(AdjacencyList adjacencyList, List visited, List queue) : this() { this.visited = visited; diff --git a/Cursach/States/StatesManager.cs b/Cursach/States/StatesStorage.cs similarity index 83% rename from Cursach/States/StatesManager.cs rename to Cursach/States/StatesStorage.cs index 2e5ec4c..e9e5fa3 100644 --- a/Cursach/States/StatesManager.cs +++ b/Cursach/States/StatesStorage.cs @@ -1,26 +1,13 @@ using Cursach.Realisations; -using System; -using System.Collections.Generic; -using System.ComponentModel.Design; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; using ProtoBuf; namespace Cursach.States; -public class StatesManager +public class StatesStorage { - List states; + List states = []; public int Count => states.Count; - public int CurrentStateIndex { get; private set; } - - public StatesManager() - { - states = []; - CurrentStateIndex = 0; - } + public int CurrentStateIndex { get; private set; } = 0; public bool NextState() { @@ -68,9 +55,18 @@ public class StatesManager return null; } + public State? GoToFirstState() + { + if (states.Count != 0) + { + CurrentStateIndex = 0; + return states[0]; + } + return null; + } public State? GetFirstState() { - if (states.Count == 0) + if (states.Count != 0) { return states[0]; } @@ -80,7 +76,7 @@ public class StatesManager public State? GetLastState() { - if (states.Count == 0) + if (states.Count != 0) { return states[Count - 1]; } diff --git a/Cursach/Visualizator.cs b/Cursach/Visualizator.cs index d6b939f..356deed 100644 --- a/Cursach/Visualizator.cs +++ b/Cursach/Visualizator.cs @@ -3,15 +3,12 @@ using Cursach.States; namespace Cursach; +// Can do this class static public class Visualizator { - private State? State { get; set; } - public void Draw(Graphics g, State state) { Pen penLine = new(Color.Black, 5); - Pen penBid = new(Color.Red, 2); - Pen penSelectedBid = new(Color.Green, 3); Brush darkBrush = new SolidBrush(Color.Black); g.FillRectangle(new SolidBrush(Color.White), 0, 0, 500, 500); diff --git a/Cursach/data/4ver.states b/Cursach/data/4ver.states new file mode 100644 index 0000000000000000000000000000000000000000..6fe59872f65b77cc0fa1abd771860f21df50437f GIT binary patch literal 893 zcmd;@#wf%j#30qr$fd}|A;iY9gi+u;qYxJdBanTOQQ#1h5GMz-5GzL$lRy=h5*M-r z7Y7qi(Hka#i;O~Wtz61LH3)HpW@Iygb|Fc?^#YZ^b#kp`^4XAZJ97LCVYefv#laWu012#zhyVZp literal 0 HcmV?d00001 diff --git a/Cursach/data/6ver.states b/Cursach/data/6ver.states new file mode 100644 index 0000000..fbfffb4 --- /dev/null +++ b/Cursach/data/6ver.states @@ -0,0 +1,221 @@ + + + + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + ` + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + + ` + + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + + + ` +cd + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + + + + ` +cd + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + + + + ` + +cd + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + + + + + + + + + ` + + +cd + +cd + ` + + + +cd + + ` +cd +/ + + + + ` +$ + + + + + + + + \ No newline at end of file