0

我想我会发布我正在使用的整个程序。我已经基本完成了 Main 方法。我只需要创建 3 个类。文件例程、学生和学生。FileRoutine 是我遇到的主要问题。它有2个公共课程。LoadStudents 和 LoadAssignments。加载学生应该读取学生输入文件中的所有行,从每一行创建一个学生对象,并返回一个包含学生对象数组的学生对象。Load Assignments 应该读取作业输入文件中的所有行,并根据学生编号将每个作业与相应的学生相关联。2 个文本文件如下所示:

学生.txt

122338  Weltzer Teresa
123123  Wang    Choo
123131  Adams   James
123132  Binkley Joshua
123139  Howard  Tyler
123160  King    Alma    

作业.txt

122338  HW1 93  0.05
123123  HW1 100 0.05
123131  HW1 90  0.05
123132  HW1 85  0.05
123139  HW1 89  0.05
123160  HW1 90  0.05
122338  Quiz1   94  0.08
123123  Quiz1   83  0.08
123131  Quiz1   93  0.08
123132  Quiz1   72  0.08
123139  Quiz1   0   0.08
123160  Quiz1   63  0.08
122338  HW2 70  0.05
123123  HW2 95  0.05
123131  HW2 100 0.05
123132  HW2 82  0.05
123139  HW2 73  0.05
123160  HW2 81  0.05
122338  HW3 87  0.05
123123  HW3 98  0.05
123131  HW3 87  0.05
123132  HW3 65  0.05
123139  HW3 55  0.05
123160  HW3 81  0.05
122338  Test1   86  0.15
123123  Test1   82  0.15
123131  Test1   92  0.15
123132  Test1   78  0.15
123139  Test1   67  0.15
123160  Test1   74  0.15

我真的只是想弄清楚如何处理这两个文件,并让它们做我想做的事。我被告知使用

string[] lines = File.ReadAllLines(path)

阅读这些行,和

string[] data = line.Split('\t') 

拆分制表符分隔的行。

我不是要求有人为我编写这个程序。只是为了一些帮助。朝着正确的方向推进。我真的通过看到一些东西来学习,我还没有真正看到有人按照我被指示这样做的方式去做。如果大家有任何问题要问,请随时提出!

提前致谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace Project2
{
    class Program
    {
        static void Main()
        {
            // Change the path to match where you save the files.
            //const string STUDENT_FILE = @"C:\Temp\Students.txt";
            //const string ASSIGNMENT_FILE = @"C:\Temp\Assignments.txt";
            const string STUDENT_FILE = @"C:\Users\PleaseGod\Desktop\Project 2\Students.txt";
            const string ASSIGNMENT_FILE = @"C:\Users\PleaseGod\Desktop\Project 2\Assignments.txt";

            // First, load the student and assignment data from text files.
            Students students = FileRoutines.LoadStudents(STUDENT_FILE);
            FileRoutines.LoadAssignments(ASSIGNMENT_FILE, students);

            // Next, display information about the students and grades.
            Console.WriteLine("The students and their average are:");
            foreach (Student student in students.StudentList)
            {
                Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
            }

            DisplayStudentsByGrade(students, LetterGrade.A);
            DisplayStudentsByGrade(students, LetterGrade.B);
            DisplayStudentsByGrade(students, LetterGrade.C);
            DisplayStudentsByGrade(students, LetterGrade.D);
            DisplayStudentsByGrade(students, LetterGrade.F);

            Student[] studentsWithHighestAverage = students.GetStudentsWithHighestAverage();
            Console.WriteLine("\nThe students with the highest average are:");
            foreach (Student student in studentsWithHighestAverage)
            {
                Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
            }

            Console.WriteLine("\nThe class average is: {0:N2}", students.GetClassAverage());

            Console.WriteLine("\nThe class average on HW2 is: {0:N2}", students.GetAverageGradeOnAssignment("HW2"));

            Console.ReadLine();
        }

        private static void DisplayStudentsByGrade(Students students, LetterGrade letterGrade)
        {
            Student[] studentList = students.GetStudentsByGrade(letterGrade);

            Console.WriteLine("\nThe following students have a grade of {0}:", letterGrade);
            if (studentList.Length > 0)
            {
                foreach (Student student in studentList)
                {
                    Console.WriteLine("{0,-20}\t{1:N2}", student.FullName, student.GetAverage());
                }
            }
            else
            {
                Console.WriteLine("<none>");
            }
        }
    }
}
4

3 回答 3

1

朝着正确的方向前进……有一个优雅的解决方案可以使用 LINQ 解析文件和创建对象列表。要从文件中获取学生列表,您可以执行以下操作:

public List<Student> LoadStudents(string STUDENT_FILE)
{
   var parseQuery = //read all lines from a file
                 from line in File.ReadAllLines(STUDENT_FILE)
                 //split every line on Tab delimiter
                 let split = line.Split(new[] {'\t'})
                 //create new Student object from string array created by split for every line
                 select new Student()
                 {
                   ID = split[0],
                   FirstName = split[1],
                   LastName = split[2]
                 };

     //execute parseQuery, your student objects are now in List<Student>
      var studentList = parseQuery.ToList();
      return studentList;
 }
于 2013-05-04T13:38:16.820 回答
0

目前尚不清楚您要实现什么目标,但据我了解,您想知道如何读取文件,然后您可以使用 StreamReader。这里有很多关于这个主题的问题,你可以搜索如何阅读 txt 文件。

于 2013-05-04T13:42:16.133 回答
0

尝试类似:

class Student
{
    public string mNumber { get; set; }
    public string mFirstName { get; set; }
    public string mLastName { get; set; }

    public Student(string nr, string fName, string lName)
    {
        mNumber = nr;
        mFirstName = fName;
        mLastName = lName;
    }
}

并循环数据,例如:

foreach (string val in data)
{
    string[] valArr = val.Split(' ');
    Student st = new Student(valArr[0], valArr[1], valArr[2]);
}
于 2013-05-04T13:44:49.350 回答