Kernighans large earthquake problem: Difference between revisions

Content added Content deleted
Line 390: Line 390:
$
$
</pre>
</pre>
<p>PROBLEM: What if the file contains more information than expected? We would have to validate the lines that can be processed. For example, we might have a file with the following information:
</p>
<pre>
8/27/1883 Krakatoa 8.8
5/18/1980 MountStHelens 7.6
3/13/2009 CostaRica 5.1
Total events: 3

1/23/4567 EdgeCase1 6

1/24/4567 EdgeCase2 6.0
1/25/4567 EdgeCase3 6.1
1/25/4567 EdgeCase4 6.1
Total events: 4

Total Events in periods: 7
</pre>
<p>There are lines with 3 tokens, and the third token is numeric: it would be shown as a valid record!</p>
<p>On the other hand, HOPPER processes dates in DD/MM/YYYY format, and the file records dates in MM/DD/YYYY format: therefore, it is necessary to exchange "DD" for "MM", because HOPPER does not allow other types of format for "dates".</p>
<p>The final program would be as follows:</p>
<lang Amazing Hopper>
/* Kernighans large earthquake problem. */

#include <flow.h>
#include <flow-flow.h>

#define MAX_LINE 1000
#define SwapDayByMonth(_N_,_M_) TOK-SEP("/"), TOK(1), TOK-SWAP(2,Event), TOK-SEP(" ")

DEF-MAIN(argv,argc)
MSET(fd, Event )
TOK-INIT
OPEN-INPUT("datos.txt")(fd)
COND( IS-NOT-FILE-ERROR? )
TOK-SEP( " " )
TIC(t1)
WHILE( NOT( EOF(fd) ) )
LET( Event := USING(MAX_LINE) READ-LINE(fd) APPLY-TRM )
WHEN( LEN(Event) ){
WHEN( EQ?(TOK-COUNT( Event ),3) ){
Swap Day By Month(1,2)
WHEN( IS-DATE-VALID?( TOK(1) TOK-GET(Event) )){
TOK(3)
WHEN( GT?( VAL(TOK-GET(Event)), 6 ) ){
PRNL( Event )
}
}
}
}
WEND
TOC( t1, t2 ), PRNL( "Time = ", t2 )
CLOSE(fd)
ELS
PRNL("Error: ", ~GET-STR-FILE-ERROR )
CEND
END
</lang>


=={{header|AppleScript}}==
=={{header|AppleScript}}==