2
1
Fork 0

Day 2: Add COBOL solution

This commit is contained in:
Mia Herkt 2021-12-02 14:31:44 +01:00
parent 1ce45e6d9f
commit 03f4a91463
Signed by: mia
GPG Key ID: 72E154B8622EC191
2 changed files with 100 additions and 0 deletions

49
02/SLTN1.CBL Normal file
View File

@ -0,0 +1,49 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. DAY2PT1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.txt'
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD PIC A(16).
WORKING-STORAGE SECTION.
01 WS-COMMAND.
05 WS-CMD PIC A(16).
05 WS-VAL PIC 9(1).
01 WS-H PIC 9(10) USAGE IS COMP.
01 WS-V PIC 9(10) USAGE IS COMP.
01 WS-RESULT PIC Z(10).
PROCEDURE DIVISION.
START-ROUTINE.
OPEN INPUT INPUT-FILE.
READ-ROUTINE.
MOVE SPACES TO INPUT-RECORD.
MOVE SPACES TO WS-COMMAND.
READ INPUT-FILE AT END GO TO END-ROUTINE.
UNSTRING INPUT-RECORD DELIMITED BY SPACE
INTO WS-CMD, WS-VAL.
IF WS-CMD = 'forward' THEN
ADD WS-VAL TO WS-H
END-IF
IF WS-CMD = 'down' THEN
ADD WS-VAL TO WS-V
END-IF
IF WS-CMD = 'up' THEN
SUBTRACT WS-VAL FROM WS-V
END-IF
GO TO READ-ROUTINE.
END-ROUTINE.
CLOSE INPUT-FILE.
MULTIPLY WS-H BY WS-V GIVING WS-RESULT.
DISPLAY 'Answer: 'WS-RESULT.
STOP RUN.

51
02/SLTN2.CBL Normal file
View File

@ -0,0 +1,51 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. DAY2PT2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT INPUT-FILE ASSIGN TO 'input.txt'
ORGANIZATION IS LINE SEQUENTIAL
ACCESS IS SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD INPUT-FILE.
01 INPUT-RECORD PIC A(16).
WORKING-STORAGE SECTION.
01 WS-COMMAND.
05 WS-CMD PIC A(16).
05 WS-VAL PIC 9(1).
01 WS-AIM PIC 9(10) USAGE IS COMP.
01 WS-H PIC 9(10) USAGE IS COMP.
01 WS-V PIC 9(10) USAGE IS COMP.
01 WS-RESULT PIC Z(10).
PROCEDURE DIVISION.
START-ROUTINE.
OPEN INPUT INPUT-FILE.
READ-ROUTINE.
MOVE SPACES TO INPUT-RECORD.
MOVE SPACES TO WS-COMMAND.
READ INPUT-FILE AT END GO TO END-ROUTINE.
UNSTRING INPUT-RECORD DELIMITED BY SPACE
INTO WS-CMD, WS-VAL.
IF WS-CMD = 'forward' THEN
ADD WS-VAL TO WS-H
COMPUTE WS-V=WS-V+WS-AIM*WS-VAL
END-IF
IF WS-CMD = 'down' THEN
ADD WS-VAL TO WS-AIM
END-IF
IF WS-CMD = 'up' THEN
SUBTRACT WS-VAL FROM WS-AIM
END-IF
GO TO READ-ROUTINE.
END-ROUTINE.
CLOSE INPUT-FILE.
MULTIPLY WS-H BY WS-V GIVING WS-RESULT.
DISPLAY 'Answer: 'WS-RESULT.
STOP RUN.