读取file.txt上缺少的日期 [英] reading missing dates on a file.txt

查看:76
本文介绍了读取file.txt上缺少的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个应用程序,用户输入日期,输出应该是用户未输入的月份的日期,如果您在2012年8月在1/08/2012,2/08/2012,3/08/2012中输入了该日期,输出应为未输入的八月日期(周末除外)
我编写的控制台应用程序

Hi
I have an application where a user input dates and the output should be the dates of the month that the user didnt input if in august 2012 you putted in 1/08/2012,2/08/2012,3/08/2012 so the output should be those august dates were not entered excluding weekends
I writing a console application which is

// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");

// write a line of text to the file
tw.WriteLine(DateTime.Now);

// close the stream
tw.Close();

推荐答案

我将尝试为您提供一些有关如何尝试解决此问题的提示.

I''ll try to give you some hints on how you could attempt to solve this.


  1. 创建一个名为DateCheck的类,它具有两个属性DateTime Datebool IsPresent
  2. 创建字典Dictionary<datetime,>并运行一个月的日子填充字典,其中的键是当前的循环中的日期,该值是DateCheck的一个实例,其中Date也设置为当前日期,IsPresent设置为false(也可以在DateCheck的构造函数中完成).
  3. 在逐行显示日期的文件:

  1. Create a class called DateCheck with two properties DateTime Date and bool IsPresent
  2. Create a dictionary Dictionary<datetime,> and running throug the days of one month fill the dictionary where the key is the current date in the loop and the value is an instance of DateCheck with Date also set to the current date and IsPresent set to false (that could also be done in the constructor of DateCheck).
  3. Read in the file with the dates in it line by line and:

  1. 从您从文件中读取的行(DateTime.TryParseDateTime.Parse)中解析日期
  2. 在字典中查找与您在文件中找到的日期相关联的值
  3. >
  4. 将上一步中查找的值的IsPresent设置为true

  1. Parse the date from the line you read from the file (DateTime.TryParse or DateTime.Parse)
  2. Lookup the value in the dictionary that is associated with the date you found in the file
  3. Set IsPresent to true for the value you looked up in the previous step

  • 遍历字典中的所有值并收集所有DateCheck 实例IsPresent是false.
  • Iterate over all the values in the dictionary and collect all instances of DateCheck where IsPresent is false.


  • 而且您已经完成了!

    尝试一下,您将可以正常使用.

    — Manfred
    PS:可以使用LinQ查询来过滤字典中的值.



    And you''re pretty much done!

    Give it a try, you''ll work it out all right.

    — Manfred
    P.S: The filtering of the values from the dictionary could be done by using a LinQ query.


    您所做的,真的什么都没有.您尚未完成任何任务.您要做的是使用File.ReadAllLines将文件作为字符串数组获取.然后使用DateTime.TryParse获取日期.然后,您需要遍历所有日期,并将找到的每个新月存储到数组中.您可以使用Exists方法检查其中是否已经有东西,我不确定.NET是否具有set类.最后,检查每个可能的日期是否在列表中,如果没有,则发出该日期.最简单的方法是列出所有月份的列表,然后遍历整个列表并发出不在较小列表中的任何内容.
    What you did, is really nothing. You haven''t done any of your tasks. What you want to do is to use File.ReadAllLines to get your file as an array of strings. Then DateTime.TryParse to get dates out. Then you need to iterate over all the dates, and store each new month you find in to an array. You can use the Exists method to check if something is in there already, I am not sure .NET has a set class. Finally, you check if each possible date is in the list and emit it if it is not. A list of all the months is the easiest way to do that, iterate over the full list and emit anyting not in the smaller list.


    这篇关于读取file.txt上缺少的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆