C#程序设计经典教程(第三版)实验六

注意:C#语言刚入门,代码质量不高!!!可以参考

1.设计一个 Windows 应用程序,定义一个 Teacher 类,包含姓名和职称两个字段和一个输出自己信息的方法,并用 ArrayList 实现与例 6-1 相同的功能。

控件名称如下:

代码如下:

using System;
using System.Collections;
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 实验六
{
    public partial class Form1 : Form
    {
        ArrayList a = new ArrayList();
        public Form1()
        {
            InitializeComponent();
        }
        private void display()
        {
            foreach(Teacher t in a){
                label4.Text += "\n"+t.ShowMsg();
            }
        } 
        
        private void button1_Click(object sender, EventArgs e)//末尾插入
        {
            Teacher t = new Teacher(textBox2.Text,textBox3.Text);
            a.Add(t);
            label4.Text = "";
            display();
        }

        private void button2_Click(object sender, EventArgs e)//指定位置插入
        {
            int index = Convert.ToInt32(textBox1.Text);
            Teacher t = new Teacher(textBox2.Text, textBox3.Text);
            a.Insert(index-1, t);
            label4.Text = "";
            display();
        }

        private void button3_Click(object sender, EventArgs e)//遍历
        {
            label4.Text = "";
            display();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(textBox1.Text);
            a.RemoveAt(index-1);
            label4.Text = "";
            display();
        }
    }
    class Teacher
    {
        string name;
        string job;
        public Teacher(string name, string job)
        {
            this.name = name;
            this.job = job;
        }
        public string ShowMsg()
        {
            return string.Format("姓名:{0},职称:{1}", name, job);
        }
    }
}

运行结果(个人建议,还是要看懂代码,可以自行修改)

2.设计一个 Windows 应用程序,定义一个 Student类,包含学号和姓名两个字段,并定义一个班级类 ClassList,该类包括一个 Student 集合,使用索引器访问该集合,实现与例6-3类似的功能。

控件名称:

代码如下:

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolTip;

namespace 实验六
{
    public partial class Form2 : Form
    {
        ClassList cList = new ClassList();
        int size = 0;
        public Form2()
        {
            InitializeComponent();
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student s = new Student(Convert.ToInt32(textBox1.Text),textBox2.Text);
            int index = 1;
            if (!textBox3.Text.Equals(""))
            {
                index = Convert.ToInt32(textBox3.Text);
            }
            if (index-1 > size)
            {
                label5.Text = "非法值";
            }
            else
            {
                cList[index-1] = s;
                size++;
                label5.Text = "";
                label4.Text = "";
                display();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int index = Convert.ToInt32(textBox3.Text);
            if (index - 1 > size)
            {
                label5.Text = "非法值";
            }
            else
            {
                Student s = cList[index - 1];
                label5.Text = "";
                label4.Text = "";
                label4.Text = s.ShowMsg();
            }
            
        }
        private void display()
        {
            foreach(Student s in cList.getStudList())
            {
                label4.Text += "\n" + s.ShowMsg();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (textBox4.Text.Equals(""))
            {
                label5.Text = "请输入学生学号";
                return;
            }
            int StudNo = Convert.ToInt32(textBox4.Text);
            label4.Text = "";
            foreach(Student s in cList.getStudList())
            {
                if (s.No == StudNo)
                {
                label4.Text += "\n" + s.ShowMsg();
                    return;
                }
            }
            label4.Text += "没有该学生";
        }

        private void label3_Click(object sender, EventArgs e)
        {

        }
    }

    class Student
    {
        private int stuNo;
        private string stuName;

        public int No
        {
            get => stuNo;
            set => stuNo = value;
        }
        public string Name
        {
            get => stuName;
            set => stuName = value;
        }
        public Student(int noo,string name)
        {
            stuNo = noo;
            stuName = name;
        }

        public string ShowMsg()
        {
            return string.Format("学号:{0},姓名:{1}", stuNo, stuName);
        }

    }
    class ClassList
    {
        ArrayList Stus;
        public ClassList()
        {
            Stus = new ArrayList(100);
        }

        public ArrayList getStudList()
        {
            return Stus; 
        }
        public Student this[int index]
        {
            get{
                if (index < 0)
                {
                    return null;
                }
                return (Student)Stus[index];
            }
            set
            {
                if (index < 0)
                {
                    return;
                }
                Stus.Insert(index, value);
            }
        }
    }
}

运行结果如下:添加和显示按钮功能,可以在textbox3文本框中输入制定的索引值,比如下面的图文本框中输入2,点击添加按钮,王流这个信息插入到第二个位置中。


3.设计一个 Windows 应用程序,要求如下
来那的价
(1) 构造一个学生基本类。
效果
(2) 分别构造小学生、中学生、大学生等派生类,要求具有不同的特征和行为。(3)定义一个泛型班级类,约束参数类型为学生类,该泛型班级类包括一个泛型集合,用于存放各种学生对象,并包含一个方法用于输出每个学生的相关信息。

(4)仿照例 6-5,定义泛型的班级类对象,完成对学生的添加和信息的输出。

控件名称如下:

代码如下

using System;
using System.Collections;
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 实验六
{
    public partial class Form3 : Form
    {
        Class<student> mystudents = new Class<student>();
        public Form3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Pupil p = new Pupil(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToDouble(textBox3.Text));
            mystudents.List.Add(p);

        }

        private void button2_Click(object sender, EventArgs e)
        {
            junior j = new junior(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text),
                 Convert.ToDouble(textBox5.Text));
            mystudents.List.Add(j);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            College c = new College(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToDouble(textBox3.Text), Convert.ToDouble(textBox4.Text),
                 Convert.ToDouble(textBox5.Text),textBox6.Text);
            mystudents.List.Add(c);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            label1.Text = "";
            label1.Text = mystudents.getAllStudentInformation();
        }
    }
    class student
    {
        private int StuNo;
        private string StuName;
        public student(int no, string name)
        {
            this.StuNo = no;
            this.StuName = name;
        }
        public string Name
        {
            get => StuName;
            set => StuName = value;
        }
        public int No
        {
            get => StuNo;
            set => StuNo = value;
        }
        public virtual string action()
        {
            return string.Format("学生进行活动");
        }
        public virtual string information()
        {
            return string.Format("学生信息");
        }
    }
    class Pupil : student
    {
        private double yuwen;

        public Pupil(int no,string name,double yuwen) : base(no, name)
        {
            this.yuwen = yuwen;
        }
        public override string action()
        {
            return string.Format("正在玩耍");
        }
        public override string information()
        {
            return string.Format("学号是{0},我的名字:{1}、身份是:小学生、语文成绩{2}", No,Name, yuwen);
        }
    }
    class junior : student
    {
        private double yuwen;
        private double shuxue;
        private double english;

        public junior(int no, string name,double yuwen, double shuxue,double english) : base(no, name)
        {
            this.yuwen = yuwen;
            this.shuxue = shuxue;
            this.english = english;
        }
        public override string action()
        {
            return string.Format("正在复习资料");
        }
        public override string information()
        {
            return string.Format("学号是{0},我的名字:{1}、身份是中学生、语文成绩{2},数学成绩是{3},英语成绩是{4}", No,Name, yuwen,shuxue,english);
        }
    }
    class College : student
    {
        private double yuwen;
        private double shuxue;
        private double english;
        private string job;

        public College(int no, string name,double yuwen, double shuxue, double english,string job) : base(no, name)
        {
            this.yuwen = yuwen;
            this.shuxue = shuxue;
            this.english = english;
            this.job = job;
        }
        public override string action()
        {
            return string.Format("正在参加志愿活动");
        }
        public override string information()
        {
            return string.Format("学号是{0},我的名字:{1}、身份是大学生、语文成绩{2},数学成绩是{3},英语成绩是{4},\n工作职称{5}", No, Name, yuwen, shuxue, english,job);
        }
    }
    class Class<T> where T: student
    {
        List<T> S_List = new List<T>(100);
        public List<T> List
        {
            get => S_List;
        }
        public string  getAllStudentInformation()//获取所有同学的相关信息
        {
            string result = "";
            foreach(T t in S_List)
            {

                result += t.information()+"\n"+t.action()+"\n\n";
            }
            return result;
        }
    }
}

运行结果(注意:中学生和大学生的数学成绩和英语成绩必填,否则有异常,可以自行写代码检测)

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值