initial import from moinmoin
[debienna.git] / Kata4Lösungen / CSharp / index.mdwn
1 = Part 1 =\r
2 \r
3 {{{\r
4 using System;\r
5 using System.IO;\r
6 using System.Text.RegularExpressions;\r
7 \r
8 namespace Kata4\r
9 {\r
10         class Kata4\r
11         {\r
12                 public static void Main(string[] argv)\r
13                 {\r
14                         FileStream fs = new FileStream("K4Weather.txt", FileMode.Open, FileAccess.Read);\r
15                         StreamReader r = new StreamReader(fs);\r
16                         int min_spread = Int32.MaxValue;\r
17                         int spread_day = -1;\r
18                         while(r.Peek() > -1)\r
19                         {\r
20                                 string s = r.ReadLine();\r
21                                 Match fields = Regex.Match(s, "^ +([0-9]+) +([0-9]+)[^ ]* *([0-9]+).*");\r
22                                 try {\r
23                                         int day = Convert.ToInt32(fields.Groups[1].ToString());\r
24                                         int min = Convert.ToInt32(fields.Groups[3].ToString());\r
25                                         int max = Convert.ToInt32(fields.Groups[2].ToString());\r
26                                         int spread = max - min;\r
27                                         if (spread <= min_spread)\r
28                                         {\r
29                                                 spread_day = day;\r
30                                                 min_spread = spread;\r
31                                         }\r
32                                 } catch {\r
33                                 }\r
34                         }\r
35                         Console.WriteLine(spread_day);\r
36                 }\r
37         }\r
38 }\r
39 \r
40 \r
41 }}}\r
42 \r
43 \r
44 = Part 2 =\r
45 \r
46 {{{\r
47 using System;\r
48 using System.IO;\r
49 using System.Text.RegularExpressions;\r
50 \r
51 namespace Kata4\r
52 {\r
53         class Kata4_2\r
54         {\r
55                 public static void Main(string[] argv)\r
56                 {\r
57                         FileStream fs = new FileStream("K4Soccer.txt", FileMode.Open, FileAccess.Read);\r
58                         StreamReader r = new StreamReader(fs);\r
59                         int min_spread = Int32.MaxValue;\r
60                         string spread_day = "";\r
61                         while(r.Peek() > -1)\r
62                         {\r
63                                 string s = r.ReadLine();\r
64                                 Match fields = Regex.Match(s, "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");\r
65                                 try {\r
66                                         string day = fields.Groups[1].ToString();\r
67                                         Console.Write("Testing");\r
68                                         Console.WriteLine(day);\r
69                                         int min = Convert.ToInt32(fields.Groups[2].ToString());\r
70                                         int max = Convert.ToInt32(fields.Groups[3].ToString());\r
71                                         int spread = Math.Abs( max - min );\r
72                                         if (spread <= min_spread)\r
73                                         {\r
74                                                 spread_day = day;\r
75                                                 min_spread = spread;\r
76                                         }\r
77                                 } catch {\r
78                                 }\r
79                         }\r
80                         Console.WriteLine(spread_day);\r
81                 }\r
82         }\r
83 }\r
84 }}}\r
85 \r
86 = Part 3 =\r
87 \r
88 {{{\r
89 using System;\r
90 using System.IO;\r
91 using System.Text.RegularExpressions;\r
92 \r
93 namespace Kata4\r
94 {\r
95         class Kata4_2\r
96         {\r
97                 public static void Main(string[] args)\r
98                 {\r
99                         Console.Write("part1: ");\r
100                         DM("K4Weather.txt", "^ +([0-9]+) +([0-9]+)[^ ]* *([0-9]+).*");\r
101                         Console.Write("part2: ");\r
102                         DM("K4Soccer.txt", "^ +[0-9]+\\. ([A-Za-z_]+) +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +([0-9]+) +- +([0-9]+).*");\r
103                 }\r
104 \r
105                 public static void DM(string file, string pattern)\r
106                 {\r
107                         FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);\r
108                         StreamReader r = new StreamReader(fs);\r
109                         int min_spread = Int32.MaxValue;\r
110                         string spread_day = "";\r
111                         while(r.Peek() > -1)\r
112                         {\r
113                                 string s = r.ReadLine();\r
114                                 Match fields = Regex.Match(s, pattern);\r
115                                 try {\r
116                                         string day = fields.Groups[1].ToString();\r
117                                         int min = Convert.ToInt32(fields.Groups[2].ToString());\r
118                                         int max = Convert.ToInt32(fields.Groups[3].ToString());\r
119                                         int spread = Math.Abs( max - min );\r
120                                         if (spread <= min_spread)\r
121                                         {\r
122                                                 spread_day = day;\r
123                                                 min_spread = spread;\r
124                                         }\r
125                                 } catch {\r
126                                 }\r
127                         }\r
128                         Console.WriteLine(spread_day);\r
129                 }\r
130         }\r
131 }\r
132 }}}\r
133 \r
134 ----\r
135 CategoryCodeSnippets