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 Int32 year;
  13. private readonly Int32 month;
  14.  
  15. internal Calendar(Int32 year, Int32 month)
  16. {
  17. this.year = year;
  18. this.month = month;
  19. }
  20.  
  21. private Int32 GetDaysInMonth()
  22. {
  23. switch (this.month)
  24. {
  25. case 2:
  26. if (this.year % (this.year % 25 != 0 ? 4 : 16) < 1)
  27. {
  28. return 29;
  29. }
  30. else
  31. {
  32. return 28;
  33. }
  34.  
  35. case 4:
  36. case 6:
  37. case 9:
  38. case 11:
  39. return 30;
  40.  
  41. default:
  42. return 31;
  43. }
  44. }
  45.  
  46. private Int32 Zeller()
  47. {
  48. var y = this.year;
  49. var m = this.month;
  50.  
  51. if (m < 3)
  52. {
  53. y--;
  54. m += 12;
  55. }
  56.  
  57. return (y + y / 4 - y / 100 + y / 400 + (13 * m + 8) / 5 + 1) % 7;
  58. }
  59.  
  60. internal void Show()
  61. {
  62. Console.WriteLine(" {0}年{1}月", this.year, this.month);
  63. Wdays.ForEach(v => Console.Write("{0} ", v));
  64. Console.WriteLine("");
  65.  
  66. Int32 sw = 1;
  67. Int32 dow = Zeller();
  68. Int32 max = dow + this.GetDaysInMonth();
  69. Enumerable.Range(1, max)
  70. .ToList()
  71. .ForEach(i =>
  72. {
  73. if (i > dow)
  74. {
  75. Console.Write("{0, 2} ", sw++);
  76. }
  77. else
  78. {
  79. Console.Write(" ");
  80. }
  81.  
  82. if (i % Nwdays == 0)
  83. {
  84. Console.WriteLine("");
  85. }
  86. });
  87. Console.WriteLine("");
  88. }
  89. }
  90.  
  91. internal sealed class Program
  92. {
  93. static void Main(string[] args)
  94. {
  95. Int32 year;
  96. while (true)
  97. {
  98. if (Int32.TryParse(Console.ReadLine(), out year) && year >= 1582)
  99. {
  100. break;
  101. }
  102.  
  103. Console.WriteLine("正しい入力ではありません。");
  104. }
  105.  
  106. Int32 month;
  107. while (true)
  108. {
  109. if (Int32.TryParse(Console.ReadLine(), out month) && month >= 1 && month <= 12)
  110. {
  111. break;
  112. }
  113.  
  114. Console.WriteLine("正しい入力ではありません。");
  115. };
  116.  
  117. new Calendar(year, month).Show();
  118. }
  119. }
  120. }
  121.  
Success #stdin #stdout 0.03s 16024KB
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