This commit is contained in:
LivelyPuer 2024-05-22 20:29:11 +04:00
parent 69ad981e94
commit 60380d5262
10 changed files with 265 additions and 74 deletions

View File

@ -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(

View File

@ -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
{

View File

@ -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());
}

View File

@ -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;
}
}

View File

@ -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<Node>(visited), new List<Node>(queue));
}
public void SetState(State state)
@ -36,6 +34,7 @@ public class BFS
adjacencyList = state.AdjacencyList;
visited = state.visited;
queue = new Queue<Node>(state.queue);
isCompleted = queue.Count == 0;
}
public bool Step()

View File

@ -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<Node> visited = [];
[ProtoMember(2)] public List<Node> queue = [];
[ProtoMember(3)] public AdjacencyList AdjacencyList = new();
public bool IsCompleted => !queue.Any();
public State(AdjacencyList adjacencyList, List<Node> visited, List<Node> queue) : this()
{
this.visited = visited;

View File

@ -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<State> states;
List<State> 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];
}

View File

@ -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);

BIN
Cursach/data/4ver.states Normal file

Binary file not shown.

221
Cursach/data/6ver.states Normal file
View File

@ -0,0 +1,221 @@
<EFBFBD>
<08><10><1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10> <08>`<1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10> <08>`
<08><10><1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10> <08>`
<08><10>cd<1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10>
<08>`
<08><10>cd<1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10>
<08>`
<08><10>cd<1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>
<EFBFBD>
<08><10>
<08><10>
<08><10>
<08>`
<08><10>
cd<1A>

cd
<08><10> <08>`
<08><10>
<08><10>cd

<08>`
<08><10>cd
/
<08><10>
<08><10>
<08><10> <08>`
$
<08><10>
<08><10>
<08><10>

<08><10>
<08><10>