-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.cs
More file actions
178 lines (168 loc) · 6.3 KB
/
Data.cs
File metadata and controls
178 lines (168 loc) · 6.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
namespace SortingVisualiser
{
public partial class DataPage : Form
{
private bool CloseAll = true;
public static int[] DataNums { get; private set; }
public DataPage()
{
InitializeComponent();
}
private void HomeBtn_Click(object sender, EventArgs e)
{
CloseAll = false;
//finds home page then unhides it
var formToShow = Application.OpenForms.Cast<Form>()
.FirstOrDefault(c => c is HomePage);
formToShow.Size = new Size(this.Size.Width, this.Size.Height);
formToShow.Location = new Point(this.Location.X, this.Location.Y);
formToShow.Show();
this.Close();
}
private void GameBtn_Click(object sender, EventArgs e)
{
CloseAll = false;
GamePage game = new GamePage();
game.Size = new Size(this.Size.Width, this.Size.Height);
game.Location = new Point(this.Location.X, this.Location.Y);
game.Show();
this.Close();
}
private void SortBtn_Click(object sender, EventArgs e)
{
//shows and hides sorting algorithms
if (SortPanel.Height == SortPanel.MaximumSize.Height)
{
SortPanel.Height = SortPanel.MinimumSize.Height;
}
else
{
SortPanel.Height = SortPanel.MaximumSize.Height;
}
}
private void BubbleSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Bubble);
}
private void MergeSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Merge);
}
private void QuickSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Quick);
}
private void RadixSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Radix);
}
private void InsertionSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Insertion);
}
private void SelectionSortBtn_Click(object sender, EventArgs e)
{
CreateSortPage(SortingName.Selection);
}
private void CreateSortPage(SortingName name)
{
CloseAll = false;
SortPage sort = new SortPage(name);
sort.Size = new Size(this.Size.Width, this.Size.Height);
sort.Location = new Point(this.Location.X, this.Location.Y);
this.Close();
sort.Show();
}
private void DataPage_FormClosing(object sender, FormClosingEventArgs e)
{
if (CloseAll)
{
Application.Exit();
}
}
private void AddDataBtn_Click(object sender, EventArgs e)
{
// Open file explorer, allows you to select a text file
OpenFileDialog openFileDialog = new OpenFileDialog() {
Filter = "Text Documents|*.txt", Multiselect = false, ValidateNames = true
};
//checks valid input
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//places all text into a string then converts it to an array.
string StringToConvert = File.ReadAllText(openFileDialog.FileName);
ConvertString(StringToConvert);
DisplayData();
}
openFileDialog.Dispose();
}
private void ConvertString(string StringToConvert)
{
int CurrentInt = -1;
List<int> ConvertList = new List<int>();
for(int i = 0; i< StringToConvert.Length; i++) //passes through each character
{
if(StringToConvert[i] == ',' && CurrentInt != -1)
{
ConvertList.Add(CurrentInt); //valid so add to list
CurrentInt = -1;
}
else if(CurrentInt > 99999999) { } //to large so ignore
else if(int.TryParse(StringToConvert[i].ToString(), out int n)) //checks valid int.
{
if(CurrentInt == -1) //must start at 0
{
CurrentInt = 0;
}
CurrentInt *= 10; //left shift
CurrentInt += n; //adds on current digit
}
}
if(CurrentInt != -1) //adds last value
{
ConvertList.Add(CurrentInt);
}
DataNums = ConvertList.ToArray(); //converts to array
}
private void DisplayData()
{
DataBox.Clear();
foreach(int i in DataNums)
{
DataBox.Text += i + ",";
}
}
private void SaveDataBtn_Click(object sender, EventArgs e)
{
//copies data
int[] SortedData = new int[DataNums.Length];
DataNums.CopyTo(SortedData, 0);
Array.Sort(SortedData);
//saves as text file
SaveFileDialog savefile = new SaveFileDialog
{
FileName = "SortingVisualiser.txt",
Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
};
//if location and saved name is valid, then write in file.
if (savefile.ShowDialog() == DialogResult.OK)
{
using (StreamWriter sw = new StreamWriter(savefile.FileName))
{
foreach (float num in SortedData)
{
sw.Write(num + ", ");
}
}
}
savefile.Dispose();
}
}
}