SCUT软件2018年机试第一题练习, C#实现

博客参考师兄博客,介绍scut2018年机试题。新建Student和Course类保存学生与课程信息,用泛型List<Course>存课程列表,让Student类实现泛型IComparable<Student>接口以按学号降序排列。还提到从test.txt读取信息写入t.xml,XML操作可参考相关文档。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题参见师兄的博客《scut2018年机试第一题》, 不再赘述

首先新建两个类, Student与Course, 分别保存学生与课程的信息

using System;
using System.Collections.Generic;

namespace ConsoleApp14
{
    class Student : IComparable<Student>
    {
        public int StuNum { get; set; }
        public string Name { get; set; }
        public List<Course> Courses { get; set; }
        public Student(int num, string name)
        {
            this.StuNum = num;
            this.Name = name;
            this.Courses = new List<Course>();
        }
        public void AddCourse(string courseName, int courseMark)
        {
            this.Courses.Add(new Course(courseName, courseMark));
        }
        public int CompareTo(Student otherStu)
        {
            return this.StuNum.CompareTo(otherStu.StuNum);
        }
    }
    class Course : IComparable<Course>
    {
        public string CourseName { get; set; }
        public int CourseMark { get; set; }
        public Course(string courseName,int courseMark)
        {
            this.CourseName = courseName;
            this.CourseMark = courseMark;
        }
        public int CompareTo(Course other)
        {
            return this.CourseMark.CompareTo(other.CourseMark);
        }
    }
}

显然, 每个学生都可对应于多个课程, 故用泛型List<Course>来保存课程列表
由于题目中要求对学生按学号降序排列, 故令Student类实现泛型IComparable<Student>接口
Course的泛型IComparable<Course>接口在本题中没有用, 顺手写的

下面是主体部分

using System;
using System.IO;
using System.Collections.Generic;
using System.Xml;

namespace ConsoleApp14
{
    class Program
    {
        static void Main(string[] args)
        {
            StreamReader sr = new StreamReader("test.txt");
            List<string[]> processed = new List<string[]>();
            while (sr.EndOfStream == false)
            {
                string line = sr.ReadLine();
                string[] tmp = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                processed.Add(tmp);
            }            
            List<Student> stuList = new List<Student>();
            foreach (string[] info in processed)
            {
                Student tmpStudent;
                if (FindStudent(stuList, int.Parse(info[0]), out tmpStudent))
                {
                    tmpStudent.AddCourse(info[2],int.Parse(info[3]));
                }
                else
                {
                    tmpStudent = new Student(int.Parse(info[0]), info[1]);
                    tmpStudent.AddCourse(info[2], int.Parse(info[3]));
                    stuList.Add(tmpStudent);
                }
            }
            stuList.Sort();
            stuList.Reverse();            
            //到此, student信息已经完全录入完毕, 并且按照学号逆序排列, 下面开始创建XML的工作
            OutputXML(stuList);
        }
        static bool FindStudent(List<Student> stuList, int stuNum, out Student student)
        {
            student = null;
            foreach (Student stu in stuList)
            {
                if (stu.StuNum == stuNum)
                {
                    student = stu;
                    return true;
                }
            }
            return false;
        }
        static void OutputXML(List<Student> students)
        {
            XmlDocument xml = new XmlDocument();
            XmlDeclaration declaration = xml.CreateXmlDeclaration("1.0", "utf-8",string.Empty);
            xml.AppendChild(declaration);
            XmlElement root = xml.CreateElement("students");
            xml.AppendChild(root);
            foreach (var stu in students)
            {
                XmlElement stunode = xml.CreateElement("student");
                root.AppendChild(stunode);
                XmlElement num = xml.CreateElement("学号");
                num.InnerText = stu.StuNum.ToString();
                stunode.AppendChild(num);
                XmlElement name = xml.CreateElement("姓名");
                name.InnerText = stu.Name;
                stunode.AppendChild(name);
                foreach (var course in stu.Courses)
                {
                    XmlElement cnode = xml.CreateElement("课程");
                    cnode.InnerText = course.CourseMark.ToString();
                    cnode.SetAttribute("课程名",course.CourseName);
                    stunode.AppendChild(cnode);
                }
            }
            xml.Save("t.xml");
        }
    }
}


从根目录的test.txt中读取信息, 并写入t.xml中, 可自行更改读入文件与写入文件的路径及文件名
XML部分可参见《C# 实现对XML文件的基本操作》或者微软官方的在线文档

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值