Transcript ADD-ANS

Modifications to program
Addda.cbl
Please use speaker notes for
additional information!
ADDDA.CBL
Two other ways
that the ADD
statement could
have been
written - shown
as comments.
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES
05 FIRST-NUMBER
PIC 999
VALUE 0.
05 SECOND-NUMBER
PIC 999
VALUE 0.
05 ADD-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
ACCEPT FIRST-NUMBER.
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
ACCEPT SECOND-NUMBER.
ADD FIRST-NUMBER TO SECOND-NUMBER
GIVING ADD-ANS.
*
ADD FIRST-NUMBER SECOND-NUMBER
*
GIVING ADD-ANS.
*
ADD FIRST-NUMBER, SECOND-NUMBER
*
GIVING ADD-ANS.
DISPLAY "THE ANSWER IS " ADD-ANS.
DISPLAY "PRESS ENTER TO END THE PROGRAM".
ACCEPT GET-ACCEPT-ANS.
STOP RUN.
ADDDA1.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES.
05 FIRST-NUMBER
PIC 999
VALUE 0.
05 SECOND-NUMBER
PIC 999
VALUE 0.
05 ADD-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
The COMPUTE
ACCEPT FIRST-NUMBER.
statement is an
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
alternate way to do
ACCEPT SECOND-NUMBER.
mathematics. In this
COMPUTE ADD-ANS = FIRST-NUMBER + SECOND-NUMBER.
example I am saying
DISPLAY "THE ANSWER IS " ADD-ANS.
COMPUTE ADDDISPLAY "PRESS ENTER TO END THE PROGRAM".
ANS = to the sum of
ACCEPT GET-ACCEPT-ANS.
the data in FIRSTSTOP RUN.
NUMBER and the
data in SECONDNUMBER.
ADDDA1.INT
When I ran ADDDA1.INT, it produce the results
shown above.
ADDDA1A.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES.
05 FIRST-NUMBER
PIC 999
VALUE 0.
05 SECOND-NUMBER
PIC 999
VALUE 0.
05 ADD-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
ACCEPT FIRST-NUMBER.
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
ACCEPT SECOND-NUMBER.
COMPUTE ADD-ANS = FIRST-NUMBER + SECOND-NUMBER * 2.
DISPLAY "THE ANSWER IS " ADD-ANS.
DISPLAY "PRESS ENTER TO END THE PROGRAM".
ACCEPT GET-ACCEPT-ANS.
STOP RUN.
Order of Operation
Order of Operation:
•Works from left to right
•Resolves parenthesis first
•Order of mathematics:
•Exponentiation
•Multiplication or Division
•Addition or Subtraction
ADDDA1A.INT
COMPUTE ADD-ANS = FIRST-NUMBER + SECOND-NUMBER * 2.
=
5
+
6
* 2
=
5
+
12
=
0017
ADDDA1B.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES.
05 FIRST-NUMBER
PIC 999
VALUE 0.
05 SECOND-NUMBER
PIC 999
VALUE 0.
05 ADD-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
ACCEPT FIRST-NUMBER.
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
ACCEPT SECOND-NUMBER.
COMPUTE ADD-ANS = (FIRST-NUMBER + SECOND-NUMBER) * 2.
DISPLAY "THE ANSWER IS " ADD-ANS.
DISPLAY "PRESS ENTER TO END THE PROGRAM".
ACCEPT GET-ACCEPT-ANS.
STOP RUN.
ADDDA1B.INT
COMPUTE ADD-ANS = (FIRST-NUMBER + SECOND-NUMBER) * 2.
=
(
=
=
5
+
11
0022
6
) * 2
* 2
ADDDA2.CBL
ADDDA2.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES.
05 FIRST-NUMBER
PIC 999
VALUE 0.
05 SECOND-NUMBER
PIC 999
VALUE 0.
05 ADD-ANS
PIC 9999
VALUE 0.
05 FINAL-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
ACCEPT FIRST-NUMBER.
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
ACCEPT SECOND-NUMBER.
ADD FIRST-NUMBER SECOND-NUMBER
GIVING ADD-ANS.
MULTIPLY ADD-ANS BY 2
GIVING FINAL-ANS.
DISPLAY "THE ANSWER IS " ADD-ANS " " FINAL-ANS.
DISPLAY "PRESS ENTER TO END THE PROGRAM".
ACCEPT GET-ACCEPT-ANS.
STOP RUN.
ADDDA2.INT
ADD FIRST-NUMBER SECOND-NUMBER
GIVING ADD-ANS.
12
+
17
=
29
=
58
MULTIPLY ADD-ANS BY 2
GIVING FINAL-ANS.
29
*
2
ADDDA2A.CBL
IDENTIFICATION DIVISION.
PROGRAM-ID. ADDPROG.
AUTHOR. GROCER.
*ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 INPUT-AREA.
05 GET-ACCEPT-ANS
PIC X
VALUE SPACES.
This more
05 FIRST-NUMBER
PIC 999
VALUE 0.
accurately shows
05 SECOND-NUMBER
PIC 999
VALUE 0.
how the data is
01 OUTPUT-AREA.
actually being
05 ADD-ANS
PIC 9999
VALUE 0.
used.
05 FINAL-ANS
PIC 9999
VALUE 0.
PROCEDURE DIVISION.
MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)".
ACCEPT FIRST-NUMBER.
DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)".
ACCEPT SECOND-NUMBER.
ADD FIRST-NUMBER SECOND-NUMBER
GIVING ADD-ANS.
MULTIPLY ADD-ANS BY 2
GIVING FINAL-ANS.
DISPLAY "THE ANSWER IS " ADD-ANS " " FINAL-ANS.
DISPLAY "PRESS ENTER TO END THE PROGRAM".
ACCEPT GET-ACCEPT-ANS.
STOP RUN.
ADDDA3.CBL
This is where I am storing the output, I
have edited it so that a comma will be
inserted if needed and so the Zs will
suppress leading zeros. Notice that edited
data does not have a value!
The results of the calculations are being stored in
ADD-ANS-WS and FINAL-ANS-WS. Note, the
data is being stored in a pure numeric field without
editing characters. When I am ready to display the
data, I move it to the output areas(ADD-ANS-OUT
and FINAL-ANS-OUT) that contain the editing
characters and then display the output areas.
ADDDA3.INT
The PIC Z,ZZ9 suppresses
leading zeros so you now
see the answers as 11 and
22.
Note that there is a 9 in the
units position, so if the
answer was 0, the single 0
would print since the units
position is not suppressed.
The PIC Z,ZZ9 inserts the comma in
the answers shown.
ADDDA4.CBL
The pictures have now been
defined so they can hold decimal
numbers, not just whole numbers.
The V is an assumed decimal point
that essentially says assume that the
decimal point is here when you do
your calculations.
The user wants to see an actual
decimal point, so in the output
pictures where I have done my
editing, I use the actual decimal
point.
ADDDA4.INT
ADDDA5.CBL
S is used with the pictures
where the answer is being
stored. S remembers the
sign.
The actual sign is used in the
output picture so the user can
tell if the data is negative.
Note that in one case the
negative sign is in front of the
data and in one case it is after
the data.
ADDDA5.INT
In the first example,
the subtraction did
not result in a
negative answer so
the negative sign in
the output pictures
turned to a space.
In the second
example, the answer
was negative. The S
remembered that it
had a negative answer
and the sign in the
output picture showed
the sign.
ADDDA6.CBL
Input now
supports
negative data.
ADDDA6.INT
Negative data was entered
into the second number.
When the subtraction was
done, the answer also was
a negative.