我的程序应该接受一个字符,并对照一个以大写和小写字母填充的表进行检查。然后,它应该显示字母在字母表中的位置。 D =4。我显然做错了什么,但我一生无法弄清楚是什么。
下面的表定义。
01 WS-ALPHABET-TABLE-DATA.
05 FILLER PIC X(02) VALUE 'Aa'.
05 FILLER PIC X(02) VALUE 'Bb'.
05 FILLER PIC X(02) VALUE 'Cc'.
05 FILLER PIC X(02) VALUE 'Dd'.
05 FILLER PIC X(02) VALUE 'Ee'.
05 FILLER PIC X(02) VALUE 'Ff'.
05 FILLER PIC X(02) VALUE 'Gg'.
05 FILLER PIC X(02) VALUE 'Hh'.
05 FILLER PIC X(02) VALUE 'Ii'.
05 FILLER PIC X(02) VALUE 'Jj'.
05 FILLER PIC X(02) VALUE 'Kk'.
05 FILLER PIC X(02) VALUE 'Ll'.
05 FILLER PIC X(02) VALUE 'Mm'.
05 FILLER PIC X(02) VALUE 'Nn'.
05 FILLER PIC X(02) VALUE 'Oo'.
05 FILLER PIC X(02) VALUE 'Pp'.
05 FILLER PIC X(02) VALUE 'Qq'.
05 FILLER PIC X(02) VALUE 'Rr'.
05 FILLER PIC X(02) VALUE 'Ss'.
05 FILLER PIC X(02) VALUE 'Tt'.
05 FILLER PIC X(02) VALUE 'Uu'.
05 FILLER PIC X(02) VALUE 'Vv'.
05 FILLER PIC X(02) VALUE 'Ww'.
05 FILLER PIC X(02) VALUE 'Xx'.
05 FILLER PIC X(02) VALUE 'Yy'.
05 FILLER PIC X(02) VALUE 'Zz'.
01 FILLER REDEFINES WS-ALPHABET-TABLE-DATA.
05 WS-ALPHABET-TABLE OCCURS 26 TIMES
INDEXED BY C.
10 ALPHABET-CHAR PIC X(02).
01 ALPHABET-NUM PIC 99 VALUE ZEROS.
01 CHAR PIC X.
88 VALIDCHAR VALUE "A" THRU "Z", "a" THRU "z".
其余代码
0000-DRIVER.
PERFORM 0100-INITIALIZE.
PERFORM 1000-CHAR-TEST.
PERFORM 9000-CLOSE.
GOBACK.
0100-INITIALIZE.
ACCEPT CURRENT-DATE FROM DATE.
MOVE CURRENT-YEAR TO WS-FORMATTED-DATE-YY.
MOVE CURRENT-MONTH TO WS-FORMATTED-DATE-MM.
MOVE CURRENT-DAY TO WS-FORMATTED-DATE-DD.
DISPLAY 'HW15 RACHEL GRAHAM ' WS-FORMATTED-DATE.
PERFORM VARYING C FROM 1 BY 1
UNTIL C > 26
DISPLAY ALPHABET-CHAR(C)
END-PERFORM.
PERFORM 1500-ACCEPT.
1000-CHAR-TEST.
IF CHAR = ' ' THEN
PERFORM 1300-NO-CHAR
ELSE
IF VALIDCHAR
PERFORM 1200-VALID-CHAR
ELSE
PERFORM 1100-INVALID-CHAR
END-IF
END-IF.
1100-INVALID-CHAR.
DISPLAY 'CHARACTER IN: ' CHAR.
DISPLAY 'NOT A LETTER IN THE ALPHABET'.
1200-VALID-CHAR.
SET C TO 1.
SEARCH WS-ALPHABET-TABLE
WHEN ALPHABET-CHAR (C) = CHAR
SET ALPHABET-NUM TO C
DISPLAY CHAR 'IS NUMBER ' ALPHABET-NUM 'IN THE ALPHABET'
END-SEARCH.
1300-NO-CHAR.
DISPLAY 'CHARACTER IN: " "' CHAR.
DISPLAY 'NO CHARACTER ENTERED'.
1500-ACCEPT.
ACCEPT CHAR.
9000-CLOSE.
GOBACK.
STOP RUN.
任何帮助将不胜感激。
答案 0 :(得分:4)
什么是 CHAR ???如果是单个字符,请说
03 CHAR pic X.
您正在将2个字符的字段与1个字符的字段进行比较 尝试将表更改为
01 FILLER REDEFINES WS-ALPHABET-TABLE-DATA.
05 WS-ALPHABET-TABLE OCCURS 52 TIMES
INDEXED BY C.
10 ALPHABET-CHAR PIC X(01).
01 ALPHABET-NUM PIC 99 VALUE ZEROS.
和搜索
SET C TO 1.
SEARCH WS-ALPHABET-TABLE
WHEN ALPHABET-CHAR (C) = CHAR
SET ALPHABET-NUM TO C
compute ALPHABET-NUM = (ALPHABET-NUM + 1) / 2
end-compute
DISPLAY CHAR 'IS NUMBER ' ALPHABET-NUM 'IN THE ALPHABET'
END-SEARCH.
或者,您可以将表格更改为
01 FILLER REDEFINES WS-ALPHABET-TABLE-DATA.
05 WS-ALPHABET-TABLE OCCURS 26 TIMES
INDEXED BY C.
10 UPPER-CHAR PIC X(01).
10 LOWER-CHAR PIC X(01).
和搜索
SEARCH WS-ALPHABET-TABLE
WHEN UPPER-CHAR (C) = CHAR or LOWER-CHAR (c) = CHAR
SET ALPHABET-NUM TO C
DISPLAY CHAR 'IS NUMBER ' ALPHABET-NUM 'IN THE ALPHABET'
END-SEARCH.