COBOLの変換:行末尾のピリオドを<PERIOD>に変換する
✏スクリプト
import re
# COBOLソースコード
cobol_code = """
IDENTIFICATION DIVISION.
PROGRAM-ID. FloatConditionalSample.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Age PIC 9(3).
01 Income PIC 9(7)V99 USAGE IS COMPUTATIONAL-3.
01 isStudent PIC X(1).
01 isSenior PIC X(1).
01 discountApplied PIC X(1) VALUE 'N'.
01 discountThreshold PIC 9(7)V99 USAGE IS COMPUTATIONAL-3 VALUE 50000.00.
PROCEDURE DIVISION.
DISPLAY "Enter age: ".
ACCEPT Age.
DISPLAY "Enter income: ".
ACCEPT Income.
DISPLAY "Are you a student? (Y/N): ".
ACCEPT isStudent.
DISPLAY "Are you a senior citizen? (Y/N): ".
ACCEPT isSenior.
IF Age >= 18 AND isStudent = 'Y' AND
(Income > discountThreshold OR
(Age >= 60 AND isSenior = 'Y'))
DISPLAY "Discount applied."
MOVE 'Y' TO discountApplied
END-IF.
IF discountApplied = 'N'
DISPLAY "No discounts applied."
END-IF.
STOP RUN.
"""
# 複数行の変数を行ごとに
lines = cobol_code.split("\n")
# 1行ごとに処理
for line in lines:
line = re.sub(r"\.$", "<PERIOD>", line)
print(line)
✏実行結果
IDENTIFICATION DIVISION<PERIOD>
PROGRAM-ID. FloatConditionalSample<PERIOD>
DATA DIVISION<PERIOD>
WORKING-STORAGE SECTION<PERIOD>
01 Age PIC 9(3)<PERIOD>
01 Income PIC 9(7)V99 USAGE IS COMPUTATIONAL-3<PERIOD>
01 isStudent PIC X(1)<PERIOD>
01 isSenior PIC X(1)<PERIOD>
01 discountApplied PIC X(1) VALUE 'N'<PERIOD>
01 discountThreshold PIC 9(7)V99 USAGE IS COMPUTATIONAL-3 VALUE 50000.00<PERIOD>
PROCEDURE DIVISION<PERIOD>
DISPLAY "Enter age: "<PERIOD>
ACCEPT Age<PERIOD>
DISPLAY "Enter income: "<PERIOD>
ACCEPT Income<PERIOD>
DISPLAY "Are you a student? (Y/N): "<PERIOD>
ACCEPT isStudent<PERIOD>
DISPLAY "Are you a senior citizen? (Y/N): "<PERIOD>
ACCEPT isSenior<PERIOD>
IF Age >= 18 AND isStudent = 'Y' AND
(Income > discountThreshold OR
(Age >= 60 AND isSenior = 'Y'))
DISPLAY "Discount applied."
MOVE 'Y' TO discountApplied
END-IF<PERIOD>
IF discountApplied = 'N'
DISPLAY "No discounts applied."
END-IF<PERIOD>
STOP RUN<PERIOD>
IT領域を中心に、日々学んだことを備忘を兼ねてメモしています。
コメント
コメントを投稿