1

我对编程相当陌生,并且难以对以下程序代码进行建模。

该程序读取一个文件,选择某些需求并显示它们。已尝试使用传递数组作为我的教科书中所述的参数或函数,但我似乎无法将其正确写入 C#。使用 int getAges(int array[], integer) 等示例。

//Parsing data into memory

string content;
using (StreamReader reader = new StreamReader(File.Open("Data.txt", FileMode.Open)))
{
    content = reader.ReadToEnd();
}
string[] rows = content.Split('\n'); //Each row is on a new line
string[][] table = new string[rows.Length][];
for (int i = 0; i < rows.Length; table[i] = rows[i].Split(','), i++) ;



//selecting information

int[] districts = new int[rows.Length];
int[] ages = new int[rows.Length];

for (int i = 0; i < rows.Length; i++)
{
    districts[i] = int.Parse(table[i][3]);
    ages[i] = int.Parse(table[i][0]);
}



//Analyzing selected information

foreach (int district in districts.Distinct().OrderBy(x => x))
    Console.WriteLine("District {0} has {1} resident(s)", district, districts.Count(x => x == district));
Console.WriteLine("Ages 0-18 : {0} resident(s)", ages.Count(x => x < 18));
Console.WriteLine("Ages 18-30 : {0} resident(s)", ages.Count(x => x >= 18 && x <= 30));
Console.WriteLine("Ages 31-45 : {0} resident(s)", ages.Count(x => x >= 31 && x <= 45));
Console.WriteLine("Ages 46-64 : {0} resident(s)", ages.Count(x => x >= 46 && x <= 64));
Console.WriteLine("Ages >=65 : {0} resident(s)", ages.Count(x => x >= 65));
4

1 回答 1

3

您可以使用 Linq 将整个代码缩短至此。

var table = File.ReadLines("Data.txt").Select(x => x.Split(',')).ToArray();

请注意,File.ReadLines会懒惰地读取行,因此它对内存效率也有好处(如果您正在使用它做更多的事情)。要分别获取地区和年龄,我会执行以下操作:

var districts = table.Select(x => int.Parse(x[3])).ToArray();
var ages = table.Select(x => int.Parse(x[0])).ToArray();

你也可以foreach用 linq 写成一行。

var districtFrequency = districts.GroupBy(x => x)
                                 .Select(x => new { name = x.Key, count = x.Count() })
                                 .OrderBy(x => x.name);

foreach (var district in districtFrequency)
    Console.WriteLine("District {0} has {1} resident(s)", district.name, districts.count);

我想我误读了你的问题。您可以将您的逻辑建模为类并以 oop 方式访问它,但在不了解您的模型/域的情况下,详细说明任何内容都毫无意义。然而,这是一个粗略的草图:

public class Person //or whatever the entity is
{
    public string Name { get; private set; }
    public int Age { get; private set; }
    public string Place { get; private set; }

    //provide a suitable constructor
}

现在您将能够以Person几乎相同的方式从文本文件中获取列表:

public static IEnumerable<Person> GetPeople(string filepath)
{
    foreach (var line in File.ReadLines(filepath))
    {
        var properties = line.Split(',');
        yield return new Person(properties[0], int.Parse(properties[1]), etc...
        //according to properties of Person class
    }
}

//you can call it like
var people = GetPeople("Data.txt").ToList();

//your foreach calls will look almost the same:
foreach (var district in people.Select(x => x.Place).Distinct().OrderBy(x => x))
    Console.WriteLine("District {0} has {1} resident(s)", district, people.Count(x => x.Place == district));
Console.WriteLine("Ages 0-18 : {0} resident(s)", people.Count(x => x.Age < 18));
Console.WriteLine("Ages 18-30 : {0} resident(s)", people.Count(x => x.Age >= 18 && x.Age <= 30));
Console.WriteLine("Ages 31-45 : {0} resident(s)", people.Count(x => x.Age >= 31 && x.Age <= 45));
Console.WriteLine("Ages 46-64 : {0} resident(s)", people.Count(x => x.Age >= 46 && x.Age <= 64));
Console.WriteLine("Ages >=65 : {0} resident(s)", people.Count(x => x.Age >= 65));

您可以创建自己的集合类并将这些功能委托给类本身,但这可能不是很有用。

public class People : List<Person>
{
    //your methods to fetch or print or whatever
}

最后,文本文件并不是存储这种数据的最佳方式。您可能想查看专用数据库。

于 2013-11-06T08:35:13.183 回答