2、 基于附件(附件文件是一个文本文件,可以用记事本打开查看)完成如下要求: 1) 计算所有人的总分,并设为新的一列“总分”,并存储到磁盘 2) 输出1、2班 以及合班 三份总分的降序排名 3) 求解1、2班各自的各科平均分。
时间: 2024-02-20 16:01:12 浏览: 18
好的,以下是一个基于附件完成要求的示例代码:
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// 读取文件内容
string filePath = "scores.txt";
string[] lines = File.ReadAllLines(filePath);
// 解析成绩数据
List<Student> students = new List<Student>();
foreach (string line in lines)
{
string[] fields = line.Split(',');
string name = fields[0];
int classNum = int.Parse(fields[1]);
int[] scores = fields.Skip(2).Select(int.Parse).ToArray();
Student student = new Student(name, classNum, scores);
students.Add(student);
}
// 添加总分列并计算总分
foreach (Student student in students)
{
student.TotalScore = student.Scores.Sum();
}
// 写入文件
string outputFilePath = "scores_with_total.txt";
using (StreamWriter writer = new StreamWriter(outputFilePath))
{
foreach (Student student in students)
{
string line = string.Format("{0},{1},{2}", student.Name, student.ClassNum, string.Join(",", student.Scores));
line += "," + student.TotalScore.ToString();
writer.WriteLine(line);
}
}
// 计算总分排名
List<Student> allStudents = students.OrderByDescending(s => s.TotalScore).ToList();
Console.WriteLine("合班总分排名:");
for (int i = 0; i < allStudents.Count; i++)
{
Console.WriteLine("{0}: {1}", i + 1, allStudents[i]);
}
// 计算1班和2班总分排名
List<Student> class1Students = students.Where(s => s.ClassNum == 1).ToList();
List<Student> class2Students = students.Where(s => s.ClassNum == 2).ToList();
Console.WriteLine("1班总分排名:");
for (int i = 0; i < class1Students.Count; i++)
{
Console.WriteLine("{0}: {1}", i + 1, class1Students[i]);
}
Console.WriteLine("2班总分排名:");
for (int i = 0; i < class2Students.Count; i++)
{
Console.WriteLine("{0}: {1}", i + 1, class2Students[i]);
}
// 计算各科平均分
int numSubjects = students[0].Scores.Length;
List<double> class1Averages = new List<double>();
List<double> class2Averages = new List<double>();
for (int i = 0; i < numSubjects; i++)
{
int sum1 = class1Students.Sum(s => s.Scores[i]);
int sum2 = class2Students.Sum(s => s.Scores[i]);
double avg1 = (double)sum1 / class1Students.Count;
double avg2 = (double)sum2 / class2Students.Count;
class1Averages.Add(avg1);
class2Averages.Add(avg2);
}
Console.WriteLine("1班各科平均分:");
for (int i = 0; i < numSubjects; i++)
{
Console.WriteLine("科目{0}: {1:F2}", i + 1, class1Averages[i]);
}
Console.WriteLine("2班各科平均分:");
for (int i = 0; i < numSubjects; i++)
{
Console.WriteLine("科目{0}: {1:F2}", i + 1, class2Averages[i]);
}
}
}
class Student
{
public string Name { get; set; }
public int ClassNum { get; set; }
public int[] Scores { get; set; }
public int TotalScore { get; set; }
public Student(string name, int classNum, int[] scores)
{
Name = name;
ClassNum = classNum;
Scores = scores;
}
public override string ToString()
{
return string.Format("{0}({1}): {2}", Name, ClassNum, TotalScore);
}
}
```
这个程序会读取附件中的成绩文件,计算总分并添加新的一列,然后将结果写入新的文件。接着,程序会计算所有学生的总分排名和1、2班的总分排名,并输出结果。最后,程序会计算1、2班各自的各科平均分,并输出结果。注意,这个程序中的文件路径和分隔符是根据附件的具体格式进行设置的,如果格式有变化,需要相应地进行修改。