
C#的一个函数题
从一个记录了学生成绩的文本文档,每个学生成绩是一行,每行是用|分割的数据,用|分割的域分别是姓名、年龄、成绩,写程序取出成绩最高学生的姓名和成绩。参考:使用string[...
从一个记录了学生成绩的文本文档,每个学生成绩是一行,每行是用|分割的数据,用|分割的域分别是姓名、年龄、成绩,写程序取出成绩最高学生的姓名和成绩。参考:使用string [] lines=System.IO.File.ReadAllLines(@"c:\root.int",Encoding.Default);从文本文件读取数据,返回值为string数组,每个元素是一行
展开
3个回答
展开全部
假设一文本文件内容如下:
mereg|22|80
mereg1|20|99
mereg2|32|100
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class student
{
public string name { get; set; }
public int age { get; set; }
public int grade { get; set; }
}
class Class1
{
static IEnumerable<student> Lines(StreamReader reader)
{
if (reader == null)
throw new ArgumentNullException("Reader is null");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] content = line.Split('|');
yield return new student { name = content[0], age = int.Parse(content[1]), grade = int.Parse(content[2]) };
}
}
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader("c:/sample.txt"))
{
var stu = from s in Lines(reader)
orderby s.grade descending
group s by s.grade into grade
select grade;
if (stu != null)
{
foreach (var sb in stu.First())
{
Console.WriteLine("学生姓名 "+sb.name +" 成绩: "+ sb.grade);
}
}
}
}
}
}
mereg|22|80
mereg1|20|99
mereg2|32|100
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication2
{
class student
{
public string name { get; set; }
public int age { get; set; }
public int grade { get; set; }
}
class Class1
{
static IEnumerable<student> Lines(StreamReader reader)
{
if (reader == null)
throw new ArgumentNullException("Reader is null");
string line;
while ((line = reader.ReadLine()) != null)
{
string[] content = line.Split('|');
yield return new student { name = content[0], age = int.Parse(content[1]), grade = int.Parse(content[2]) };
}
}
static void Main(string[] args)
{
using (StreamReader reader = new StreamReader("c:/sample.txt"))
{
var stu = from s in Lines(reader)
orderby s.grade descending
group s by s.grade into grade
select grade;
if (stu != null)
{
foreach (var sb in stu.First())
{
Console.WriteLine("学生姓名 "+sb.name +" 成绩: "+ sb.grade);
}
}
}
}
}
}
展开全部
class Test
{
public void Process()
{
List<StudentInfo> list = new List<StudentInfo>();
string[] lines = System.IO.File.ReadAllLines(@"c:\root.int",Encoding.Default);
foreach(string line in lines)
{
list.Add(StudentInfo.Parse(line));
}
int highest = -1;
List<int> highestIndexList = new List<int>();
for( int i = 0 ; i < list.Count ; i++)
{
if(list[i].Result > highest )
{
highestIndexList .Clear();
highestIndexList.Add(i);
highest = list[i].Result;
}
else if(list[i].Result == highest)
{
highestIndexList.Add(i);
}
}
foreach(int index in highestIndexList )
{
Console.WriteLine(list[index].Name + " : " + list[index].Result);
}
}
}
class StudentInfo
{
public string Name;
public int Year;
public int Result;
private StudentInfo(){}
private StudentInfo(string name, string year, string result){Name = name; Year = year; Result = result;}
public static StudentInfo Parse(string line)
{
string spliter = {"|"}
string[] infos = line.Split(spliter, StringSplitOptions.RemoveEmptyEntries);
string name = line[0];
int year = int.Parse(line[1]);
int result =int.Parse(line[2]);
return new StudentInfo(name, year, result);
}
}
{
public void Process()
{
List<StudentInfo> list = new List<StudentInfo>();
string[] lines = System.IO.File.ReadAllLines(@"c:\root.int",Encoding.Default);
foreach(string line in lines)
{
list.Add(StudentInfo.Parse(line));
}
int highest = -1;
List<int> highestIndexList = new List<int>();
for( int i = 0 ; i < list.Count ; i++)
{
if(list[i].Result > highest )
{
highestIndexList .Clear();
highestIndexList.Add(i);
highest = list[i].Result;
}
else if(list[i].Result == highest)
{
highestIndexList.Add(i);
}
}
foreach(int index in highestIndexList )
{
Console.WriteLine(list[index].Name + " : " + list[index].Result);
}
}
}
class StudentInfo
{
public string Name;
public int Year;
public int Result;
private StudentInfo(){}
private StudentInfo(string name, string year, string result){Name = name; Year = year; Result = result;}
public static StudentInfo Parse(string line)
{
string spliter = {"|"}
string[] infos = line.Split(spliter, StringSplitOptions.RemoveEmptyEntries);
string name = line[0];
int year = int.Parse(line[1]);
int result =int.Parse(line[2]);
return new StudentInfo(name, year, result);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我也只是初学,还不清楚这么多呢
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询