题目考试成绩排序
要求
1实现从高到低排序
2统计分组人数及百分比(分为大于等于90807060及不及挌)
3分数数据来源于文本文件
4处理结果保存在一个文本文件中
C#代码如下
using System;
using System.IO;
using System.Linq;
namespace ScoreSorter
{
class Program
{
static void Main(string[] args)
{
// 读取文件中的分数
var scores = File.ReadAllLines("scores.txt")
.Select(line => int.Parse(line))
.ToList();
// 从高到低排序
scores.Sort((a, b) => b.CompareTo(a));
// 统计分组人数及百分比
var scoreGroups = new int[6];
foreach (var score in scores)
{
if (score >= 90)
{
scoreGroups[0]++;
}
else if (score >= 80)
{
scoreGroups[1]++;
}
else if (score >= 70)
{
scoreGroups[2]++;
}
else if (score >= 60)
{
scoreGroups[3]++;
}
else
{
scoreGroups[4]++;
}
}
// 计算百分比
var scoreGroupPercents = new double[6];
for (int i = 0; i < scoreGroups.Length; i++)
{
scoreGroupPercents[i] = (double)scoreGroups[i] / scores.Count;
}
// 将排序后的分数和分组统计信息写入文件
using (var writer = File.CreateText("output.txt"))
{
// 写入排序后的分数
foreach (var score in scores)
{
writer.WriteLine(score);
}
// 写入分组人数及百分比
writer.WriteLine("90+: " + scoreGroups[0] + " " + scoreGroupPercents[0]);
writer.WriteLine("80+: " + scoreGroups[1] + " " + scoreGroupPercents[1]);
writer.WriteLine("70+: " + scoreGroups[2] + " " + scoreGroupPercents[2]);
writer.WriteLine("60+: " + scoreGroups[3] + " " + scoreGroupPercents[3]);
writer.WriteLine("<60: " + scoreGroups[4] + " " + scoreGroupPercents[4]);
}
}
}
}
文件中成绩是一行,还是每个成绩一行呢
static void Main(string[] args)
{
int a, b, c, d, e, t, count;
a = b = c = d = e = count = 0;
try
{
using (StreamReader sr = new StreamReader("in.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
Console.WriteLine(line);
t = int.Parse(line);
count++;
if (t >=
90) a++;
else if (t < 90 && t >= 80) b++;
else if (t < 80 && t >= 70) c++;
else if (t < 70 && t >= 60) d++;
else e++;
}
}
using (StreamWriter sr = new StreamWriter("out.txt"))
{
sr.WriteLine(count);
sr.WriteLine("{0} {1:#.##%}", a, a * 1.0 / count);
sr.WriteLine("{0} {1:#.##%}", b, b * 1.0 / count);
sr.WriteLine("{0} {1:#.##%}", c, c * 1.0 / count);
sr.WriteLine("{0} {1:#.##%}", d, d * 1.0 / count);
sr.WriteLine("{0} {1:#.##%}", e, e * 1.0 / count);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadKey();
}
in.txt一行一个成绩。
代码如下
private void button1_Click(object sender, EventArgs e)
{
string[] lines = System.IO.File.ReadAllLines(@".\test.txt");
List<float> values = new List<float>();
foreach (string line in lines)
{
if(float.TryParse(line.Trim(),out float grade))
{
values.Add(grade);
}
}
int total=values.Count;
int count90 = values.Count(x => { return x >= 90; } );
int count80 = values.Count(x => { return x >= 80; });
int count70 = values.Count(x => { return x >= 70; });
int count60 = values.Count(x => { return x >= 60; });
int lower60 = values.Count(x => { return x < 60; });
List<string> res = new List<string>();
res.Add($"90分以上:{count90},占比:{(float)count90 / (float)total}");
res.Add($"80分以上:{count80},占比:{(float)count80 / (float)total}");
res.Add($"70分以上:{count70},占比:{(float)count70 / (float)total}");
res.Add($"60分以上:{count60},占比:{(float)count60 / (float)total}");
res.Add($"60分以下:{lower60},占比:{(float)lower60 / (float)total}");
System.IO.File.WriteAllLines(@".\test_res.txt", res);
}
如有帮助,请采纳
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Student
{
string sname;
int english;
int math;
int num;
public void inputinfo()
{
Console.Write("学号:");
num = int.Parse(Console.ReadLine());
Console.Write("姓名:");
sname =Console.ReadLine();
Console.Write("数学:");
math = int.Parse(Console.ReadLine());
Console.Write("英语:");
english = int.Parse(Console.ReadLine());
}
public float Comsum()
{
return this.math + this.english;
}
public void display()
{
Console.WriteLine("\t{0} \t{1} \t{2} \t{3} \t{4}",num,sname,english,math,Comsum());
}
}
class Program
{
static void sort(int n,params Student[] p)
{
Student temp;
int pos = 0;
for (int i = 0; i < n - 1; i++)
{
pos = i;
for (int j = i + 1; j < n; j++)
{
if (p[j].Comsum() > p[pos].Comsum())
{
pos = j;
}
}//第i个数和最小的数p【pos】互换
temp = p[i];
p[i]=p[pos];
p[pos] = temp;
}
}
static void Main(string[] args)
{
int i, stuNum;
Console.WriteLine("输入学生个数:");
stuNum = int.Parse(Console.ReadLine());
Student[] stu=new Student[stuNum];
for(i=0;i<stuNum;i++)
stu[i]=new Student();
for(i=0;i<stuNum;i++)
{
Console.WriteLine("输入第{0}学生数据:",i+1);
stu[i].inputinfo();
}
Console.WriteLine("排序前:");
Console.WriteLine("\t姓名\t 英语\t 数学\t 总分");
for (i = 0; i < stuNum;i++ )
{
Console.Write("序号{0}:",i);
stu[i].display();
}
sort(stuNum, stu);
Console.WriteLine("排序后:");
Console.WriteLine("\t姓名\t 英语\t 数学\t 总分");
for (i = 0; i < stuNum; i++)
{
Console.Write("第{0}名",i+1);
stu[i].display();
}
Console.ReadLine();
}
}
}