@@ -13,17 +13,18 @@ interface
1313 TWeatherCity = class
1414 public
1515 CityName: string;
16- MinTemp: Single ;
17- MaxTemp: Single ;
16+ MinTemp: Integer ;
17+ MaxTemp: Integer ;
1818 DataCount: Int64;
19- TotalTemp: Double ;
20- constructor Create(const NewCityName: string; const NewTemp: single );
21- procedure AddNewTemp (const NewTemp: Single );
19+ TotalTemp: Int64 ;
20+ constructor Create(const NewCityName: string; const NewTemp: Integer );
21+ procedure AddNewTemp (const NewTemp: Integer );
2222 function Mean : Double;
23+ function OutputSumLine : string;
2324 end ;
2425
2526 // anonymous method type
26- TWeatherCityProc = reference to procedure(const City: string; const Temp: Double );
27+ TWeatherCityProc = reference to procedure(const City: string; const Temp: Integer );
2728
2829 // a global class to provide standard open/close and other functions
2930 TChallengeCommon = class
@@ -36,7 +37,7 @@ TChallengeCommon = class
3637 procedure CloseWeatherData ;
3738 procedure ReadAndParseAllData (WeatherCityProcessor: TWeatherCityProc);
3839 function PascalRound (x: Double): Double;
39- function SplitCityTemp (const StationLine: string; var CityName: string; var CityTemp: Double ): Boolean;
40+ function SplitCityTemp (const StationLine: string; var CityName: string; var CityTemp: Integer ): Boolean;
4041 property InputFilename: string read FInputFilename write FInputFilename;
4142 property WeatherDataFile: TextFile read FWeatherDataFile;
4243 end ;
@@ -47,7 +48,11 @@ TChallengeCommon = class
4748implementation
4849
4950uses
50- System.Classes, System.SysUtils, System.Math;
51+ System.Classes, System.SysUtils,
52+ { $IFDEF DEBUG}
53+ System.Diagnostics,
54+ { $ENDIF }
55+ System.Math;
5156
5257{ TChallengeCommon }
5358
@@ -88,14 +93,23 @@ procedure TChallengeCommon.ReadAndParseAllData(WeatherCityProcessor: TWeatherCit
8893var
8994 WeatherLine: string;
9095 WeatherCity: string;
91- CityTemp: Double;
96+ CityTemp: Integer;
97+ LineCount: Int64;
9298begin
99+ { $IFDEF DEBUG}
100+ var StopWatch := TStopwatch.StartNew;
101+ LineCount := 0 ;
102+ { $ENDIF}
103+
93104 OpenWeatherData;
94105 try
95106 // read all rows
96107 while not Eof(WeatherDataFile) do begin
97108 // read a row into a temp string
98109 Readln(WeatherDataFile, WeatherLine);
110+ { $IFDEF DEBUG}
111+ Inc(LineCount);
112+ { $ENDIF}
99113
100114 // parse the data and call a procedure to process it
101115 if SplitCityTemp(WeatherLine, WeatherCity, CityTemp) then
@@ -104,14 +118,20 @@ procedure TChallengeCommon.ReadAndParseAllData(WeatherCityProcessor: TWeatherCit
104118 finally
105119 ChallengeCommon.CloseWeatherData;
106120 end ;
121+
122+ { $IFDEF DEBUG}
123+ StopWatch.Stop;
124+ Writeln(Format(' Loaded %d lines from %s in %d milliseconds' , [LineCount, FInputFilename,
125+ StopWatch.ElapsedMilliseconds]));
126+ { $ENDIF}
107127end ;
108128
109129procedure TChallengeCommon.CloseWeatherData ;
110130begin
111131 CloseFile(FWeatherDataFile);
112132end ;
113133
114- function TChallengeCommon.SplitCityTemp (const StationLine: string; var CityName: string; var CityTemp: Double ): Boolean;
134+ function TChallengeCommon.SplitCityTemp (const StationLine: string; var CityName: string; var CityTemp: Integer ): Boolean;
115135var
116136 SemiPos: Integer;
117137 TempStr: string;
@@ -126,14 +146,16 @@ function TChallengeCommon.SplitCityTemp(const StationLine: string; var CityName:
126146 Exit;
127147
128148 CityName := Copy(StationLine, 1 , SemiPos - 1 );
129- TempStr := Copy(StationLine, SemiPos + 1 , 50 );
149+ TempStr := Trim(Copy(StationLine, SemiPos + 1 , 50 ));
150+ // remove the decimal point to make this an integer
151+ CityTemp := StrToInt(Copy(TempStr, 1 , Length(TempStr) - 2 ) + Copy(TempStr, Length(TempStr), 1 ));
130152
131- Result := TryStrToFloat(TempStr, CityTemp) ;
153+ Result := True ;
132154end ;
133155
134156{ TWeatherCity }
135157
136- procedure TWeatherCity.AddNewTemp (const NewTemp: Single );
158+ procedure TWeatherCity.AddNewTemp (const NewTemp: Integer );
137159begin
138160 Inc(DataCount);
139161
@@ -147,7 +169,7 @@ procedure TWeatherCity.AddNewTemp(const NewTemp: Single);
147169 TotalTemp := TotalTemp + NewTemp;
148170end ;
149171
150- constructor TWeatherCity.Create(const NewCityName: string; const NewTemp: single );
172+ constructor TWeatherCity.Create(const NewCityName: string; const NewTemp: Integer );
151173begin
152174 CityName := NewCityName;
153175 DataCount := 1 ;
@@ -161,4 +183,13 @@ function TWeatherCity.Mean: Double;
161183 Result := ChallengeCommon.PascalRound(TotalTemp / DataCount);
162184end ;
163185
186+ function TWeatherCity.OutputSumLine : string;
187+ begin
188+ Result := Format(' %s=%0.1f/%0.1f/%0.1f' , [CityName,
189+ MinTemp / 10 ,
190+ Mean,
191+ MaxTemp / 10 ]);
192+
193+ end ;
194+
164195end .
0 commit comments