Commit 4bdbcc88 authored by Unknown's avatar Unknown

Добавил пошаговое выполнение

parent b41e52e8
......@@ -12,6 +12,159 @@ namespace SQRT_Decomposition_Visualizer
{
public partial class Form1 : Form
{
SQRTDecompositionSum sqrtdsum;
int qL, qR;
Steps steps;
class SQRTDecompositionSum
{
List<int> source, support;
int segment_length;
Steps steps;
public SQRTDecompositionSum(List<int> src, ref Steps steps)
{
source = src;
this.steps = steps;
CalculateSupportArray();
}
public int GetSegmentLength()
{
return segment_length;
}
public List<int> GetSupportArray()
{
return support;
}
void CalculateSupportArray()
{
segment_length = (int)Math.Sqrt(source.Count + .0) + 1;
Step s = new Step();
s.description = "Вычислен размер сегмента. Размер сегмента = " + segment_length;
steps.AddStep(s);
support = new List<int>(new int[segment_length]);
List<int> supportStepHelp = new List<int>();
for (int i = 0; i < segment_length; i++)
{
supportStepHelp.Add(0);
}
s = new Step();
s.description = "Инициалирован вспомогательный массив.";
s.support_state = supportStepHelp;
steps.AddStep(s);
for (int i = 0; i < source.Count; ++i)
support[i / segment_length] += source[i];
for (int i=0; i < support.Count; i++)
{
supportStepHelp[i] = support[i];
s = new Step();
s.description = "Вычислена сумма сегмента №" + i;
s.support_state = support;
s.highlight_source = new List<int>();
s.highlight_support = new List<int>();
for (int j = segment_length * i; j < segment_length * (i + 1); j++)
{
s.highlight_source.Add(j);
}
s.highlight_support.Add(i);
steps.AddStep(s);
}
}
public int GetRangeSum(int l, int r)
{
int sum = 0;
int c_l = l / segment_length, c_r = r / segment_length;
if (c_l == c_r)
{
for (int i = l; i <= r; ++i)
sum += source[i];
}
else
{
for (int i = l, end = (c_l + 1) * segment_length - 1; i <= end; ++i)
sum += source[i];
for (int i = c_l + 1; i <= c_r - 1; ++i)
sum += support[i];
for (int i = c_r * segment_length; i <= r; ++i)
sum += source[i];
}
return sum;
}
}
struct Step
{
public string description;
public List<int> highlight_source, highlight_support;
public List<int> support_state;
}
class Steps
{
View source, support;
Panel description;
List<Step> steps;
int currentStep;
public Steps(View source, View support, Panel description)
{
this.source = source;
this.support = support;
this.description = description;
steps = new List<Step>();
}
public void AddStep(Step step)
{
this.steps.Add(step);
}
public void Load()
{
Label counter = description.Controls["stepCounter"] as Label;
counter.Text = "1/" + steps.Count;
currentStep = 1;
LoadStep(1);
}
public void NextStep()
{
if (currentStep < steps.Count)
{
currentStep++;
LoadStep(currentStep);
}
}
public void PrevStep()
{
if (currentStep > 1)
{
currentStep--;
LoadStep(currentStep);
}
}
void LoadStep(int n)
{
(description.Controls["stepCounter"] as Label).Text = n + "/" + steps.Count;
(description.Controls["stepDescription"] as Label).Text = steps[n - 1].description;
if (steps[n - 1].support_state != null)
{
support.DisplayArray(steps[n - 1].support_state);
} else
{
support.Clear();
}
}
}
class View
{
Panel panel;
......@@ -21,9 +174,15 @@ namespace SQRT_Decomposition_Visualizer
this.panel = panel;
this.tag = tag;
}
public void Clear()
{
panel.Controls.Clear();
}
public void DisplayArray(List<int> a)
{
Clear();
int x = 0;
for (int i = 0; i < a.Count; i++)
{
......@@ -68,7 +227,7 @@ namespace SQRT_Decomposition_Visualizer
{
InitializeComponent();
//CHECK SQRT
List<int> source_array = GenerateArray(25, 1, 50);
/*List<int> source_array = GenerateArray(25, 1, 50);
debug_log.AppendText("START ARRAY:\r\n");
foreach (int e in source_array)
{
......@@ -81,59 +240,65 @@ namespace SQRT_Decomposition_Visualizer
{
debug_log.AppendText(e + " ");
}
debug_log.AppendText("\r\nSum from range (2, 8): " + sqrtd.GetRangeSum(2,8));
debug_log.AppendText("\r\nSum from range (2, 8): " + sqrtd.GetRangeSum(2,8));*/
Random r = new Random();
int rndSize = r.Next(5,30);
arrayCount.Text = "" + rndSize;
int rndL = (int)r.Next(0, rndSize/2);
int rndR = (int)r.Next(rndL,rndSize);
queryL.Text = "" + rndL;
queryR.Text = "" + rndR;
qL = rndL; qR = rndR;
calculateSteps(Int32.Parse(arrayCount.Text));
}
//VISUALIZER
void calculateSteps(int length, int min=0, int max= 100)
{
List<int> source_array = GenerateArray(length, min, max);
View source_array_view = new View(source_array_panel, "source");
source_array_view.DisplayArray(source_array);
View support_array_view = new View(support_array_panel, "support");
support_array_view.DisplayArray(sqrtd.GetSupportArray());
steps = new Steps(source_array_view, support_array_view, stepsPanel);
Step s = new Step();
s.description = "Сгенерирован исходный массив, состоящий из " + length +" элементов.";
steps.AddStep(s);
sqrtdsum = new SQRTDecompositionSum(source_array, ref steps);
steps.Load();
}
}
class SQRTDecompositionSum
{
List<int> source, support;
int segment_length;
public SQRTDecompositionSum(List<int> src)
private void prevStep_Click(object sender, EventArgs e)
{
source = src;
CalculateSupportArray();
steps.PrevStep();
}
public int GetSegmentLength()
{
return segment_length;
}
public List<int> GetSupportArray()
private void nextStep_Click(object sender, EventArgs e)
{
return support;
steps.NextStep();
}
void CalculateSupportArray()
private void setArraySize_Click(object sender, EventArgs e)
{
segment_length = (int)Math.Sqrt(source.Count + .0) + 1;
support = new List<int>(new int[segment_length]);
for (int i = 0; i < source.Count; ++i)
support[i / segment_length] += source[i];
int j;
if (Int32.TryParse(arrayCount.Text, out j))
{
if (j > 0)
{
calculateSteps(j);
} else
{
MessageBox.Show("Введенное число должно быть больше 0", "Ошибка");
}
}
else
{
MessageBox.Show("Некорректный ввод","Ошибка");
}
}
public int GetRangeSum(int l, int r)
private void setQuery_Click(object sender, EventArgs e)
{
int sum = 0;
int c_l = l / segment_length, c_r = r / segment_length;
if (c_l == c_r)
{
for (int i = l; i <= r; ++i)
sum += source[i];
} else
{
for (int i = l, end = (c_l + 1) * segment_length - 1; i <= end; ++i)
sum += source[i];
for (int i = c_l + 1; i <= c_r - 1; ++i)
sum += support[i];
for (int i = c_r * segment_length; i <= r; ++i)
sum += source[i];
}
return sum;
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment