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