计算参与者总数和插槽1、2、3的总数

问题描述 投票:0回答:3

所以我遇到了这个问题。我的文本文件具有以下布局:人名电话号码时隙

我必须计算该文件的参加者总数以及插槽1,2,3的注册参加者有什么帮助吗?

例如:

 Manchester Event Information
 Total Registered Participants: 10
 Registered Participants for Slot 1:   6
 Registered Participants for Slot 2:   2
 Registered Participants for Slot 3:   2
 Participant 1 Name: Tom Jones
 Participant 2 Name: Katy Perry



static void displayRegisteredParticipants() 
{                    
    string sValidLocation;            
    int iIndex;            

    Console.WriteLine("Please enter the location you wish to view data for.");
    sValidLocation = Console.ReadLine();

    switch (sValidLocation)
    {
        case "London":
            if (File.Exists("London.txt"))
            {
                Console.WriteLine("Location exists");
                Console.WriteLine();
            }  
            else
            {
                Console.WriteLine("Location not found, please try again");
                sValidLocation = Console.ReadLine();
                Console.WriteLine();
            }

            Console.WriteLine( "*  " + sValidLocation + " Event Information");
            Console.WriteLine();
            using (StreamReader sr = new StreamReader("London.txt"))
            {

                string sReadName = sr.ReadLine();
                string sReadPhoneNo = sr.ReadLine();
                int iReadTimeSlot = Convert.ToInt32(sr.ReadLine()); 

            }
            break;
c# function loops streamreader
3个回答
0
投票

我认为这里需要更多信息。在文件中,您提到的人名,电话号码和时间段-数据的格式如何,例如逗号分隔? (我不太清楚它如何映射到代码块顶部的示例数据)

顺便说一句,如果您打算打开每个可能的位置/文件名,您的代码将变得很长,并且包含很多重复。我建议也许创建一个新变量来保存您的文件名,例如

var filename = $"{sValidLocation}.txt";

然后在检查文件中使用它来检查文件的存在和读取。


0
投票

从文件中读取行之后,需要查看并根据其内容对其进行处理:

string line = sr.ReadLine();

int tot = -1;
if(line.ToLower().StartsWith("total registered participants:"){
  line = line.ToLower().Replace("total registered participants:", "").Trim();
  int.TryParse(line, out tot);
}
else if(...)

具有一组如果检查您的行并删除所有非数字文本,然后解析剩下的内容。这样可以使您的行以任意顺序排列,并且可以承受大小写更改和冒号后的多余空格等


0
投票

这里:

void Main()
{
    //Input:
    //Tom Jones 07722990000 1
    //Katy Perry 01122334455 1
    //Kylie Jenner 01267483233 2
    //Taylor Swift 1234543234 3
    //Rod Stewart 07722996700 2
    //Liam Payne 07722995000 3
    //Harry Styles 07722994000 1
    //Jonas Blue 07722993000 1
    //Rita Ora 07722290000 1
    //Tom Walker 07722990010 1


    string fileName = @"C:\\TEMP\\London.txt";
    string outputFileName = @"C:\\TEMP\\output.txt";
    List<Participant> list = GetParticipants(fileName);
    CreateOutput(list, outputFileName);

    //  Total Registered Participants: 10
    //  Registered Participants for Slot 1: 6
    //  Registered Participants for Slot 2: 2
    //  Registered Participants for Slot 3: 2
    //  Participant 1 Name: Tom Jones
    //  Participant 1 Name: Katy Perry
    //  Participant 1 Name: Kylie Jenner
    //  Participant 1 Name: Taylor Swift
    //  Participant 1 Name: Rod Stewart
    //  Participant 1 Name: Liam Payne
    //  Participant 1 Name: Harry Styles
    //  Participant 1 Name: Jonas Blue
    //  Participant 1 Name: Rita Ora
    //  Participant 1 Name: Tom Walker
}

private void CreateOutput(List<Participant> list, string outputFileName)
{
    using(StreamWriter w = new StreamWriter(outputFileName))
    {
        w.WriteLine($"Total Registered Participants: {list.Count}");
        w.WriteLine($"Registered Participants for Slot 1: {list.Count(x => x.Slot == 1)}");
        w.WriteLine($"Registered Participants for Slot 2: {list.Count(x => x.Slot == 2)}");
        w.WriteLine($"Registered Participants for Slot 3: {list.Count(x => x.Slot == 3)}");

        foreach(Participant p in list)
        {
            w.WriteLine($"Participant 1 Name: {p.Name}");
        }
    }
}

private List<Participant> GetParticipants(string fileName)
{
    List<Participant> list = new List<UserQuery.Participant>();
    if(File.Exists(fileName))
    {
        string pattern = @"(.*?)([\d]+)\s+([\d]+)";
        int outValue;
        using(StreamReader reader = new StreamReader(fileName))
        {
            while(reader.Peek() != -1)
            {
                string line = reader.ReadLine();
                Match m = Regex.Match(line, pattern);
                if(m.Success && m.Groups.Count > 2)
                {
                    Participant p = new Participant();
                    p.Name = m.Groups[1].Value;
                    p.Phone = m.Groups[2].Value;
                    if(int.TryParse(m.Groups[3].Value, out outValue))
                        p.Slot = outValue;
                    list.Add(p);
                }
            }
        }
    }
    return list;
}

public class Participant
{
    public string Name {get;set;}
    public string Phone {get;set;}
    public int Slot {get;set;}
}
© www.soinside.com 2019 - 2024. All rights reserved.