Ejemplos De Aritmetica en COBOL
En COBOL existen dos maneras de llevar a cabo operaciones aritméticas:
- Con el verbo
COMPUTE
- Con los verbos
ADD
,SUBSTRACT
,MULTIPLY
,DIVIDE
,EXPONENT
El verbo COMPUTE
El verbo COMPUTE
Este verbo o declaración, permite hacer cálculos aritméticos con los símbolos
tradicionales (+,-,*,/,**)
, es un verbo muy eficiente comparado con ADD
,
SUBSTRACT
, MULTIPLY
, DIVIDE
y EXPONENT
. Puede ser utilizado para
escribir mas de una operación aritmética en una sóla expresión.
Sintaxis
COMPUTE identifier-1 [ROUNDED] =
Identifier-2 <arithmetic operator> identifier-3
[ON SIZE ERROR <block of statements>]
[NOT ON SIZE ERROR <block of statements>]
[END-COMPUTE].
Las expresiones entre corchetes son opcionales.
ADD
, SUBSTRACT
, MULTIPLY
, DIVIDE
Estos verbos nos permiten llevar a cabo una sola operación por oración, son útiles para calcular reglas de negocio sencillas y claras o para desglosar completamente un algoritmo complicado.
Existen 3 tipos de sintaxis para utilizar estos verbos.
Tipo 1
ADD A B TO C D
ADD A B C TO D GIVING E
ADD CORR WS-GROUP1 TO WS-GROUP2
Ejemplos
Ejemplo usando el verbo COMPUTE
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE-ARITH-1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMBER1 PIC 9.
01 NUMBER2 PIC 9.
01 NUMBER3 PIC 9.
01 NUMBER4 PIC 9.
01 SUMA1 PIC 99.
01 RESTA1 PIC 99.
01 MULT1 PIC 99.
01 DIV1 PIC 99.
01 EXP1 PIC 999.
PROCEDURE DIVISION.
SET-VALUES-ROUTINE.
SET NUMBER1 TO 3.
SET NUMBER2 TO 5.
SET NUMBER3 TO 6.
SET NUMBER4 TO 9.
CALCULATE-ARITH-ROUTINE.
COMPUTE SUMA1 = NUMBER1 + NUMBER4.
COMPUTE RESTA1 = NUMBER4 - NUMBER2.
COMPUTE MULT1 = NUMBER2 * NUMBER3.
COMPUTE DIV1 = NUMBER4 / NUMBER1.
COMPUTE EXP1 = NUMBER1 ** NUMBER2.
DISPLAY-ROUTINE.
DISPLAY 'THE SUM OF ' NUMBER1 ' AND ' NUMBER4 ' IS ' SUMA1.
DISPLAY 'THE SUBST OF ' NUMBER4 ' AND ' NUMBER2 ' IS ' RESTA1.
DISPLAY 'THE PRODUCT OF ' NUMBER2 ' AND ' NUMBER3 ' IS ' MULT1.
DISPLAY 'THE DIVISION OF ' NUMBER4 ' AND ' NUMBER2 ' IS ' DIV1.
DISPLAY 'THE POWER OF ' NUMBER1 ' TO ' NUMBER2 ' IS ' EXP1.
FINISH-ROUTINE.
STOP RUN.
Ejemplo usando ADD
, SUBTRACT
, etc…
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE-ARITH-2.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 NUMBER1 PIC 9.
01 NUMBER2 PIC 9.
01 NUMBER3 PIC 9.
01 NUMBER4 PIC 9.
01 SUMA1 PIC 99.
01 RESTA1 PIC 99.
01 MULT1 PIC 99.
01 DIV1 PIC 99.
01 EXP1 PIC 999.
PROCEDURE DIVISION.
SET NUMBER1 TO 3.
SET NUMBER2 TO 5.
SET NUMBER3 TO 6.
SET NUMBER4 TO 9.
ADD NUMBER1 TO NUMBER4 GIVING SUMA1 END-ADD
SUBTRACT NUMBER4 FROM NUMBER2 GIVING RESTA1 END-SUBTRACT
MULTIPLY NUMBER2 BY NUMBER3 GIVING MULT1 END-MULTIPLY
DIVIDE NUMBER4 BY NUMBER1 GIVING DIV1 END-DIVIDE
DISPLAY 'THE SUM OF ' NUMBER1 ' AND ' NUMBER4 ' IS ' SUMA1.
DISPLAY 'THE SUBST OF ' NUMBER4 ' AND ' NUMBER2 ' IS ' RESTA1.
DISPLAY 'THE PRODUCT OF ' NUMBER2 ' AND ' NUMBER3 ' IS ' MULT1.
DISPLAY 'THE DIVISION OF ' NUMBER4 ' AND ' NUMBER1 ' IS ' DIV1.
STOP RUN.
No existe un verbo como POWER para elevar a una potencia en OpenCOBOL.