COBOL - Exemplos simples - Classificacao de uma tabela interna



Desenvolvido por DORNELLES Carlos Alberto - Analista de Sistemas - Brasília DF. - cad_cobol@hotmail.com

COBOL - Exemplos simples - Classificacao de uma tabela interna
IDENTIFICATION DIVISION. 
PROGRAM-ID.    SORTTABE.
AUTHOR.        CARLOS ALBERTO DORNELLES.
DATA DIVISION. 
WORKING-STORAGE SECTION. 

01  WS-TABELEMT                           VALUE ZEROES. 
    05 WS-ELEMENTO    PIC 9(002) OCCURS 1 TO 99 
                                          DEPENDING WS-NROELEM.
01  WS-AUXILIARES. 
    05  WS-NROELEM    PIC 9(002)          VALUE 15. 
    05  I             PIC 9(002)          VALUE 1. 
    05  J             PIC 9(002). 
    05  K             PIC 9(002). 
    05  T             PIC X(002). 

PROCEDURE DIVISION. 

    PERFORM 001-INICIAL
    PERFORM 002-PROCESSAMENTO
    PERFORM 003-FINAL
    STOP RUN.

001-INICIAL. 

    MOVE 102204521253650667569970013345 TO WS-TABELEMT.

002-PROCESSAMENTO.

    PERFORM VARYING I FROM 1 BY 1 UNTIL I > WS-NROELEM 
            COMPUTE K = I + 1 
            PERFORM VARYING J FROM K BY 1 UNTIL J > WS-NROELEM 
                    IF  WS-ELEMENTO (I) > WS-ELEMENTO (J) 
                        MOVE WS-ELEMENTO (I) TO T  
                        MOVE WS-ELEMENTO (J) TO WS-ELEMENTO (I) 
                        MOVE T TO WS-ELEMENTO (J)
                    END-IF 
            END-PERFORM 
    END-PERFORM. 

003-FINAL.

    MOVE 1 TO I. 
    DISPLAY 'A tabela classificada: '. 
    PERFORM WS-NROELEM TIMES 
            DISPLAY WS-ELEMENTO(I) 
            ADD 1 TO I  
    END-PERFORM.   

    DISPLAY 'A tabela classificada - 1: '. 
    DISPLAY WS-TABELEMT.   

A tabela classificada:

01
04
06
10
12
22
33
45
52
53
56
65
67
70
99

A tabela classificada - 1:

010406101222334552535665677099