Kernighans large earthquake problem: Difference between revisions
Kernighans large earthquake problem (view source)
Revision as of 11:10, 12 December 2023
, 5 months ago→{{header|Wren}}: Minor tidy
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
m (→{{header|Wren}}: Minor tidy) |
||
(5 intermediate revisions by 3 users not shown) | |||
Line 1:
{{task}}
[
;Problem:
Line 1,007:
First, with a data file. This adds a fair amount of verbosity to COBOL. For something this one-off, a simpler cut using ACCEPT from standard in is shown.
<syntaxhighlight lang="
*> Kernighan large earthquake problem
*> Tectonics: cobc -xj kernighan-earth-quakes.cob
*>
*>
>>SOURCE FORMAT IS FREE
IDENTIFICATION DIVISION.
PROGRAM-ID. quakes.
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
ASSIGN
ORGANIZATION IS
STATUS
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 quake-fd-status
88 ok
"05", "06", "07", "08", "09".
88 no-more
88 io-error
01 line-length USAGE IS BINARY-LONG.
01 date-time PICTURE IS X(10).
01 quake PICTURE IS X(20).
01 magnitude PICTURE IS 99V99.
01 command-filename
PROCEDURE DIVISION.
show-big-ones.
OPEN
DISPLAY
PERFORM
IF magnitude
*>
IF
EXIT
END
{{output}}
Line 1,110 ⟶ 1,108:
<syntaxhighlight lang="cobol"> *>
*> Tectonics: ./kerighan-earth-quakes <quakes.txt
01 data-line
88 no-more
01 date-time
01 quake
01 magnitude
show-big-ones.
That cut would be used as <pre>prompt$ ./kernighans-large-earthquakes <quakes.txt</pre>
Line 1,240 ⟶ 1,238:
5/18/1980 MountStHelens 7.6
1/25/4567 EdgeCase3 6.1</pre>
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|Controls,SysUtils,Classes,StdCtrls,ExtCtrls}}
This code takes advantage of the standard Delphi "TStringGrid" object to do most of the heavy lifting. It is initially used to read the earthquake file into memory, breaking it up into of individual lines as it goes. Then the individual fields are stored in structurs/records attached to the data. finally, the data is sorted by magnitude so the earthquakes of magnitudes greater than six can be extracted. Because the data is now neatly organized in memory, all kinds of other process could be done, including sorting it by date or location. To make the problem more realistic, I extracted actual earthquake data from the first few months of 2023. I've post the data for other people to test here: [https://fountainware.com/download/EarthQuakes.txt EarthQuakes.txt]
<syntaxhighlight lang="Delphi">
{Structure used to contain all the earthquake data}
type TQuakeInfo = record
Date: TDate;
Name: string;
Mag: double;
end;
type PQuakeInfo = ^TQuakeInfo;
{Used to contain individual fields of the earthquake data}
type TStringArray = array of string;
function SortCompare(List: TStringList; Index1, Index2: Integer): Integer;
{Custom sort routine to sort data by magnitude }
var QI1,QI2: TQuakeInfo;
begin
QI1:=PQuakeInfo(List.Objects[Index1])^;
QI2:=PQuakeInfo(List.Objects[Index2])^;
Result:=Round(QI2.Mag*10)-Round(QI1.Mag*10);
end;
procedure GetFields(S: string; var SA: TStringArray);
{Extract the three fields from each row of data}
var I,F: integer;
begin
SetLength(SA,3);
for I:=0 to High(SA) do SA[I]:='';
F:=0;
for I:=1 to Length(S) do
if S[I] in [#$09,#$20] then Inc(F)
else SA[F]:=SA[F]+S[I];
end;
procedure AnalyzeEarthQuakes(Filename: string; Memo: TMemo);
{Read earhtquake data from specified file}
{Extract the individual fields and sort and display it}
var SL: TStringList;
var I: integer;
var S: string;
var FA: TStringArray;
var QI: PQuakeInfo;
begin
SL:=TStringList.Create;
try
{Read file, separating it into rows}
SL.LoadFromFile(Filename);
{Process each row}
for I:=0 to SL.Count-1 do
begin
S:=SL[I];
{Separate row into fields}
GetFields(S,FA);
{Store data as objects in TStringList}
New(QI);
QI.Date:=StrToDate(FA[0]);
QI.Name:=FA[1];
QI.Mag:=StrToFloat(FA[2]);
SL.Objects[I]:=TObject(QI);
end;
{Sort data by magnitude}
SL.CustomSort(SortCompare);
{Display sorted data}
for I:=0 to SL.Count-1 do
begin
if PQuakeInfo(SL.Objects[I]).Mag<6 then break;
S:=FormatDateTime('dd/mm/yyyy', PQuakeInfo(SL.Objects[I]).Date);
S:=S+Format(' %-34s',[PQuakeInfo(SL.Objects[I]).Name]);
S:=S+Format(' %5f',[PQuakeInfo(SL.Objects[I]).Mag]);
Memo.Lines.Add(S);
end;
{Dispose of memory}
finally
for I:=0 to SL.Count-1 do Dispose(PQuakeInfo(SL.Objects[I]));
SL.Free;
end;
end;
procedure ShowEarthQuakes(Memo: TMemo);
begin
AnalyzeEarthQuakes('EarthQuakes.txt',Memo);
end;
</syntaxhighlight>
{{out}}
<pre>
06/02/2023 Turkey_Kahramanmaras 7.80
09/01/2023 Indonesia_Maluku 7.60
06/02/2023 Turkey_Kahramanmaras 7.50
02/04/2023 Papua_New_Guinea_East_Sepik 7.00
16/03/2023 New_Zealand_Kermadec_Islands 7.00
18/01/2023 Indonesia_North_Maluku 7.00
14/04/2023 Indonesia_East_Java 7.00
08/01/2023 Vanuatu_Sanma 7.00
04/03/2023 New_Zealand_Kermadec_Islands 6.90
23/02/2023 Tajikistan_Gorno-Badakhshan 6.90
18/03/2023 Ecuador_Guayas 6.80
20/01/2023 Argentina_Santiago_del_Estero 6.80
18/04/2023 South_of_theFiji_Islands 6.70
06/02/2023 Turkey_Gaziantep 6.70
01/03/2023 Papua_New_Guinea_West_New_Britain 6.60
02/03/2023 Vanuatu_Sanma 6.50
03/04/2023 Russia_Kamchatka_Krai 6.50
22/03/2023 Argentina_Jujuy 6.50
21/03/2023 Afghanistan_Badakhshan 6.50
24/01/2023 Argentina_Santiago_del_Estero 6.40
19/04/2023 Papua_New_Guinea_West_New_Britain 6.30
16/01/2023 Japan_Bonin_Islands 6.30
20/02/2023 Turkey_Hatay 6.30
23/02/2023 Indonesia_North_Maluku 6.30
21/04/2023 Indonesia_Southeast_Sulawesi 6.30
14/03/2023 Papua_New_Guinea_Madang 6.30
30/03/2023 Chile_Maule 6.30
04/04/2023 Panama_Chiriqu- 6.30
25/02/2023 Papua_New_Guinea_West_New_Britain 6.20
04/04/2023 Philippines_Bicol 6.20
27/03/2023 Solomon_Islands_Isabel 6.10
03/04/2023 Indonesia_North_Sumatra 6.10
17/02/2023 Indonesia_Maluku 6.10
15/02/2023 Philippines_Bicol 6.10
13/02/2023 New_Zealand_Kermadec_Islands 6.10
20/01/2023 France_Guadeloupe 6.10
15/01/2023 Indonesia_Aceh 6.10
28/03/2023 Japan_Hokkaido 6.00
18/01/2023 Indonesia_Gorontalo 6.00
01/02/2023 Philippines_Davao 6.00
05/01/2023 Afghanistan_Badakhshan 6.00
26/01/2023 New_Zealand_Kermadec_Islands 6.00
06/02/2023 Turkey_Kahramanmaras 6.00
06/02/2023 Turkey_Malatya 6.00
25/02/2023 Japan_Hokkaido 6.00
13/04/2023 Canada_British_Columbia 6.00
</pre>
=={{header|D}}==
Line 2,198 ⟶ 2,340:
=={{header|Wren}}==
{{libheader|Wren-pattern}}
<syntaxhighlight lang="
import "os" for Process
import "./pattern" for Pattern
var args = Process.arguments
|