用c#编写一个程序读取附件(附件文件是一个文本文件,可以用记事本打开查看)完成如下要求: 1) 计算所有人的总分,并设为新的一列“总分”,并存储到磁盘 2) 输出1、2班 以及合班 三份总分的降序排名 3) 求解1、2班各自的各科平均分
时间: 2024-03-07 14:48:15 浏览: 59
C++输入函数和成绩显示函数,并计算每位同学的总分和平均分,对成绩排名输出等.docx
5星 · 资源好评率100%
好的,以下是一个基于您的要求的 C# 程序实现,假设附件文件名为 "scores.txt":
```csharp
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main(string[] args)
{
// 读取附件文件
var lines = File.ReadAllLines("scores.txt");
// 解析每行数据
var students = lines.Select(line =>
{
var fields = line.Split(',');
var student = new Student
{
Class = int.Parse(fields[0]),
Name = fields[1],
MathScore = int.Parse(fields[2]),
EnglishScore = int.Parse(fields[3]),
ChineseScore = int.Parse(fields[4])
};
return student;
}).ToList();
// 计算每个学生的总分
foreach (var student in students)
{
student.TotalScore = student.MathScore + student.EnglishScore + student.ChineseScore;
}
// 将总分保存到磁盘
var outputLines = students.Select(student =>
$"{student.Class},{student.Name},{student.MathScore},{student.EnglishScore},{student.ChineseScore},{student.TotalScore}");
File.WriteAllLines("scores_with_total.txt", outputLines);
// 计算总分排名
var totalScores = students.Select(student => student.TotalScore);
var overallRanking = totalScores.OrderByDescending(score => score).ToList();
var class1Ranking = totalScores.Where(score => students.Where(s => s.Class == 1).Select(s => s.TotalScore).Contains(score)).OrderByDescending(score => score).ToList();
var class2Ranking = totalScores.Where(score => students.Where(s => s.Class == 2).Select(s => s.TotalScore).Contains(score)).OrderByDescending(score => score).ToList();
// 输出总分排名
Console.WriteLine("Overall ranking:");
for (int i = 0; i < overallRanking.Count; i++)
{
Console.WriteLine($"{i + 1}. {overallRanking[i]}");
}
Console.WriteLine("Class 1 ranking:");
for (int i = 0; i < class1Ranking.Count; i++)
{
Console.WriteLine($"{i + 1}. {class1Ranking[i]}");
}
Console.WriteLine("Class 2 ranking:");
for (int i = 0; i < class2Ranking.Count; i++)
{
Console.WriteLine($"{i + 1}. {class2Ranking[i]}");
}
// 计算平均分
var class1MathAverage = students.Where(s => s.Class == 1).Average(s => s.MathScore);
var class1EnglishAverage = students.Where(s => s.Class == 1).Average(s => s.EnglishScore);
var class1ChineseAverage = students.Where(s => s.Class == 1).Average(s => s.ChineseScore);
var class2MathAverage = students.Where(s => s.Class == 2).Average(s => s.MathScore);
var class2EnglishAverage = students.Where(s => s.Class == 2).Average(s => s.EnglishScore);
var class2ChineseAverage = students.Where(s => s.Class == 2).Average(s => s.ChineseScore);
Console.WriteLine($"Class 1 math average: {class1MathAverage}");
Console.WriteLine($"Class 1 English average: {class1EnglishAverage}");
Console.WriteLine($"Class 1 Chinese average: {class1ChineseAverage}");
Console.WriteLine($"Class 2 math average: {class2MathAverage}");
Console.WriteLine($"Class 2 English average: {class2EnglishAverage}");
Console.WriteLine($"Class 2 Chinese average: {class2ChineseAverage}");
}
}
class Student
{
public int Class { get; set; }
public string Name { get; set; }
public int MathScore { get; set; }
public int EnglishScore { get; set; }
public int ChineseScore { get; set; }
public int TotalScore { get; set; }
}
```
这个程序首先读取附件文件,解析每行数据并创建一个 `Student` 对象的列表。然后,它计算每个学生的总分,并将结果保存到一个新的文件中。接下来,它计算总分排名,并输出1、2班和合班的排名。最后,它计算每个班级的各科平均分,并输出结果。
阅读全文