fork download
  1. namespace MyCalendar
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6.  
  7. internal sealed class Calendar
  8. {
  9. private static readonly List<String> Wdays = new List<string>() { "日", "月", "火", "水", "木", "金", "土" };
  10. private static readonly Int32 Nwdays = Wdays.Count;
  11.  
  12. private readonly DateTime dt;
  13.  
  14. internal Calendar(DateTime dt)
  15. {
  16. this.dt = dt;
  17. }
  18.  
  19. internal void Show()
  20. {
  21. Console.WriteLine(" {0}年{1}月", dt.Year, dt.Month);
  22. Wdays.ForEach(v => Console.Write("{0} ", v));
  23. Console.WriteLine("");
  24.  
  25. Int32 sw = 1;
  26. Int32 dow = (Int32)dt.DayOfWeek;
  27. Int32 max = dow + DateTime.DaysInMonth(dt.Year, dt.Month);
  28. Enumerable.Range(1, max)
  29. .ToList()
  30. .ForEach(i =>
  31. {
  32. if (i > dow)
  33. {
  34. Console.Write("{0, 2} ", sw++);
  35. }
  36. else
  37. {
  38. Console.Write(" ");
  39. }
  40.  
  41. if (i % Nwdays == 0)
  42. {
  43. Console.WriteLine("");
  44. }
  45. });
  46. Console.WriteLine("");
  47. }
  48. }
  49.  
  50. internal sealed class Program
  51. {
  52. static void Main(string[] args)
  53. {
  54. Int32 year;
  55. while (true)
  56. {
  57. if (Int32.TryParse(Console.ReadLine(), out year))
  58. {
  59. var issuccess = false;
  60. try
  61. {
  62. new DateTime(year, 1, 1);
  63. issuccess = true;
  64. }
  65. catch (ArgumentOutOfRangeException)
  66. {
  67. }
  68.  
  69. if (issuccess)
  70. {
  71. break;
  72. }
  73. }
  74.  
  75. Console.WriteLine("正しい入力ではありません。");
  76. }
  77.  
  78. Int32 month;
  79. DateTime? dt = null;
  80. while (true)
  81. {
  82. if (Int32.TryParse(Console.ReadLine(), out month))
  83. {
  84. var issuccess = false;
  85. try
  86. {
  87. dt = new DateTime(year, month, 1);
  88. issuccess = true;
  89. }
  90. catch (ArgumentOutOfRangeException)
  91. {
  92. }
  93.  
  94. if (issuccess)
  95. {
  96. break;
  97. }
  98. }
  99.  
  100. Console.WriteLine("正しい入力ではありません。");
  101. };
  102.  
  103. (new Calendar((DateTime)dt)).Show();
  104. }
  105. }
  106. }
  107.  
Success #stdin #stdout 0.03s 16072KB
stdin
0
-1
a
null
2016
13
2
stdout
正しい入力ではありません。
正しい入力ではありません。
正しい入力ではありません。
正しい入力ではありません。
正しい入力ではありません。
     2016年2月
日 月 火 水 木 金 土 
    1  2  3  4  5  6 
 7  8  9 10 11 12 13 
14 15 16 17 18 19 20 
21 22 23 24 25 26 27 
28 29