aboutsummaryrefslogtreecommitdiff
path: root/1brc.cbl
blob: 6b8b2b27ed565b90368a17b09c1510860a88dab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
       IDENTIFICATION DIVISION.
       PROGRAM-ID. 1brc.
       AUTHOR. Trey Bastian.

       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
       SELECT measurements-file ASSIGN TO "./measurements.txt"
           ORGANIZATION IS LINE SEQUENTIAL.
       SELECT sorted-measurements ASSIGN TO OUTPUT1.
       SELECT workfile ASSIGN TO WORK1.
       DATA DIVISION.
       FILE SECTION.
       FD measurements-file.
       01 measurement.
           02 line-item PIC X(106).
       FD sorted-measurements.
       01 measurement.
           02 line-item PIC X(106).
       SD workfile.
       01 measurement.
           02 line-item PIC X(106).

       WORKING-STORAGE section.
       01 pic x.
             88 eof VALUE "Y".
             88 eof-n VALUE "N".

       01 pic x.
             88 is-first VALUE "Y".
             88 not-first VALUE "N".

       77 s-name PIC X(100).
       77 temp PIC S9(2)V9.

       77 station-name PIC X(100).
       77 min-temp PIC S9(2)V9 VALUE ZEROS.
       77 max-temp PIC S9(2)V9 VALUE ZEROS.
       77 total PIC S9(11)V9(2) VALUE ZEROS.
       77 cnt PIC S9(11) VALUE ZEROS.

       77 temp-str PIC -(2)9.9 VALUE ZEROS.
       77 mean-calc PIC S9(2)V9 VALUE ZEROS.


       PROCEDURE DIVISION.
           SET is-first TO TRUE.
           OPEN INPUT measurements-file.
           SORT workfile ON ASCENDING line-item OF workfile
           USING measurements-file
           GIVING sorted-measurements

           OPEN INPUT sorted-measurements.
           SET eof-n TO TRUE.
           PERFORM UNTIL eof
             READ sorted-measurements AT END
                 SET eof TO TRUE
             NOT AT END

               UNSTRING line-item of sorted-measurements DELIMITED BY
               ";" INTO s-name, temp
               END-UNSTRING

               IF s-name = station-name THEN
                 IF min-temp > temp THEN
                   MOVE temp to min-temp
                  END-IF
                  IF max-temp < temp THEN
                    MOVE temp to max-temp
                  END-IF
                  ADD temp TO total
                  ADD 1 TO cnt
               ELSE
                 IF not-first THEN
                 PERFORM display-procedure
                 END-IF
                 MOVE s-name TO station-name
                 MOVE temp TO min-temp
                 MOVE temp TO max-temp
                 MOVE temp to total
                 MOVE 1 to cnt
                 IF is-first THEN
                   SET not-first TO TRUE
                 END-IF
               END-IF
             END-READ
           END-PERFORM.
           ClOSE sorted-measurements.
       STOP-RUN.

       display-procedure.
           DISPLAY FUNCTION TRIM(station-name TRAILING)
                WITH NO ADVANCING
              DISPLAY ";" WITH NO ADVANCING
              MOVE min-temp TO temp-str
              DISPLAY FUNCTION TRIM(temp-str LEADING)
                WITH NO ADVANCING
              DISPLAY ";" WITH NO ADVANCING
              COMPUTE mean-calc ROUNDED = total / cnt
              MOVE mean-calc TO temp-str
              DISPLAY FUNCTION TRIM(temp-str LEADING)
                WITH NO ADVANCING
              DISPLAY ";" WITH NO ADVANCING
              MOVE max-temp TO temp-str
              DISPLAY FUNCTION TRIM(temp-str LEADING).