在form窗体上面,有个textbox1,我在里面输入一组数字,比如1,2,3,4,5
或者其他数字78,54,89,784,44,5,8,9,7,98,5969,............
逗号为在英文状态下输入,不会有错
然后比较大小,点击计算按钮后,在textbox2里面显示最大值
哪位朋友帮帮我
private void button2_Click(object sender, System.EventArgs e)
{
if ((textBox1.Text == null) || (textBox1.Text.Trim() == ""))
{
return;
}
int[] arrInts = GetArrayInts(textBox1.Text);
textBox2.Text = Sort(arrInts) ? arrInts[arrInts.Length - 1].ToString() : "";
}
private int[] GetArrayInts(string p_strInts)
{
int[] arrInts = null;
try
{
string[] arrStrings = p_strInts.Split(",".ToCharArray());
arrInts = new int[arrStrings.Length];
for (int i = 0; i < arrStrings.Length; i++)
{
arrInts[i] = Int32.Parse(arrStrings[i]);
}
}
catch
{
arrInts = null;
}
return arrInts;
}
private bool Sort(params int[] p_arrInts)
{
if (p_arrInts == null)
{
return false;
}
if (p_arrInts.Length == 1)
{
return true;
}
int intTemp;
int intIndex;
for (int i = 1; i < p_arrInts.Length; i++)
{
intTemp = p_arrInts[i];
intIndex = i;
while ((intIndex > 0) && (p_arrInts[intIndex - 1] > p_arrInts[intIndex]))
{
p_arrInts[intIndex] = p_arrInts[intIndex - 1];
intIndex--;
}
p_arrInts[intIndex] = intTemp;
}
return true;
}