原题参见师兄的博客《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文件的基本操作》或者微软官方的在线文档