Skip to content

Commit

Permalink
All variable names upper-cased.
Browse files Browse the repository at this point in the history
Rework the initialization of the Allergens table according to "Beginning COBOL for Programmers", ch 11.
  • Loading branch information
glennj committed Jun 23, 2023
1 parent 0bc60a9 commit 3164ab3
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions exercises/practice/allergies/.meta/proof.ci.cob
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,28 @@
01 WS-RESULT PIC A.
01 WS-RESULT-LIST PIC X(108).

01 Allergens VALUE "eggs peanuts shellfish strawberriestomatoes chocolate pollen cats ".
02 Allergen PIC X(12) OCCURS 8 TIMES.
01 ALLERGENS.
02 ALLERGEN-NAMES.
03 FILLER PIC X(24) VALUE "eggs peanuts".
03 FILLER PIC X(24) VALUE "shellfish strawberries".
03 FILLER PIC X(24) VALUE "tomatoes chocolate".
03 FILLER PIC X(24) VALUE "pollen cats".
02 FILLER REDEFINES ALLERGEN-NAMES.
03 ALLERGEN PIC X(12) OCCURS 8 TIMES
INDEXED BY INDEX-1 INDEX-2.

01 idx PIC 9.
01 idx2 PIC 9.
01 allergen-value PIC 999.
01 shifted PIC 999.
01 list-separator PIC X.
01 ALLERGEN-VALUE PIC 999.
01 SHIFTED PIC 999.
01 LIST-SEPARATOR PIC X.

PROCEDURE DIVISION.
ALLERGIC-TO.
MOVE "N" TO WS-RESULT
PERFORM VARYING idx FROM 1 BY 1 UNTIL idx > 8
IF Allergen(idx) EQUAL TO WS-ITEM THEN
COMPUTE allergen-value = 2 ** (idx - 1)
DIVIDE WS-SCORE BY allergen-value GIVING shifted
IF FUNCTION REM(shifted; 2) EQUAL TO 1 THEN
PERFORM VARYING INDEX-1 FROM 1 BY 1 UNTIL index-1 > 8
IF ALLERGEN(INDEX-1) EQUAL TO WS-ITEM THEN
COMPUTE ALLERGEN-VALUE = 2 ** (INDEX-1 - 1)
DIVIDE WS-SCORE BY ALLERGEN-VALUE GIVING SHIFTED
IF FUNCTION REM(SHIFTED; 2) EQUAL TO 1 THEN
MOVE "Y" TO WS-RESULT
EXIT PERFORM
END-IF
Expand All @@ -33,17 +38,17 @@

LIST-ALLERGENS.
MOVE SPACES TO WS-RESULT-LIST
MOVE SPACE TO list-separator
PERFORM VARYING idx2 FROM 1 BY 1 UNTIL idx2 > 8
MOVE Allergen(idx2) TO WS-ITEM
MOVE SPACE TO LIST-SEPARATOR
PERFORM VARYING INDEX-2 FROM 1 BY 1 UNTIL index-2 > 8
MOVE ALLERGEN(INDEX-2) TO WS-ITEM
PERFORM ALLERGIC-TO
IF WS-RESULT EQUAL TO "Y" THEN
STRING
WS-RESULT-LIST DELIMITED BY SPACE
list-separator DELIMITED BY SPACE
Allergen(idx2) DELIMITED BY SPACE
LIST-SEPARATOR DELIMITED BY SPACE
ALLERGEN(INDEX-2) DELIMITED BY SPACE
INTO WS-RESULT-LIST
END-STRING
MOVE "," TO list-separator
MOVE "," TO LIST-SEPARATOR
END-IF
END-PERFORM.

0 comments on commit 3164ab3

Please sign in to comment.