Unfortunately I forgot to mention in the previous article a few important keywords:
for divide.
DIVIDE WS-B BY WS-A GIVING WS-RESULT REMAINDER <variable>.
')
Using REMAINDER we specify the variable in which the REMAINST division is written.
For any mathematical operations:
Also, adding ROUNDED after the operation, we achieve that the result will be rounded, and not just “discarded” the extra tail.
ADD WS-A WS-B GIVING WS-RESULT ROUNDED.
And now we will continue to learn new things. Complying with the promise to dispel the surprise of the strange derivation of the values ​​of variables, we will analyze this time PICTURE IS (aka PIC) and the formats of the variables.
And again we return to DATA DIVISION.
Consider one more line with the description of the variable:
01 - variable level;
WS-B is the variable name;
PIC is a keyword indicating that the variable format is described next; the old form is PICTURE IS.
9 (3) - the type of characters in the variable “9” and their number is “3”.
LEVEL VARIABLE.Levels from 01 to 49 inclusive and 77 are available to the user for ordinary variables. The variable level describes its hierarchy in the variable group; the higher the number, the lower the variable in the “ladder”. for example
01 CUSTOMER-RECORD.
05 CUSTOMER-NAME PIC X (10).
05 CUSTOMER-ADDRESS PIC X (30).
Level 77 declares a variable as ELEMENTARY, that is, it cannot have sub-variables.
Level 88 declares a variable as conditional, and in a sufficiently sophisticated and functionally rich form, more about them next time.
There are some other levels that are introduced either by compiler manufacturers, or are not needed now.
NAME OF VARIABLE.A variable name can consist of numbers, letters, and “-”, but it should not begin with a “-”.
The variable name must contain no more than 30 characters.
DESCRIPTION OF FORMAT VARIABLE.Consider what descriptive characters exist for variables in COBOL.
They are divided into two groups: “ordinary” and “editing”.
In the usual include
9 - one digit;
V is the character separating the integer part from the decimal (can be used only ONE time in each variable).
S - “±” sign, must be the very first character in the variable format (can be used only ONE time in each variable).
X is one alphanumeric character, i.e. number, letter, space, spec. characters.
A - one letter or space.
Editing symbols - thanks to them, variables are described that will not participate in the calculations, but allow you to fine-tune the view in which the variable will be displayed / printed:
Z - replaces 9'ki with the difference that all leading zeros are removed.
$ - “currency sign” specified for the compiler, by default, basically the dollar sign, is used ONLY as the FIRST character in the description. Can only be used once. Its value is set in ENVIRONMENT DIVISION. via
* Is an analogue of Z, but the leading zeros are replaced by *.
- (minus) - can be specified both the first and the last character, but only once. If the number is negative, then a minus will be displayed, otherwise an empty space.
+ (plus) - analogue “-” (minus), BUT in the case of a positive number, “+” will be displayed, and in the case of a negative number, “-” is displayed.
. (dot) is a “decimal point” sign. In its place will be displayed exactly the “point”.
, (comma) - the “comma” sign. May be somewhat variable.
/ (slash) - the sign “slash”. Maybe a few. Displays “slash”, is popular in dates.
0 (zero) - “zero”. Just this place will be displayed 0.
B (from blank, empty) - “space”.
But most importantly, as you can understand, all these symbols are combined and allow you to get very flexible shapes and types. The rule here is actually only one thing - numeric variables should not exceed 18 characters, alphanumeric - 160 characters. But here again, much depends on the compiler and its parameters.
DELICIOUS ADDITIONS OR THERE IS LIFE AFTER PIC'a!
In addition, when describing a variable after its format, additional “options” may be added:
BLANK WHEN ZERO - if the variable is 0, then empty space will be displayed instead of it, not zeros.
VALUE “value” - immediately assigns a specific value to a variable.
There are others, but to mention them now would be useless.
So now let's just feel all of them in different forms.
000100 IDENTIFICATION DIVISION.
000200 PROGRAM-ID. VARIABLES-TEST.
000300 AUTHOR. ME
000400 ENVIRONMENT DIVISION.
000500 DATA DIVISION.
000600 WORKING-STORAGE SECTION.
000700 01 HUMAN-CONTAINER.
000800 05 HUMAN-NAME PIC A (30).
000900 05 HUMAN-ADDRESS PIC X (160).
001000 77 SQUARE-METERS PIC 9 (18).
001100 77 SQUARE-PRICE PIC 9 (15) V9 (2).
001150 77 FLAT-PRICE PIC 9 (15) V9 (2).
001200 77 TAX-PERCENT PIC 9 (2).
001300 77 TAX-SUMM PIC 9 (15) V9 (2).
001350 77 NEGATIVE-VALUE PIC S9 (10) V9 (6).
001400 * --- OOOOMG! ---
001500 77 SQUARE-METERS-OUT PIC Z (17) 9.
001600 77 SQUARE-PRICE-OUT PIC Z (14) 9.Z (2).
001650 77 FLAT-PRICE-OUT-1 PIC Z (3), Z (3), Z (3), Z (3) .9 (2).
001660 77 FLAT-PRICE-OUT-2 PIC Z (3) BZ (3) BZ (3) BZ (3) .9 (2).
001700 77 TAX-PERCENT-OUT PIC Z (2).
001800 77 TAX-SUMM-OUT PIC Z (14) 9.9 (2).
001900 77 NEGATIVE-VALUE-OUT-1 PIC + Z (10) .9 (6).
002000 77 NEGATIVE-VALUE-OUT-2 PIC -Z (10). * (6).
002100 * --------------------------------
002200 PROCEDURE DIVISION.
002300 BEGIN.
002400 DISPLAY "Please enter Name:".
002500 ACCEPT HUMAN-NAME.
002550 DISPLAY "Please enter Address:".
002600 ACCEPT HUMAN-ADDRESS.
002700 DISPLAY "Please enter square meters of flat:".
002800 ACCEPT SQUARE-METERS.
002900 DISPLAY "Please enter square meter's price:".
003000 ACCEPT SQUARE-PRICE.
003100 DISPLAY "Please enter percent of tax:".
003200 ACCEPT TAX-PERCENT.
003300 DISPLAY "Enter any really big NEGATIVE value:".
003400 ACCEPT NEGATIVE-VALUE.
003500 DISPLAY "---------------------------------------".
003600 DISPLAY "".
003700 DISPLAY HUMAN-NAME.
003800 DISPLAY HUMAN-ADDRESS.
003900 DISPLAY "SQUARE-METERS:", SQUARE-METERS.
004000 MOVE SQUARE-METERS TO SQUARE-METERS-OUT.
004100 DISPLAY "SQUARE-METERS-OUT:", SQUARE-METERS-OUT.
004200 DISPLAY "SQUARE-PRICE:", SQUARE-PRICE.
004300 MOVE SQUARE-PRICE TO SQUARE-PRICE-OUT.
004400 DISPLAY "SQUARE-PRICE-OUT:", SQUARE-PRICE-OUT.
004500 MULTIPLY SQUARE-METERS BY SQUARE-PRICE GIVING FLAT-PRICE.
004600 DISPLAY "FLAT-PRICE:", FLAT-PRICE.
004700 MOVE FLAT-PRICE TO FLAT-PRICE-OUT-1 FLAT-PRICE-OUT-2.
004800 DISPLAY "FLAT-PRICE-OUT-1:", FLAT-PRICE-OUT-1.
004850 DISPLAY "FLAT-PRICE-OUT-2:", FLAT-PRICE-OUT-2.
004900 DISPLAY "TAX-PERCENT:", TAX-PERCENT.
005000 MOVE TAX-PERCENT TO TAX-PERCENT-OUT.
005100 DISPLAY "TAX-PERCENT-OUT:", TAX-PERCENT-OUT.
005200 DISPLAY "TAX-SUMM:", TAX-SUMM.
005300 MOVE TAX-SUMM TO TAX-SUMM-OUT.
005400 DISPLAY "TAX-SUMM-OUT:", TAX-SUMM-OUT.
005500 DISPLAY "NEGATIVE-VALUE:", NEGATIVE-VALUE.
005600 MOVE NEGATIVE-VALUE TO NEGATIVE-VALUE-OUT-1 NEGATIVE-VALUE-OUT-2.
005700 DISPLAY "NEGATIVE-VALUE-OUT-1:", NEGATIVE-VALUE-OUT-1.
005800 DISPLAY "NEGATIVE-VALUE-OUT-2:", NEGATIVE-VALUE-OUT-2.
005900 STOP RUN.
And what we get in the end.
Please enter Name:
Slava
Please enter Address:
Moscow 15
Please enter square meters of flat:
44
Please enter square meter's price:
1234123
Please enter percent of tax:
20
Enter any really big NEGATIVE value:
-123123213213
---------------------------------------
Slava
Moscow 15
SQUARE-METERS: 000000000000000044
SQUARE-METERS-OUT: 44
SQUARE-PRICE: 000000001234123.00
SQUARE-PRICE-OUT: 1234123.00
FLAT-PRICE: 000000054301412.00
FLAT-PRICE-OUT-1: 54,301,412.00
FLAT-PRICE-OUT-2: 54 301 412.00
TAX-PERCENT: 20
TAX-PERCENT-OUT: 20
TAX-SUMM: 000000000000000.00
TAX-SUMM-OUT: 0.00
NEGATIVE-VALUE: -3123213213.000000
NEGATIVE-VALUE-OUT-1: -3123213213.000000
NEGATIVE-VALUE-OUT-2: -3123213213.000000
As you can see, the output of “formatted” and raw variables is different and customizable.
Naturally, this is not just your right, but the obligation to play around with variables, to see how they will behave.
And by the way, if you carefully watched the output of your program, you might have noticed a very unpleasant “trifle” - values ​​that did not fit into the variable, if you didn’t, try experimenting.
Until next meeting! There is still a lot of interesting ahead)
Vorontsov “nerfur” Vyacheslav. 2011