从'计算两个日期天数之差'程序介绍RPG语言

RPG,这个不是 Role Playing Game 的缩写,而是一种编程语言,Report Program Generator的缩写,是一种起源于用在DEC及IBM的小型机操作系统中编制报表程序的编程语言,一种运行在IBM的AS400小型机上的高级语言,一种完全过程化的程序语言。下面的这段代码是用来计算两个日期之差的天数:
     H*
     H* Author : KXT
     H*
     D*
     D  TOTAL_DAYS     S              8S 0
     D  BEGIN_REMAIN   S              3S 0
     D  END_PASS       S              3S 0
     D  MONTH_DAYS     S              2S 0 DIM(12) CTDATA PERRCD(12)
     D*
     D  BEGIN_DATE     S              8S 0
     D  BEGIN_YEAR     S              4S 0
     D  BEGIN_MONTH    S              2S 0
     D  BEGIN_DAY      S              2S 0
     D  END_DATE       S              8S 0
     D  END_YEAR       S              4S 0
     D  END_MONTH      S              2S 0
     D  END_DAY        S              2S 0
     D  TEMP_DATE      S              8S 0
     D  TEMP_YEAR      S              4S 0
     D*
     D  REM_DATA1      S              4S 0
     D  REM_DATA2      S              4S 0
     D  TEMP_DATA      S              4S 0
     D  I              S              4S 0
     D*
     D  IS_LEAP        S                   LIKE(*IN99)
     D  NEED_TRANSFER  S                   LIKE(*IN99)
     D*
     C*
     C* Main Logic
     C                   EXSR      SR999
     C                   EXSR      SR001
     C                   EXSR      SR998
     C*
     C* Processor
     C     SR001         BEGSR
     C     BEGIN_DATE    IFGT      END_DATE
     C                   MOVE      *ON           NEED_TRANSFER
     C                   MOVE      BEGIN_DATE    TEMP_DATE
     C                   MOVE      END_DATE      BEGIN_DATE
     C                   MOVE      TEMP_DATE     END_DATE
     C                   ENDIF
     C* Get begin year, month, day
     C                   EVAL      BEGIN_YEAR = %DIV(BEGIN_DATE:10000)
     C                   EVAL      TEMP_DATA = %REM(BEGIN_DATE:10000)
     C                   EVAL      BEGIN_MONTH = %DIV(TEMP_DATA:100)
     C                   EVAL      BEGIN_DAY = %REM(TEMP_DATA:100)
     C* Get end year, month, day
     C                   EVAL      END_YEAR = %DIV(END_DATE:10000)
     C                   EVAL      TEMP_DATA = %REM(END_DATE:10000)
     C                   EVAL      END_MONTH = %DIV(TEMP_DATA:100)
     C                   EVAL      END_DAY = %REM(TEMP_DATA:100)
     C* If begin and end is same year
     C     BEGIN_YEAR    IFEQ      END_YEAR
     C                   EVAL      I = BEGIN_MONTH
     C                   MOVE      *ZERO         TEMP_DATA
     C                   EVAL      TEMP_DATA = END_MONTH
     C                   DOW       I < TEMP_DATA
     C                   ADD       MONTH_DAYS(I) TOTAL_DAYS
     C                   MOVE      BEGIN_YEAR    TEMP_YEAR
     C                   EXSR      SR002
     C     IS_LEAP       IFEQ      *ON
     C     I             ANDEQ     2
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS + 1
     C                   ENDIF
     C                   EVAL      I = I + 1
     C                   ENDDO
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS + END_DAY - BEGIN_DAY
     C* If both year not equal
     C                   ELSE
     C* Calculate begin date remaining days
     C                   EVAL      I = BEGIN_MONTH
     C                   DOW       I <= 12
     C                   ADD       MONTH_DAYS(I) BEGIN_REMAIN
     C                   MOVE      BEGIN_YEAR    TEMP_YEAR
     C                   EXSR      SR002
     C     IS_LEAP       IFEQ      *ON
     C     I             ANDEQ     2
     C                   EVAL      BEGIN_REMAIN = BEGIN_REMAIN + 1
     C                   ENDIF
     C                   EVAL      I = I + 1
     C                   ENDDO
     C                   EVAL      BEGIN_REMAIN = BEGIN_REMAIN - BEGIN_DAY
     C* Calculate end date passing days
     C                   EVAL      I = 1
     C                   DOW       I < END_MONTH
     C                   ADD       MONTH_DAYS(I) END_PASS
     C                   MOVE      END_YEAR      TEMP_YEAR
     C                   EXSR      SR002
     C     IS_LEAP       IFEQ      *ON
     C     I             ANDEQ     2
     C                   EVAL      END_PASS = END_PASS + 1
     C                   ENDIF
     C                   EVAL      I = I + 1
     C                   ENDDO
     C                   EVAL      END_PASS = END_PASS + END_DAY
     C* Calculate other year total days
     C                   EVAL      I = BEGIN_YEAR + 1
     C                   DOW       I < END_YEAR
     C                   ADD       365           TOTAL_DAYS
     C                   MOVE      I             TEMP_YEAR
     C                   EXSR      SR002
     C     IS_LEAP       IFEQ      *ON
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS + 1
     C                   ENDIF
     C                   EVAL      I = I + 1
     C                   ENDDO
     C* Calculate total diff days
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS + BEGIN_REMAIN
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS + END_PASS
     C                   ENDIF
     C     NEED_TRANSFER IFEQ      *ON
     C                   EVAL      TOTAL_DAYS = TOTAL_DAYS * -1
     C                   ENDIF
     C     TOTAL_DAYS    DSPLY
     C                   ENDSR
     C*
     C* Check if leap year
     C     SR002         BEGSR
     C                   EVAL      REM_DATA1 = %REM(TEMP_YEAR:400)
     C     REM_DATA1     IFEQ      *ZERO
     C                   MOVE      *ON           IS_LEAP
     C                   ELSE
     C                   EVAL      REM_DATA1 = %REM(TEMP_YEAR:4)
     C                   EVAL      REM_DATA2 = %REM(TEMP_YEAR:100)
     C     REM_DATA1     IFEQ      *ZERO
     C     REM_DATA2     ANDNE     *ZERO
     C                   MOVE      *ON           IS_LEAP
     C                   ELSE
     C                   MOVE      *OFF          IS_LEAP
     C                   ENDIF
     C                   ENDIF
     C                   ENDSR
     C*
     C* Finalization
     C     SR998         BEGSR
     C                   MOVE      *ON           *INLR
     C                   RETURN
     C                   ENDSR
     C*
     C* Initialization
     C     SR999         BEGSR
     C     *ENTRY        PLIST
     C                   PARM                    BEGIN_DATE
     C                   PARM                    END_DATE
     C*
     C                   MOVE      *OFF          IS_LEAP
     C                   MOVE      *OFF          NEED_TRANSFER
     C                   MOVE      *ZERO         TOTAL_DAYS
     C                   MOVE      *ZERO         BEGIN_REMAIN
     C                   MOVE      *ZERO         END_PASS
     C*
     C                   ENDSR
     C*
**CTDATA MONTH_DAYS
312831303130313130313031 

每一行最前面的字母都有特别的意思。。

H(Header的缩写,这个是我自己认为的,以下皆同) - 可以在这里写些代码的描述;

F(File) - 数据库文件的定义

D(Definition) - 变量的定义

C(Calculation) - 主程序

I(Input) - 输入

O(Output) - 输出

每一行加*的都代表那一行是注释。

我们是在IBM的AS400机器上写RPG的,界面是黑底绿字,字体颜色可以修改的,RPG程序代码的位置都是写死的,跟汇编语言是一样的,语法其实也是很像汇编的。

据我了解,貌似国内就只有银行,保险和证券是用RPG语言的,其他好像很少有公司用到这门语言,其实RPG历史还是蛮悠久的,用着用着还是蛮好玩的。

维基百科 : http://en.wikipedia.org/wiki/IBM_RPG

你可能感兴趣的:(从'计算两个日期天数之差'程序介绍RPG语言)