Jump to content

Kernighans large earthquake problem: Difference between revisions

→‎{{header|COBOL}}: Changed to conventional case + sugar; revert if necessary.
m (link in Wikisyntax)
imported>Acediast
(→‎{{header|COBOL}}: Changed to conventional case + sugar; revert if necessary.)
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="cobolcobolfree">*>
*> Kernighan large earthquake problem
*>
*> Tectonics: cobc -xj kernighan-earth-quakes.cob
*> Kernighan large earthquake problem
*> *> Tectonics: cobc -xj kernighan-earth-quakes.cobtxt with the 3 sample lines
*> ./kernighan-earth-quakes.txt with the 3 sample lines
*>
*> ./kernighan-earth-quakes
>>SOURCE FORMAT IS FREE
*>
IDENTIFICATION DIVISION.
>>SOURCE FORMAT IS FREE
PROGRAM-ID. quakes.
identification division.
program-id. quakes.
 
ENVIRONMENT DIVISION.
environment division.
CONFIGURATION SECTION.
configuration section.
REPOSITORY.
repository.
FUNCTION ALL INTRINSIC.
function all intrinsic.
 
INPUT-OUTPUT SECTION.
input-output section.
FILE-CONTROL.
file-control.
selectSELECT quake-data
ASSIGN assign toTO command-filename
ORGANIZATION IS organizationLINE is line sequentialSEQUENTIAL
STATUS status isIS quake-fd-status.
.
 
DATA DIVISION.
data division.
FILE SECTION.
file section.
fd FD quake-data recordRECORD varyingVARYING dependingDEPENDING onON line-length.
01 data-line pic x PICTURE IS X(32768).
 
WORKING-STORAGE SECTION.
working-storage section.
01 quake-fd-status 01 quake-fd-statusPICTURE picIS xxXX.
88 ok 88 ok values VALUES ARE "00", "01", "02", "03", "04",
"05", "06", "07", "08", "09".
88 no-more 88 no-more value VALUE IS "10".
88 io-error 88 io-error value high VALUE IS HIGH-valueVALUE.
 
01 line-length USAGE IS BINARY-LONG.
01 line-length usage binary-long.
01 date-time PICTURE IS X(10).
01 quake PICTURE IS X(20).
01 magnitude PICTURE IS 99V99.
 
01 command-filename 01 date-timePICTURE picIS xX(1080).
01 quake pic x(20).
01 magnitude pic 99v99.
 
PROCEDURE DIVISION.
01 command-filename pic x(80).
show-big-ones.
procedure division.
show-big-ones.
 
acceptACCEPT command-filename fromFROM commandCOMMAND-lineLINE
ifIF command-filename equalIS EQUAL TO spacesSPACES thenTHEN
moveMOVE "data.txt" toTO command-filename
endEND-ifIF
OPEN INPUT quake-data
PERFORM status-check
IF io-error THEN
DISPLAY TRIM(command-filename) " not found" UPON SYSERR
GOBACK
END-IF
READ quake-data
PERFORM status-check
PERFORM UNTIL no-more OR io-error
UNSTRING data-line DELIMITED BY ALL SPACES
INTO date-time quake magnitude
END-UNSTRING
IF magnitude IS GREATER THAN 6
DISPLAY date-time SPACE quake SPACE magnitude
END-IF
READ quake-data
PERFORM status-check
END-PERFORM
CLOSE quake-data
PERFORM status-check
GOBACK.
*> ****
 
status-check.
open input quake-data
IF NOT ok AND NOT no-more THEN *> not normal status, bailing
perform status-check
if DISPLAY "io- error: " quake-fd-status UPON thenSYSERR
SET io-error TO TRUE
display trim(command-filename) " not found" upon syserr
gobackEND-IF
EXIT end-ifPARAGRAPH.
 
END PROGRAM quakes.</syntaxhighlight>
read quake-data
perform status-check
perform until no-more or io-error
unstring data-line delimited by all spaces
into date-time quake magnitude
end-unstring
 
if magnitude greater than 6
display date-time space quake space magnitude
end-if
 
read quake-data
perform status-check
end-perform
 
close quake-data
perform status-check
goback.
*> ****
 
status-check.
if not ok and not no-more then *> not normal status, bailing
display "io error: " quake-fd-status upon syserr
set io-error to true
end-if
.
 
end program quakes.</syntaxhighlight>
 
{{output}}
Line 1,110 ⟶ 1,108:
<syntaxhighlight lang="cobol"> *>
*> Tectonics: ./kerighan-earth-quakes <quakes.txt
identificationIDENTIFICATION divisionDIVISION.
programPROGRAM-idID. quakes.
 
dataDATA divisionDIVISION.
 
workingWORKING-storageSTORAGE sectionSECTION.
01 data-line pic x PICTURE IS X(32768).
88 no-more value high VALUE IS HIGH-valuesVALUES.
 
01 date-time pic x PICTURE IS X(10).
01 quake pic x PICTURE IS X(20).
01 magnitude pic 99v99 PICTURE IS 99V99.
 
procedurePROCEDURE divisionDIVISION.
show-big-ones.
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
performPERFORM untilUNTIL no-more
unstringUNSTRING data-line delimitedDELIMITED byBY allALL spacesSPACES
intoINTO date-time quake magnitude
endEND-unstringUNSTRING
 
ifIF magnitude greaterIS thanGREATER THAN 6
displayDISPLAY date-time spaceSPACE quake spaceSPACE magnitude
endEND-ifIF
 
acceptACCEPT data-line onON exceptionEXCEPTION setSET no-more toTO trueTRUE endEND-acceptACCEPT
endEND-performPERFORM
 
gobackGOBACK.
endEND programPROGRAM quakes.</syntaxhighlight>
 
That cut would be used as <pre>prompt$ ./kernighans-large-earthquakes <quakes.txt</pre>
 
 
=={{header|Cowgol}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.