COBOL 学习笔记 之 入門篇

       時間過的真快,轉眼三個月就過去了,在學習cobol過程中竟然沒有做筆記,但是這並不奇怪,因爲學習一種語言最好的方式是實踐,儅你了解基本的語法和編程思想之後,去看程序去修改程序是學習進步最快的方法。
        在公司做事的最大好處是入門快,有很多前輩讓你請教,有很多程序讓你參考讓你學習,還有就是你不用愁應該寫什麽程序,公司已經設計好了,安排給你寫就行了,這對於初學者來説是最好不過了,不用涉及系統構架和設計方面的事情.很多人剛開始學習編程最大的痛苦就是不知道寫什麽程序好,往往就此退卻.
        好,不講廢話了,剛學cobol應該怎麽入門呢?當然是要看教程(網上或書本)啦,這裡不准備寫詳細的教程,需要了解相關概念可以參考COBOL (10)這裡的教程,雖然不完整,但是概念還是有的。其實cobol是比較八股的,基本上寫好一個模板程序之後其他的就copy行了,剩下就主要關注邏輯實現 。
先看一個程序吧:
      *****************************************************************         
      
*   PROGRAM SPECIFICATION                                       *         
      
*---------------------------------------------------------------*         
      
*   SYSTEM NAME   : 系統名字(該程序屬於哪個系統)                *         
      
*   PROGRAM TYPE  : 程序類型                                    *         
      
*   PROGRAM NAME  : 程序名稱                                    *         
      
*   AUTHOR  .  : 程序作者                                    *         
      
*   CREATED DATE  : 2005/03/31                                  *         
      
*   LAST UPD. DATE: 2005/03/31                                  *         
      
*   VERSION NO..  : 1.0.0                                       *         
      
*---------------------------------------------------------------*         
      
*   FUNCTIONS. : 程序的功能描述                              *        
      
*---------------------------------------------------------------*         
      
*   MODIFICATION HISTORY                                        *         
      
*---------------------------------------------------------------*         
      
*   SEQ DESCRIPTION                    BY             DATE      *         
      
*   xxxxxxxxxxxxxxx                    xx             xxxx      *
      
*---------------------------------------------------------------*         
      
*   INPUT.. : 程序輸入數據描述,如:                      *         
      
*                   1. CITINV(EXTENAL A/C INFO TABLE)           *         
      
*                   2. CITSVD(SAVE BOX TABLE)                   *         
      
*                   3. CITCUS(CUSTOMER TABLE)                   *         
      
*---------------------------------------------------------------*         
      
*   OUTPUT. : 程序輸出數據描述,如:                      * 
      
*                   1.CVQCI007(SPECIAL LIST 1 REPORT DATA FILE  *        
      
*                   2.CVQCI008(EXTENAL A/C INFO REPORT DATA FILE*         
      
*---------------------------------------------------------------*         
      
*   UPDATE. : 1. NULL                                     *         
      
*---------------------------------------------------------------*         
      
*   SUBPROGRAM : 1. PRM1(該程序調用到的子程序)             *         
      
*---------------------------------------------------------------*         
      
*****************************************************************         
      
*************************                                                 
       IDENTIFICATION DIVISION.                                                 
      
*************************                                                 
       PROGRAM
-ID.    TSTHELLO.                                                 
       AUTHOR.        XXXXXXXX.                                                   
       DATE
-WRITTEN.  2005/03/31.                                               
      
*************************                                                 
       ENVIRONMENT    DIVISION.                                                 
      
*************************                                                 
       CONFIGURATION  SECTION.                                                  
      
*************************                                                 
       INPUT
-OUTPUT   SECTION.                                                  
      
*************************                                                 
       FILE
-CONTROL.                                                            
           SELECT FILENAME        ASSIGN       TO FILEPATH                      
                                  FILE STATUS  IS WS
-FILENAME-STATUS.                 
      
*************************                                                 
       DATA DIVISION.                                                           
      
*************************                                                 
       FILE SECTION.                                                            
      
*************************                                                 
       FD   R
-FILENAME.                                                       
            
01 RECORD00.
               
03 RECORD10 PIC X(15).
               
03 RECORD11 PIC X(50).
      
*************************                                                 
       WORKING
-STORAGE SECTION.                                                 
      
*************************                                                 
      
*  CONSTANT DECLARATION *                                                 
      
*************************                                                 
       
77  K-PGM-ID        PIC X(08) VALUE 'TSTHELLO'.                   
      
*************************                                                 
      
*  PROGRAM VARIABLES    *                                                 
      
*************************                                                 
       
01  WS-FILE-STATUS.                                                      
           
03 WS-OUTPUT-REC-CNT1         PIC 9(09) VALUE ZERO.                  
           
03 WS-FILENAME-STATUS         PIC 9(02).                             
              
88 WS-FILENAME-SUCCESSFUL  VALUE 00.                              
              
88 WS-FILENAME-EOF         VALUE 10.                              
              
88 WS-FILENAME-ACCEPTABLE  VALUE 00 10.                              
      
*************************                                                 
       PROCEDURE DIVISION.                                                      
      
*************************                                                 
       A00
-MAIN-PROC.                                                           
           DISPLAY K
-PGM-ID ' PROCESS START.'                                    
           PERFORM A000
-INITIALIZE                                                
           PERFORM B000
-PRG-PROCESS                               
           PERFORM Z000
-CLOSE-FILE                                              
           DISPLAY K
-PGM-ID ' END SUCCESSFULLY.'                                 
           STOP RUN.                                                              
      
*                                                                         
       A000
-INITIALIZE.                                                           
           PERFORM    A100
-OPEN-FILES                                             
           INITIALIZE R
-FILENAME                                                  
           EXIT.                                                                
      
*                                                                         
       A100
-OPEN-FILES.                                                           
           OPEN OUTPUT FILENAME                                                 
           EXIT.                                                                
      
***************************************************************           
      
*    MAIN PROCESS ROUTINE                                     *           
      
***************************************************************           
       B000
-PRG-PROCESS. 
           MOVE 
'RECORD VALUE1'           TO RECORD10
           MOVE 
'RECORD VALUE2'           TO RECORD11
           PERFORM S000
-WRITE-FILENAME
           EXIT.                                                                
      
*                                                                         
       S000
-WRITE-FILENAME.                                                      
           WRITE       R
-FILENAME                                                 
           ADD         
1                  TO WS-OUTPUT-REC-CNT1                 
           INITIALIZE  R
-FILENAME                                                 
           EXIT.                                                                
      
*****************************************************************         
      
*  CLOSE FILE : FILENAME                                         *        
      
*****************************************************************         
       Z000
-CLOSE-FILE.                                                         
           CLOSE       FILENAME                                                 
           DISPLAY     
'FILENAME NO OF FILENAME OUTPUT REC: '                   
                       WS
-OUTPUT-REC-CNT1                                       
           EXIT.                                                                
      
*****************************************************************         
      
*END PROGRAM                                                              
      
*****************************************************************         


這個程序看起來很長,但是其實開頭一大段是註釋,這是個好習慣,每個程序都要有説明。

(未完,待續...)

posted @ 2005-03-31 18:51  epan  阅读(4533)  评论(4编辑  收藏  举报