设计一个Windows应用程序,定义一个Student类,包含学号和姓名、年龄三个字段,
并定义一个班级类classlist,该类包括一个Student集合,使用索引器访问该集合,
实现与实例6-3类似的功能。进一步实现按学号升序排序和按年龄降序排序
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 MyProject6_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
try
{
if (textBox1.Text == "")
{
MessageBox.Show("请输入学号!");
}
else
{
Student stu1 = clist[textBox1.Text];
if (stu1 != null)
{
label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age);
}
else
{
label3.Text = string.Format("没有找到学号是{0}的学生",textBox1.Text );
}
}
}
catch
{
MessageBox.Show("未找到学生信息!");
}
}
ClassList clist = new ClassList(10); //创建一个容量为10的班级实例
int n = 0;
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == "" || textBox2.Text.Trim() == ""||textBox3.Text.Trim()=="")
{
MessageBox.Show("请输入学生的学号或姓名或年龄!");
}
else
{
Student stu1 = new Student(textBox1.Text, textBox2.Text, textBox3.Text);
clist[n] = stu1;
n++;
label3.Text += string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age) + "\r\n";
MessageBox.Show("添加成功!");
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
}
}
private void button5_Click(object sender, EventArgs e)
{
try
{
if (textBox4.Text == "")
{
MessageBox.Show("请输入索引号!");
}
else
{
Student stu1 = clist[Convert.ToInt32(textBox4.Text)];
if (stu1 != null)
{
label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname,stu1.Age);
}
else
{
label3.Text = string.Format("没有索引值为a{0}的学生", Convert.ToInt32(textBox4.Text));
}
}
}
catch
{
MessageBox.Show("未找到学生信息!");
}
}
private void button3_Click(object sender, EventArgs e)
{
try
{
if (textBox2.Text == "")
{
MessageBox.Show("请输入姓名!");
}
else
{
Student stu1 = clist[textBox2.Text];
if (stu1 != null)
{
label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname, stu1.Age);
}
else
{
label3.Text = string.Format("没有找到姓名是{0}的学生", textBox2.Text);
}
}
}
catch
{
MessageBox.Show("未找到学生信息!");
}
}
private void button4_Click(object sender, EventArgs e)
{
try
{
if (textBox3.Text == "")
{
MessageBox.Show("请输入年龄!");
}
else
{
Student stu1 = clist[textBox3.Text];
if (stu1 != null)
{
label3.Text = string.Format("学号:{0},姓名:{1},年龄:{2}", stu1.Sno, stu1.Sname, stu1.Age);
}
else
{
label3.Text = string.Format("没有找到姓名是{0}的学生", textBox3.Text);
}
}
}
catch
{
MessageBox.Show("未找到学生信息!");
}
label3.Text = "";
}
}
public class Student
{
private string sno;
private string sname;
private string age;
public Student(string sno, string sname,string age)
{
this.sno = sno;
this.sname = sname;
this.age = age;
}
public string Sno
{
get
{
return sno;
}
}
public string Sname
{
get
{
return sname;
}
}
public string Age
{
get
{
return age;
}
}
}
public class ClassList
{
private Student[] students;
public ClassList(int n)
{
students = new Student[n];
}
//根据索引查找
public Student this[int index]
{
get
{
if (index < 0 || index >= students.Length)
{
return null;
}
return students[index];
}
set
{
if (index < 0 || index >= students.Length)
{
return;
}
students[index] = value;
}
}
//按姓名或学号或年龄查找
public Student this[string infor]
{
get
{
foreach (Student stu in students)
{
if (stu.Sname == infor || stu.Sno == infor || stu.Age == infor)
{
return stu;
}
}
return null;
}
}
}
}
索引查找
姓名查找: