C# 学生信息管理

来源:互联网 发布:linux cp-a 编辑:程序博客网 时间:2024/06/06 11:02

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace ConsoleApp3{    class Program    {        static void Main(string[] args)        {            Student student1 = new Student();            Student student2 = new Student();            student2.S_no = 20151213;            student2.S_name = "小红";            student2.S_class = "计软1504";            student2.S_score = 500.2;            StudentInfo list = new StudentInfo(100,0);            Console.WriteLine("我们自动添加了第一个学生的信息");            list.AddInfo(student1);            list.showStudent(0);            Console.WriteLine("我们添加了第二个学生的信息");            list.AddInfo(student2);            list.showStudent(1);            Console.ReadLine();        }    }    class Student    {        private int s_no;        private string s_name;        private double s_score;        private string s_class;        public Student()        {            this.s_no = 201512;            this.s_name = "小明";            this.s_class = "计软1503";            this.s_score = 500;        }        //定义四个公有属性S_no,S_name,S_class以及S_score,分别用于封装对各字段读写访问        public int  S_no        {            get { return s_no; }            set { s_no = value; }        }        public string S_name        {            get { return s_name; }            set { s_name = value; }        }        public string S_class        {            get { return s_class; }            set { s_class = value; }        }        public double S_score        {            get { return s_score; }            set { s_score = value; }        }    }    class StudentInfo    {        Student[] m_list;        int m_maxcapacity;        int m_length;        public StudentInfo(int m_maxcapacity, int m_length)        {            this.m_maxcapacity = m_maxcapacity;            this.m_length = m_length;            this.m_list = new Student[m_maxcapacity];        }        public int Maxcapcity        {            get { return m_maxcapacity; }        }        public int Currentlength        {            get { return m_length; }        }        public int Restlength        {            get { return m_maxcapacity- m_length; }        }        public void showStudent(int index)        {            if(m_list[index]!=null)            {                Console.WriteLine("此学生信息为:\n姓名:{0}\n学号:{1}\n班级:{2}\n成绩:{3}", m_list[index].S_name, m_list[index].S_no, m_list[index].S_class, m_list[index].S_score);            }            else            {                Console.WriteLine("没有此学生。");            }        }        public bool AddInfo(Student student)//添加学生信息        {                       if(this.m_length>=m_maxcapacity)            {                Console.WriteLine("空间不足");            }            else            {                m_list[this.m_length] = student;                this.m_length++;                return true;            }            return false;        }        public bool  DeleteInfo(int number)//添加学生信息        {            int success = 0;            if(this.m_length<=0)            {                Console.WriteLine("没有学生信息,无法删除");            }            else            {               for(int i=0; i<this.m_length;i++)                {                    if(m_list[i].S_no== number)                    {                        for(int k = this.m_length; k>i;k++)                        {                            m_list[k - i] = m_list[k];                            success= 1;                            return true;                        }                    }                    else {                        success = 0;                    }                }            }                        return false;        }    }}

学生管理类

1)创建C#控制台应用程序L3_3

2)在程序中创建一个学生类Student,包含以下成员:

l  定义私有字段:学号s_no,姓名s_name,班级s_class,成绩s_score

l  定义Student类的构造函数,初始化4个私有字段。

l  定义四个公有属性S_noS_nameS_class以及S_score,分别用于封装对各字段读写访问。

3)再定义一个StudentInfo类,用于对学生信息进行管理:该类包括下列成员:

存放学生信息的Student[]类型的私有字段成员m_list

l 存储学生最大数量的私有字段m_maxcapacity

l 存储当前学生数量的私有字段m_length;

l 带参数的构造函数,根据指定长度对各私有字段进行初始化。

l 定义类的只读公有属性:Maxcapcity属性、Currentlength属性、 Restlength属性,分别用于返回列表的最大容量、已存信息的容量、剩余的最大容量。

l 定义一个索引函数,用于实现根据学生的学号对信息进行读、写访问。(无论读或写操作都要求先判断学生是否存在)。

l 定义公有方法AddInfo(返回类型为bool),用于向学生信息列表中添加学生信息(需要判断添加操作能否进行,即列表是否已满)。

l 定义一个公有方法DeleteInfo返回类型为bool),用于根据学生学号删除信息。(需要判断学生是否存在)

4)在外部类中进行StudentInfo类的各种功能进行测试。