C# 学生成绩管理系统 完整版

知识点

一种类似指针的数据结构?
文件的读取和写入 避免乱码 ansi
字符串读取后分割的bug 可能是因为编码问题导致的 为了解决这个bug 写入的时候直接用\r\n分割了 但是解决乱码问题之后可能就没有这个bug了 以后可以用\t或者space分割试试

完全面向对象用起来不是很习惯,定义全局变量都要放在一个单独的类里
窗体之间的交互也不是很会 尤其是排序那部分 每一种排序方式都是复制粘贴的 除了那个下拉菜单以外(用的switch)有没有可能定义一个全局函数?

效果

C# 学生成绩管理系统 完整版_第1张图片
其他窗体的图懒得帖了,等完善总分平均分那部分之后一起吧

代码

program.cs

/*                               学生成绩管理系统
 * 待添加功能:
 * 
 * 按照姓名查找学生
 * 添加时学号重复性检查
 * 一键生成学生、分数,学号重复性检查,自动添加
 * 排序方式下拉菜单
 * 注册账户,登录密码(有点鸡肋)
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace C_sharp学生管理系统
{
    public class LinkList
    {
        public Node Head { set; get; } //单链表头
        public int total { set; get; } //总人数

        //构造
        public LinkList()
        {
            Head = null;
            total = 0;
        }

        //增加新元素到单链表末尾
        public static void Append(int id, string name, int classnum, int chinese, int math, int english, int physics, int politics, int sports)
        {
            Node foot = new Node(id, name, classnum, chinese, math, english, physics, politics, sports);
            Node A = new Node(id, name, classnum, chinese, math, english, physics, politics, sports);
            
            if (PublicValue.Head == null)
            {
                PublicValue.Head = foot;
                return;
            }
            A = PublicValue.Head;
            while (A.Next != null)
            {
                A = A.Next;
            }
            A.Next = foot;
        }
    }

    //全局变量
    public class PublicValue
    {
        public static Node Head; //单链表头
        public static Node Tail; //单链表尾
        public static int total; //学生总数
    }

    static class Program
    {
        //应用程序的主入口点
        [STAThread]
        static void Main()
        {
            LinkList link = new LinkList();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }
}

mainform.cs

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;
using System.IO;
using System.Text;

namespace C_sharp学生管理系统
{

    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        //显示学生名单:从head到null遍历
        private void button1_Click(object sender, EventArgs e)
        {
            Node B = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            B = PublicValue.Head;

            textBox1.Text = "";//先清空
            textBox1.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n";
            while (B != null)
            {
                textBox1.Text += B.id + "\t" + B.name + "\t" + B.classnum + "\t" + B.chinese + "\t" + B.math + "\t" + B.english + "\t" + B.physics + "\t" + B.politics + "\t" + B.sports + "\t" + B.total+ "\t" + B.average.ToString("f2") + "\r\n";
                B = B.Next;
            }
        }
        //添加学生
        private void add_Click(object sender, EventArgs e)
        {
            AddStuForm f2 = new AddStuForm();
            f2.ShowDialog();
            button1_Click(null, null);
        }
        //修改学生
        private void change_Click(object sender, EventArgs e)
        {
            ChangeStuForm f3 = new ChangeStuForm();
            f3.ShowDialog();
            button1_Click(null, null);
        }
        //删除学生
        private void delete_Click(object sender, EventArgs e)
        {
            DelStuForm f4 = new DelStuForm();
            f4.ShowDialog();
            button1_Click(null, null);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            SortForm f5 = new SortForm();
            f5.ShowDialog();
            button1_Click(null, null);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            FindForm f6 = new FindForm();
            f6.ShowDialog();
        }

        private void button6_Click(object sender, EventArgs e)
        {
            //清空txt
            System.IO.File.WriteAllText(@"data.txt", string.Empty);

            //写入
            StreamWriter sw = new StreamWriter("data.txt", true, Encoding.Default);
            Node B = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            B = PublicValue.Head;

            sw.Write(PublicValue.total + "\r\n");
            while (B != null)
            {
                sw.Write(B.id + "\r\n" + B.name + "\r\n" + B.classnum + "\r\n" + B.chinese + "\r\n" + B.math + "\r\n" + B.english + "\r\n" + B.physics + "\r\n" + B.politics + "\r\n" + B.sports + "\r\n");
                B = B.Next;
            }

            //清空缓冲区
            sw.Flush();

            //关闭流
            sw.Close();

            MessageBox.Show("保存成功");
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int i;

            FileStream fs = new FileStream("data.txt", FileMode.Open);
            StreamReader sr = new StreamReader(fs, Encoding.Default);

            //临时接收
            int id;
            string name;
            int classnum;
            int chinese;
            int math;
            int english;
            int physics;
            int politics;
            int sports;

            PublicValue.total = int.Parse(sr.ReadLine());

            for (i = 0; i < PublicValue.total; i++)
            {
                id = int.Parse(sr.ReadLine());
                name = sr.ReadLine();
                classnum = int.Parse(sr.ReadLine());

                chinese = int.Parse(sr.ReadLine());
                math = int.Parse(sr.ReadLine());
                english = int.Parse(sr.ReadLine());
                physics = int.Parse(sr.ReadLine());
                politics = int.Parse(sr.ReadLine());
                sports = int.Parse(sr.ReadLine());

                LinkList.Append(id, name, classnum, chinese, math, english, physics, politics, sports);

            }
            sr.Close();
            MessageBox.Show("导入成功");
            button1_Click(null, null);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";//先清空
            textBox1.Text += "                          学生成绩管理系统\r\n\r\n";
            textBox1.Text += " by 寒泉hq\r\n\r\n";
            textBox1.Text += " 欢迎关注我的CSDN博客\r\n\r\n";
            textBox1.Text += " https://blog.csdn.net/sinat_42483341\r\n\r\n";
            textBox1.Text += " 仅供学习使用 保留所有权利\r\n\r\n";
            textBox1.Text += " 2019.03.10\r\n\r\n";
        }

        private void button7_Click(object sender, EventArgs e)
        {
            button6_Click(null, null);
            System.Environment.Exit(0);
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

        }
    }

    //一个节点
    public class Node
    {
        //数据域,当前结点数据

        //信息
        public int id;
        public string name;
        public int classnum;

        //分数
        public int chinese { set; get; }
        public int math { set; get; }
        public int english { set; get; }
        public int physics { set; get; }
        public int politics { set; get; }
        public int sports { set; get; }

        public double total { set; get; }
        public double average { set; get; }

        //指针   
        public Node Next { set; get; }    //位置域,下一个结点地址

        //构造函数
        public Node(int id, string name, int classnum, int chinese, int math, int english, int physics, int politics, int sports)
        {
            this.id = id;
            this.name = name;
            this.classnum = classnum;
            this.chinese = chinese;
            this.math = math;
            this.english = english;
            this.physics = physics;
            this.politics = politics;
            this.sports = sports;

            this.total = this.chinese + this.math + this.english + this.physics + this.politics + this.sports;
            this.average = this.total / 6;

            this.Next = null;
        }
    }
}

sortform.cs

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 C_sharp学生管理系统
{
    public partial class SortForm : Form
    {
        public SortForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Node temp = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            Node left = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node right = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node rightmin = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);

            left = PublicValue.Head;
            right = PublicValue.Head;

            if (left == null)
            {
                MessageBox.Show("无法排序,请先存入数据");
            }
            else
            {
                for (; left != null; left = left.Next)//最大的数放在左边
                {
                    right = left;
                    for (rightmin = right; right != null; right = right.Next)//从右边找出最小的数,用prightmax记录其位置
                    {
                        if (right.id < rightmin.id)
                        {
                            rightmin = right;
                        }
                    }
                    //复制粘贴好几遍,有类似于#define的使用方式吗?
                    temp.id = rightmin.id;
                    temp.name = rightmin.name;
                    temp.classnum = rightmin.classnum;
                    temp.chinese = rightmin.chinese;
                    temp.math = rightmin.math;
                    temp.english = rightmin.english;
                    temp.physics = rightmin.physics;
                    temp.politics = rightmin.politics;
                    temp.sports = rightmin.sports;
                    temp.total = rightmin.total;//补充
                    temp.average = rightmin.average;//补充

                    rightmin.id = left.id;
                    rightmin.name = left.name;
                    rightmin.classnum = left.classnum;
                    rightmin.chinese = left.chinese;
                    rightmin.math = left.math;
                    rightmin.english = left.english;
                    rightmin.physics = left.physics;
                    rightmin.politics = left.politics;
                    rightmin.sports = left.sports;
                    rightmin.total = left.total;//补充
                    rightmin.average = left.average;//补充


                    left.id = temp.id;
                    left.name = temp.name;
                    left.classnum = temp.classnum;
                    left.chinese = temp.chinese;
                    left.math = temp.math;
                    left.english = temp.english;
                    left.physics = temp.physics;
                    left.politics = temp.politics;
                    left.sports = temp.sports;
                    left.total = temp.total;//补充
                    left.average = temp.average;//补充
                }

                //输出
                MessageBox.Show("排序成功");
                Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值

                textBox1.Text = "";//先清空
                textBox1.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n";
               
                cur = PublicValue.Head;

                while (cur != null)
                {
                    textBox1.Text += cur.id + "\t" + cur.name + "\t" + cur.classnum + "\t" + cur.chinese + "\t" + cur.math + "\t" + cur.english + "\t" + cur.physics + "\t" + cur.politics + "\t" + cur.sports+ "\t"+cur.total + "\t" + cur.average.ToString("f2") + "\r\n";
                    cur = cur.Next;
                }
            }
        }

        //按班级排序
        private void button2_Click(object sender, EventArgs e)
        {
            Node temp = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            Node left = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node right = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node rightmin = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);

            left = PublicValue.Head;
            right = PublicValue.Head;

            if (left == null)
            {
                MessageBox.Show("无法排序,请先存入数据");
            }
            else
            {
                for (; left != null; left = left.Next)//最大的数放在左边
                {
                    right = left;
                    for (rightmin = right; right != null; right = right.Next)//从右边找出最小的数,用prightmax记录其位置
                    {
                        if (right.classnum < rightmin.classnum)
                        {
                            rightmin = right;
                        }
                    }
                    //复制粘贴好几遍,有类似于#define的使用方式吗?
                    temp.id = rightmin.id;
                    temp.name = rightmin.name;
                    temp.classnum = rightmin.classnum;
                    temp.chinese = rightmin.chinese;
                    temp.math = rightmin.math;
                    temp.english = rightmin.english;
                    temp.physics = rightmin.physics;
                    temp.politics = rightmin.politics;
                    temp.sports = rightmin.sports;
                    temp.total = rightmin.total;//补充
                    temp.average = rightmin.average;//补充

                    rightmin.id = left.id;
                    rightmin.name = left.name;
                    rightmin.classnum = left.classnum;
                    rightmin.chinese = left.chinese;
                    rightmin.math = left.math;
                    rightmin.english = left.english;
                    rightmin.physics = left.physics;
                    rightmin.politics = left.politics;
                    rightmin.sports = left.sports;
                    rightmin.total = left.total;//补充
                    rightmin.average = left.average;//补充


                    left.id = temp.id;
                    left.name = temp.name;
                    left.classnum = temp.classnum;
                    left.chinese = temp.chinese;
                    left.math = temp.math;
                    left.english = temp.english;
                    left.physics = temp.physics;
                    left.politics = temp.politics;
                    left.sports = temp.sports;
                    left.total = temp.total;//补充
                    left.average = temp.average;//补充
                }

                //输出
                MessageBox.Show("排序成功");
                Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值

                textBox1.Text = "";//先清空
                textBox1.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n";

                cur = PublicValue.Head;

                while (cur != null)
                {
                    textBox1.Text += cur.id + "\t" + cur.name + "\t" + cur.classnum + "\t" + cur.chinese + "\t" + cur.math + "\t" + cur.english + "\t" + cur.physics + "\t" + cur.politics + "\t" + cur.sports + "\t" + cur.total + "\t" + cur.average.ToString("f2") + "\r\n";
                    cur = cur.Next;
                }
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //按总分排序
        private void button3_Click(object sender, EventArgs e)
        {
            Node temp = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            Node left = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node right = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node rightmin = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);

            left = PublicValue.Head;
            right = PublicValue.Head;

            if (left == null)
            {
                MessageBox.Show("无法排序,请先存入数据");
            }
            else
            {
                for (; left != null; left = left.Next)//最大的数放在左边
                {
                    right = left;
                    for (rightmin = right; right != null; right = right.Next)//从右边找出最小的数,用prightmax记录其位置
                    {
                        if (right.total > rightmin.total)//从大到小 其他排序方式只改这一行即可
                        {
                            rightmin = right;
                        }
                    }
                    //复制粘贴好几遍,有类似于#define的使用方式吗?
                    temp.id = rightmin.id;
                    temp.name = rightmin.name;
                    temp.classnum = rightmin.classnum;
                    temp.chinese = rightmin.chinese;
                    temp.math = rightmin.math;
                    temp.english = rightmin.english;
                    temp.physics = rightmin.physics;
                    temp.politics = rightmin.politics;
                    temp.sports = rightmin.sports;
                    temp.total = rightmin.total;//补充
                    temp.average = rightmin.average;//补充

                    rightmin.id = left.id;
                    rightmin.name = left.name;
                    rightmin.classnum = left.classnum;
                    rightmin.chinese = left.chinese;
                    rightmin.math = left.math;
                    rightmin.english = left.english;
                    rightmin.physics = left.physics;
                    rightmin.politics = left.politics;
                    rightmin.sports = left.sports;
                    rightmin.total = left.total;//补充
                    rightmin.average = left.average;//补充


                    left.id = temp.id;
                    left.name = temp.name;
                    left.classnum = temp.classnum;
                    left.chinese = temp.chinese;
                    left.math = temp.math;
                    left.english = temp.english;
                    left.physics = temp.physics;
                    left.politics = temp.politics;
                    left.sports = temp.sports;
                    left.total = temp.total;//补充
                    left.average = temp.average;//补充
                }

                //输出
                MessageBox.Show("排序成功");
                Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值

                textBox1.Text = "";//先清空
                textBox1.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n";

                cur = PublicValue.Head;

                while (cur != null)
                {
                    textBox1.Text += cur.id + "\t" + cur.name + "\t" + cur.classnum + "\t" + cur.chinese + "\t" + cur.math + "\t" + cur.english + "\t" + cur.physics + "\t" + cur.politics + "\t" + cur.sports + "\t" + cur.total + "\t" + cur.average.ToString("f2") + "\r\n";
                    cur = cur.Next;
                }
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            int choose=-1;//语文 数学 英语 物理 政治 体育

            if (comboBox1.Text == "语文") choose = 0;
            else if (comboBox1.Text == "数学") choose = 1;
            else if (comboBox1.Text == "英语") choose = 2;
            else if (comboBox1.Text == "物理") choose = 3;
            else if (comboBox1.Text == "政治") choose = 4;
            else if (comboBox1.Text == "体育") choose = 5;

            //排序
            Node temp = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            Node left = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node right = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);
            Node rightmin = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);

            left = PublicValue.Head;
            right = PublicValue.Head;

            if (left == null)
            {
                MessageBox.Show("无法排序,请先存入数据");
            }
            else
            {
                for (; left != null; left = left.Next)//最大的数放在左边
                {
                    right = left;
                    for (rightmin = right; right != null; right = right.Next)//从右边找出最小的数,用prightmax记录其位置
                    {
                        switch(choose)
                        {
                            case -1:
                                MessageBox.Show("出错了!choose=-1 没有选择");
                                break;
                            case 0:
                                if (right.chinese > rightmin.chinese)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                            case 1:
                                if (right.math > rightmin.math)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                            case 2:
                                if (right.english > rightmin.english)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                            case 3:
                                if (right.physics > rightmin.physics)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                            case 4:
                                if (right.politics > rightmin.politics)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                            case 5:
                                if (right.sports > rightmin.sports)//从大到小
                                {
                                    rightmin = right;
                                }
                                break;
                        }
                        
                    }
                    //复制粘贴好几遍,有类似于#define的使用方式吗?
                    temp.id = rightmin.id;
                    temp.name = rightmin.name;
                    temp.classnum = rightmin.classnum;
                    temp.chinese = rightmin.chinese;
                    temp.math = rightmin.math;
                    temp.english = rightmin.english;
                    temp.physics = rightmin.physics;
                    temp.politics = rightmin.politics;
                    temp.sports = rightmin.sports;
                    temp.total = rightmin.total;//补充
                    temp.average = rightmin.average;//补充

                    rightmin.id = left.id;
                    rightmin.name = left.name;
                    rightmin.classnum = left.classnum;
                    rightmin.chinese = left.chinese;
                    rightmin.math = left.math;
                    rightmin.english = left.english;
                    rightmin.physics = left.physics;
                    rightmin.politics = left.politics;
                    rightmin.sports = left.sports;
                    rightmin.total = left.total;//补充
                    rightmin.average = left.average;//补充


                    left.id = temp.id;
                    left.name = temp.name;
                    left.classnum = temp.classnum;
                    left.chinese = temp.chinese;
                    left.math = temp.math;
                    left.english = temp.english;
                    left.physics = temp.physics;
                    left.politics = temp.politics;
                    left.sports = temp.sports;
                    left.total = temp.total;//补充
                    left.average = temp.average;//补充
                }

                //输出
                MessageBox.Show("排序成功");
                Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值

                textBox1.Text = "";//先清空
                textBox1.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n";

                cur = PublicValue.Head;

                while (cur != null)
                {
                    textBox1.Text += cur.id + "\t" + cur.name + "\t" + cur.classnum + "\t" + cur.chinese + "\t" + cur.math + "\t" + cur.english + "\t" + cur.physics + "\t" + cur.politics + "\t" + cur.sports + "\t" + cur.total + "\t" + cur.average.ToString("f2") + "\r\n";
                    cur = cur.Next;
                }
            }

        }
    }
}

findform.cs

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 C_sharp学生管理系统
{
    public partial class FindForm : Form
    {
        public FindForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //查找学生
        private void button2_Click(object sender, EventArgs e)
        {
            Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            cur = PublicValue.Head;

            textBox2.Text = "";//先清空
            textBox2.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\t总分\t平均分\r\n\r\n";

            int findId = int.Parse(textBox1.Text);

            while (cur != null)
            {
                if (cur.id == findId)
                {
                    textBox2.Text += cur.id + "\t";
                    textBox2.Text += cur.name + "\t";
                    textBox2.Text += cur.classnum + "\t";
                    textBox2.Text += cur.chinese + "\t";
                    textBox2.Text += cur.math + "\t";
                    textBox2.Text += cur.english + "\t";
                    textBox2.Text += cur.physics + "\t";
                    textBox2.Text += cur.politics + "\t";
                    textBox2.Text += cur.sports + "\t";

                    textBox2.Text += cur.total + "\t";
                    textBox2.Text += cur.average.ToString("f2") ;
                    break;
                }
                cur = cur.Next;
            }
            if (cur == null)
            {
                MessageBox.Show("你输入的学号不存在!");
            }
        }
    }
}

delstuform.cs

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 C_sharp学生管理系统
{
    public partial class DelStuForm : Form
    {
        public DelStuForm()
        {
            InitializeComponent();
        }

        private void Form4_Load(object sender, EventArgs e)
        {

        }

        //删除 点击查找信息
        private void button1_Click(object sender, EventArgs e)
        {
            Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            cur = PublicValue.Head;

            textBox2.Text = "";//先清空
            textBox2.Text += "学号\t姓名\t班级\t语文\t数学\t英语\t物理\t政治\t体育\r\n\r\n";

            int findId = int.Parse(textBox1.Text);
            while (cur != null)
            {
                if (cur.id == findId)
                {
                    textBox2.Text += cur.id + "\t";
                    textBox2.Text += cur.name + "\t";
                    textBox2.Text += cur.classnum + "\t";
                    textBox2.Text += cur.chinese + "\t";
                    textBox2.Text += cur.math + "\t";
                    textBox2.Text += cur.english + "\t";
                    textBox2.Text += cur.physics + "\t";
                    textBox2.Text += cur.politics + "\t";
                    textBox2.Text += cur.sports;
                    break;
                }
                cur = cur.Next;
            }
            if (cur == null)
            {
                MessageBox.Show("你输入的学号不存在!");
            }
        }

        //删除学生
        private void button2_Click(object sender, EventArgs e)
        {
            Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//大哥
            Node last = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//小弟

            cur = PublicValue.Head;
            last = cur;

            int findId = int.Parse(textBox1.Text);

            //查找
            while (cur != null)
            {
                if (cur.id == findId)//如果找到了
                {
                    PublicValue.total--;

                    if (cur == PublicValue.Head)//如果删除的是头节点
                    {
                        PublicValue.Head = PublicValue.Head.Next;
                        MessageBox.Show("删除成功");
                        return;
                    }
                    else
                    {
                        last.Next = cur.Next;//垃圾回收机制?不需要自己清理内存?
                        MessageBox.Show("删除成功");
                        return;
                    }
                }
                last = cur;//小弟踩大哥脚印
                cur = cur.Next;//大哥先走一步
            }

            if (cur == null)
            {
                MessageBox.Show("你删除的学号不存在!");
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

changestuform.cs

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 C_sharp学生管理系统
{
    public partial class ChangeStuForm : Form
    {
        public ChangeStuForm()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {

        }

        //输入学号,点击确定
        private void button1_Click(object sender, EventArgs e)
        {
            //遍历查找
            Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            cur = PublicValue.Head;//从头开始查找
            int findId = int.Parse(textBox1.Text);
            while (cur != null)
            {
                if (cur.id == findId)
                {
                    textBox2.Text = cur.name;
                    textBox3.Text = Convert.ToString(cur.classnum);
                    textBox4.Text = Convert.ToString(cur.chinese);
                    textBox5.Text = Convert.ToString(cur.math);
                    textBox6.Text = Convert.ToString(cur.english);
                    textBox7.Text = Convert.ToString(cur.physics);
                    textBox8.Text = Convert.ToString(cur.politics);
                    textBox9.Text = Convert.ToString(cur.sports);
                    break;
                }
                cur = cur.Next;
            }
            if (cur == null)
            {
                MessageBox.Show("你输入的学号不存在!");
            }
        }
        //修改后点击确定
        private void button2_Click(object sender, EventArgs e)
        {
            //迷之再次遍历查找
            Node cur = new Node(0, "0", 0, 0, 0, 0, 0, 0, 0);//毫无意义的赋值
            cur = PublicValue.Head;//从头开始查找
            int findId = int.Parse(textBox1.Text);
            while (cur != null)
            {
                if (cur.id == findId)
                {
                    cur.name = textBox2.Text;
                    cur.classnum = int.Parse(textBox3.Text);
                    cur.chinese = int.Parse(textBox4.Text);
                    cur.math = int.Parse(textBox5.Text);
                    cur.english = int.Parse(textBox6.Text);
                    cur.physics = int.Parse(textBox7.Text);
                    cur.politics = int.Parse(textBox8.Text);
                    cur.sports = int.Parse(textBox9.Text);

                    cur.total = cur.chinese + cur.math + cur.english + cur.physics + cur.politics + cur.sports;
                    cur.average = cur.total / 6;

                    MessageBox.Show("修改成功");
                    break;
                }
                cur = cur.Next;
            }
            if (cur == null)
            {
                MessageBox.Show("修改失败,你输入的学号不存在!请不要改变刚才输入的学号");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

addstuform.cs

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 C_sharp学生管理系统
{
    public partial class AddStuForm : Form
    {
        public AddStuForm()
        {
            InitializeComponent();
        }

        /// 
        /// 点击"添加"按钮
        /// 增加新元素到单链表末尾
        /// 
        public void button1_Click(object sender, EventArgs e)
        {
            //临时接收
            int id;
            string name;
            int classnum;

            //分数
            int chinese;
            int math;
            int english;
            int physics;
            int politics;
            int sports;

            id = int.Parse(idnum.Text);
            name = textBox2.Text;
            classnum = int.Parse(textBox3.Text);

            chinese = int.Parse(textBox4.Text);
            math = int.Parse(textBox5.Text);
            english = int.Parse(textBox6.Text);
            physics = int.Parse(textBox7.Text);
            politics = int.Parse(textBox8.Text);
            sports = int.Parse(textBox8.Text);

            LinkList.Append(id, name, classnum, chinese, math, english, physics, politics, sports);
            PublicValue.total++;
            MessageBox.Show("添加成功");
        }

        //自动填充表单
        private void button2_Click(object sender, EventArgs e)
        {
            Random rd = new Random();

            if (Convert.ToString(idnum.Text) == "")
            {
                idnum.Text = Convert.ToString(1);
            }
            else
            {
                idnum.Text = Convert.ToString(1 + int.Parse(idnum.Text));//学号自动顺延
            }

            textBox2.Text = "自动";
            textBox3.Text = Convert.ToString(rd.Next(1, 10));

            textBox4.Text = Convert.ToString(rd.Next(50, 100));
            textBox5.Text = Convert.ToString(rd.Next(50, 100));
            textBox6.Text = Convert.ToString(rd.Next(50, 100));
            textBox7.Text = Convert.ToString(rd.Next(50, 100));
            textBox8.Text = Convert.ToString(rd.Next(50, 100));
            textBox9.Text = Convert.ToString(rd.Next(50, 100));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

你可能感兴趣的:(C#)