窗口界面示意图

image.png
运行后界面
image.pngimage.png

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.Collections;
using System.Windows.Forms;
namespace 作业四
{
public partial class Form1 : Form
{
ArrayList arr = new ArrayList();
public Form1()
{
InitializeComponent();
this.Text = “学生信息管理平台”;
label1.Text = “输入学生信息:”;
label1.Text = “输入学生信息:”;
label2.Text = “学 号:”;
label3.Text = “姓 名:”;
label4.Text = “成 绩:”;
label5.Text = “出生日期:”;
groupBox1.Text = “排序方式”;
radioButton1.Text = “按学号升序排序”;
radioButton2.Text = “按姓名升序排序”;
radioButton3.Text = “按成绩降序排序”;
button1.Text = “确认”;
}

public void Output() //分别在listbox1和label6中输出信息
{
if (arr.Count > 0)
{
listBox1.Items.Clear();
listBox1.Items.Add(“学号\t姓名\t年龄\t成绩\t成绩等级”);
listBox1.Items.Add(“====================================”);
foreach (Student stu in arr)
{
listBox1.Items.Add(stu.Display());
}
label6.Text = Student.DisplayAvg();

}
}
private void Button1Click(object sender, EventArgs e)
//click事件发生后,textbox1,2,3中数据存入结构体student中
//并在listbox1中显示
{
try //代码异常处理
{
int id = int.Parse(textBox1.Text);
if (Student.FindId(arr, id)) // 如果输入重复id,则提醒出错
{
= MessageBox.Show(“学号已存在,请重新输入!”, “错误”,
MessageBoxButtons.RetryCancel, MessageBoxIcon.Error) == DialogResult.Retry;
}
else
{
string name = textBox2.Text;
double score= double.Parse(textBox3.Text);
DateTime bir = DateTime.Parse(dateTimePicker1.Text);
Student s = new Student(id, name, bir, score);
s.Score = score;
arr.Add(s);
}
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
Output();
}
catch (Exception exp) // 用消息框显示错误信息
{
MessageBox.Show(exp.Message);
}
}
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
//单选按钮1checkedchanged事件,按照学号升序排序
{
if (radioButton1.Checked)
{
IComparer byID = new myCompareID();
arr.Sort(byID);
Output();
}
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
//单选按钮2checkedchanged事件,按照姓名升序排序
{
if (radioButton2.Checked)
{
IComparer byName = new MyCompareName();
arr.Sort(byName);
Output();
}
}
private void RadioButton3_CheckedChanged(object sender, EventArgs e)
//单选按钮1checkedchanged事件,按照成绩降序排序
{
if (radioButton3.Checked)
{
IComparer byScore = new myCompareScore();
arr.Sort(byScore);
Output();
}
}
}
class Student
{
int id;
string name;
double score;
DateTime birthday;
private static double total_Score;
private static int student_Num;
private static int total_Age;
public Student(int id, string name, DateTime bir, double score) //构造器
{
this.id = id;
this.name = name;
this.birthday = bir;
this.score = score;
student_Num++; //静态字段初始化
total_Age += DateTime.Today.Year - bir.Year;
total_Score += score;
}
public double Score //属性:成绩
{
set
{
if (score > 100)
{
score = 100;
}
else if (score < 0)
{
score = 0;
}
else
{
score = value;
}

}
get
{
return score;
}
}
public int ID //属性:学号
{
set
{
id = value;
}
get
{
return id;
}
}
public string Name //属性:姓名
{
set
{
name = value;
}
get
{
return name;
}
}
public DateTime Birthday
{
set
{
birthday = value;
}
}
public string ScoreDegree //判断学生成绩的等级 !!
{
get
{
if (score >= 90)
{
return “优秀”;
}
else if (score >= 80)
{
return “良好”;
}
else if (score >= 70)
{
return “中等”;
}
else if (score >= 60)
{
return “及格”;
}
else
{
return “不及格”;
}
}
}
public string Display() //用字符串*显示单个学生信息
{
string str = string.Format(“{0}\t{1}\t{2}\t{3}\t{4}”,
id, name, DateTime.Today.Year - birthday.Year, score,ScoreDegree);
return str;
}
public static string DisplayAvg() //用字符串显示总体学生信息
{
string str1 = string.Format(“总人数:{0} \t平均年龄:{1} \t平均成绩:{2}”,
student_Num, total_Age 1.0 / student_Num, total_Score 1.0 / student_Num);
return str1;
}
public static bool FindId(ArrayList arr, int id) //防止重复输入
{
foreach (Student a in arr)
if (a.ID == id) return true;
return false;
}
}
public class myCompareScore : IComparer //对比两个学生的成绩*降序
{
int IComparer.Compare(object x, object y)
{
Student a = (Student)x;
Student b = (Student)y;
if (a.Score > b.Score) //sort中,compare结果大于0,则交换,小于0不交换
{
return -1;
}
else if (a.Score == b.Score)
{
return 0;
}
else
{
return 1;
}
}
}
public class myCompareID : IComparer //对比两个学生的学号,升序**
{
int IComparer.Compare(object x, object y)
{
Student a = (Student)x;
Student b = (Student)y;
if (a.ID < b.ID)
{
return -1;
}
else if (a.ID == b.ID)
{
return 0;
}
else
{
return 1;
}
}
}
public class MyCompareName : IComparer //对比两个学生的姓名,升序*
{
int IComparer.Compare(object x, object y)
{
Student a = (Student)x;
Student b = (Student)y;
return string.Compare(a.Name, b.Name);
}
}
}