IDENTIFICATION DIVISION.
PROGRAM-ID. CALCULATOR.
AUTHOR. Kevin Roper.
INSTALLATION. KEYHOLE HQ.
DATE-WRITTEN. 05/25/2020.

ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT CALCULATOR-TAPE ASSIGN TO "CALCULATOR-TAPE.DAT"
		ORGANIZATION IS LINE SEQUENTIAL.

DATA DIVISION.
FILE SECTION.
FD CALCULATOR-TAPE.
01 CALCULATION-RECORD.
   88 END-OF-CALC-FILE     VALUE HIGH-VALUES.
   02 F-OPERAND-A            PIC 9(8).
   02 F-OPERAND-B            PIC 9(8).
   02 F-OPERATOR             PIC A.
   02 F-ANSWER               PIC 9(16).
   02 F-ERROR-MESSAGE        PIC X(30).
   02 F-META-DATA.
       05 DATE-OF-CALCULATION.
           10 CALCULATION-YEAR     PIC 9(2).
           10 CALCULATION-MONTH    PIC 9(2).
           10 CALCULATION-DAY      PIC 9(2).

WORKING-STORAGE SECTION.
01 ERROR-MESSAGE   PIC X(30).
01 OPERAND-A       PIC 9(8).
01 OPERAND-B       PIC 9(8).
01 OPERATOR        PIC A.
01 ANSWER          PIC 9(16).

01 ERROR-MESSAGES.
       05 CLEARED-MESSAGE  PIC A(30) VALUE IS "                             ".
       05 OVERFLOW-MESSAGE PIC A(30) VALUE IS "ERROR: OVERFLOW              ".
       05 SIGN-MESSAGE     PIC A(30) VALUE IS "ERROR: LESS THAN ZERO        ".
       05 MISC-MESSAGE     PIC A(30) VALUE IS "ERROR: OTHER ERROR           ".

SCREEN SECTION.
01 MATH-SCREEN.
       05 BACKGROUND-COLOR 1 FOREGROUND-COLOR 7.
       05 LINE 1 COLUMN 30 VALUE "INTEGER CALCULATOR v0.1".
       05 LINE 10 COLUMN 5 VALUE "A=ADD S=SUBTRACT M=MULTIPLY D=DIVIDE Q=QUIT".
       05 LINE 3 COLUMN 5 VALUE "OPERAND 1".
       05 LINE 4 COLUMN 5 VALUE "OPERAND 2".
       05 LINE 5 COLUMN 5 VALUE "OPERATOR".
       05 LINE 7 COLUMN 5 VALUE "ANSWER".

       05 SCR-OPERAND-A LINE 3 COLUMN 20 PIC 9(8) TO OPERAND-A.
       05 SCR-OPERAND-B LINE 4 COLUMN 20 PIC 9(8) TO OPERAND-B.
       05 SCR-OPERATOR  LINE 5 COLUMN 20 PIC A TO OPERATOR.
       05 SCR-ANSWER    LINE 7 COLUMN 20 PIC 9(16) FROM ANSWER.
       05 SCR-ERROR     LINE 8 COLUMN 20 PIC X(30) FROM ERROR-MESSAGE.

PROCEDURE DIVISION.
PERFORM MATH-SCREEN-LOOP UNTIL OPERATOR="Q".
STOP RUN.

MATH-SCREEN-LOOP.
DISPLAY MATH-SCREEN.
ACCEPT MATH-SCREEN.
PERFORM CLEAR-ERROR-MESSAGE.
PERFORM DO-THE-MATH.

DO-THE-MATH.
IF OPERATOR="A" THEN PERFORM DO-ADD.
IF OPERATOR="S" THEN PERFORM DO-SUBTRACT.
IF OPERATOR="M" THEN PERFORM DO-MULTIPLY.
IF OPERATOR="D" THEN PERFORM DO-DIVIDE.
PERFORM WRITE-TAPE.

DO-ADD.
ADD OPERAND-A TO OPERAND-B GIVING ANSWER.

DO-SUBTRACT.
IF OPERAND-B IS GREATER THAN OPERAND-A THEN 
       PERFORM SIGN-ERROR
ELSE
       SUBTRACT OPERAND-B FROM OPERAND-A GIVING ANSWER
END-IF.

DO-MULTIPLY.
MULTIPLY OPERAND-A BY OPERAND-B GIVING ANSWER.

DO-DIVIDE.     
DIVIDE OPERAND-A BY OPERAND-B GIVING ANSWER.

CHECK-OVERFLOW.
IF ANSWER IS GREATER THAN 9999999999999999 THEN
       MOVE 1111111111111111 TO ANSWER.

WRITE-TAPE.
OPEN EXTEND CALCULATOR-TAPE
MOVE OPERAND-A TO F-OPERAND-A.
MOVE OPERAND-B TO F-OPERAND-B.
MOVE ANSWER TO F-ANSWER.
MOVE OPERATOR TO F-OPERATOR.
MOVE ERROR-MESSAGE TO F-ERROR-MESSAGE.
MOVE "200101" TO F-META-DATA.
WRITE CALCULATION-RECORD.
CLOSE CALCULATOR-TAPE.

SIGN-ERROR.
MOVE SIGN-MESSAGE IN ERROR-MESSAGES TO ERROR-MESSAGE.
MOVE 8888888888888888 TO ANSWER.
DISPLAY SCR-ANSWER REVERSE-VIDEO.

MISC-ERROR.
MOVE MISC-MESSAGE IN ERROR-MESSAGES TO ERROR-MESSAGE.
MOVE 8888888888888888 TO ANSWER.

OVERFLOW-ERROR.
MOVE OVERFLOW-MESSAGE IN ERROR-MESSAGES TO ERROR-MESSAGE.
MOVE 8888888888888888 TO ANSWER.

CLEAR-ERROR-MESSAGE.
MOVE CLEARED-MESSAGE IN ERROR-MESSAGES TO ERROR-MESSAGE.
