我的程序(C#.NET)有多个问题,不知道是什么原因造成的。
该程序旨在按名字、姓氏和生日first name,last name,DD/MM/YYYY
按升序和降序对姓名和生日列表(格式化)进行排序。它还有其他尚未实现的功能。
第一个问题出在quikSortStr
方法上。程序在第一个if
块中崩溃,说明它j
超出了数组的范围。无论是否发生这种情况mode == "asc"
。
第二个也是更令人困惑的问题是,当从文本文件加载值时, 和 的每个奇数索引值都first
将为last
空,而 的每个奇数索引值bDay
将是1/1/0001
。
我在下面包含了完整的程序以供参考,需要快速排序方法和使用并行数组。我对缺乏评论表示歉意。
提前感谢您的帮助。我完全被难住了。
namespace Names_Arrays
{
public partial class frmNamArrays : Form
{
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-CA");
string[] first;
string[] last;
DateTime[] bDay;
string order = "asc";
string format = "d/M/yyyy";
public frmNamArrays()
{
InitializeComponent();
}
private void write()
{
string[] lines = new string[first.Length];
for (int i = 0; i < lines.Length; i++)
lines[i] = first[i] + ',' + last[i] + ',' + bDay[i].ToString(format);
txtbxNames.Clear();
txtbxNames.Lines = lines;
}
private void load()
{
string[] lines = txtbxNames.Lines;
first = new string[lines.Length];
last = new string[lines.Length];
bDay = new DateTime[lines.Length];
int i = 0;
foreach (string line in lines)
{
string[] data = line.Split(',');
//There aren't any lines that split to a string[] of length less than three,
//but for some reason the program kept believing there are.
//patched that leak.
if (data.Length == 3)
{
first[i] = data[0];
last[i] = data[1];
bDay[i] = Convert.ToDateTime(data[2], culture);
}
i++;
}
}
public DateTime[] quikSortTim(DateTime[] primary, string mode, int left, int right)
{
if (primary.Length > 1)
{
int i = left, j = right;
DateTime pivot = primary[left + (right - left) / 2];
while (i <= j)
{
if (mode == "asc")
{
while (DateTime.Compare(primary[i], pivot) < 0)
i++;
while (DateTime.Compare(primary[j], pivot) > 0)
j--;
}
else
{
while (DateTime.Compare(primary[i], pivot) > 0)
i++;
while (DateTime.Compare(primary[j], pivot) < 0)
j--;
}
if (i <= j)
{
DateTime holdoverB = primary[i];
primary[i++] = primary[j];
primary[j--] = holdoverB;
string holdover = last[i - 1];
last[i] = last[j + 1];
last[j] = holdover;
holdover = first[i - 1];
first[i] = first[j + 1];
first[j] = holdover;
}
}
if (j > left)
primary = quikSortTim(primary, mode, left, j);
if (i < right)
primary = quikSortTim(primary, mode, i, right);
}
return primary;
}
public string[] quikSortStr(string[] primary, string type, string mode, int left, int right)
{
if (primary.Length > 1)
{
int i = left, j = right;
string pivot = primary[left + (right - left) / 2];
while (i <= j)
{
if (mode == "asc")
{
while (String.Compare(primary[i], pivot) < 0)
i++;
while (String.Compare(primary[j], pivot) > 0)
j--;
}
else
{
while (String.Compare(primary[i], pivot) > 0)
i++;
while (String.Compare(primary[j], pivot) < 0)
j--;
}
if (i <= j)
{
string holdover = primary[i];
primary[i] = primary[j];
primary[j] = holdover;
if (type == "first")
{
holdover = last[i];
last[i] = last[j];
last[j] = holdover;
}
else
{
holdover = first[i];
first[i] = first[j];
first[j] = holdover;
}
DateTime holdoverBeta = bDay[i];
bDay[i] = bDay[j];
bDay[j] = holdoverBeta;
i++;
j++;
}
}
if (j > left)
primary = quikSortStr(primary, type, mode, left, j);
if (i < right)
primary = quikSortStr(primary, type, mode, i, right);
}
return primary;
}
private void frmNamArrays_SizeChanged(object sender, EventArgs e)
{
txtbxNames.Width = this.Width - 40;
txtbxNames.Height = this.Height - 157;
}
private void btnSort_Click(object sender, EventArgs e)
{
load();
switch (cbobxCategory.Text)
{
case ("First Name"):
first = quikSortStr(first, "first", order, 0, first.Length - 1);
break;
case ("Last Name"):
last = quikSortStr(last, "last", order, 0, last.Length - 1);
break;
case ("Birthday"):
bDay = quikSortTim(bDay, order, 0, bDay.Length - 1);
break;
default:
break;
}
write();
}
private void cbobxOrder_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbobxOrder.Text == "Ascending")
order = "asc";
else
order = "desc";
}
private void displayfile(string name)
{
StreamReader fileData = new StreamReader(name);
txtbxNames.Lines = fileData.ReadToEnd().Split('\n');
}
private void mnuOpen_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "Text Files|*.txt";
open.Title = "Select a text file...";
if (open.ShowDialog() == DialogResult.OK && open.FileName != "")
displayfile(open.FileName);
}
private void mnuExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}