数据验证

发布于 2024-10-12 19:10:30 字数 329 浏览 2 评论 0原文

我已经进入大学第二学期了,他们让我学习一门名为“高级 COBOL”的课程。作为我的作业之一,我必须编写一个程序来测试文件中的某些内容,以确保输入没有错误。我明白了大致的想法,但只有一些事情我不明白,我的老师是那些会给你布置作业并让你在很少或没有帮助的情况下自己弄清楚的人之一。这就是我需要帮助的地方。

  1. 我有一个字段,前 5 列必须是数字,第 6 列必须是大写字母,最后 2 列数字必须在 01-68 或 78-99 范围内。

  2. 我的一个字段必须是一串带有破折号的数字,例如 00000-000,但有些字段有多个破折号。我如何计算破折号来确定是否存在问题。

So I have entered my second semester of College and they have me doing a course called Advanced COBOL. As one of my assignments I have to my make a program that tests certain things in a file to make sure the input has no errors. I get the general idea but there are just a few things I don't understand and my teacher is one of those people who will give you an assignment and make you figure it out yourself with little or no help. So here is what I need help with.

  1. I have a field that the first 5 columns have to be numbers, the 6th column a capital letter and the last 2 numbers in a range of 01-68 or 78-99.

  2. one of my fields has to be a string of numbers with a dash in it like 00000-000, but some have more than one dash. How can I count the dashes to identify that there is a problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

够运 2024-10-19 19:10:30

这里有一些提示...

使用分层记录结构以不同的方式查看数据。例如:

 01 ITEM-REC.
    05 ITEM-CODE.
       10 ITEM-NUM-CODE    PIC 9(3).
       10 ITEM-CHAR-CODE   PIC A(3).
          88 ITEM-TYPE-A   VALUE 'AAA' THRU 'AZZ'.
          88 ITEM-TYPE-B   VALUE 'BAA' THRU 'BZZ'.
    05 QUANTITY            PIC 9(4).

ITEM-CODE是一个6字符组字段,其中第一部分是数字(ITEM-NUM-CODE),最后一部分是数字(ITEM-NUM-CODE)
是字母(项目char-code)。您可以在程序中引用这三个变量中的任何一个。当你
请参阅item-code或任何其他组项目,COBOL
将变量视为被称为pic x的变量。这意味着您可以
移动几乎所有内容都没有引起错误。例如:

MOVE 'ABCdef' TO ITEM-CODE

即使基本数据项item-num-code绝对不是数字,也

MOVE 'ABCdef0005' TO ITEM-REC

不会引起错误。
验证有效性
组移动后的数据,您应该分别验证每个基本数据项(除非
您肯定知道不会发生数据类型错误)。有多种方法可以做到这一点。为了
例如,如果数据项必须是数字,则以下是可行的:

IF ITEM-NUM-CODE IS NUMERIC
   CONTINUE
ELSE
   DISPLAY 'ITEM-NUM-CODE IS NOT NUMERIC'
   PERFORM BIG-BAD-ERROR
END-IF

COBOL提供了可以针对数据项应用的各种类测试。为了
示例:通常使用数字,字母和字母数字。

测试价值范围的另一种常见方法是定义各种88个级别 - 但锻炼
警告。在上面
示例item-type-a是一个88级别,基于“ AAA”到基于“ AAA”到“ AZZ”的数据范围
当前的整理序列有效。验证item-char-code仅包含字母
字符并且第一个字母是“A”或“B”,您可以执行以下操作:

IF ITEM-CHAR-CODE ALPHABETIC
   DISPLAY 'ITEM-CHAR-CODE is alphabetic.'
   EVALUATE TRUE
      WHEN ITEM-TYPE-A
         DISPLAY 'ITEM-CHAR-CODE is in range AAA through AZZ'
      WHEN ITEM-TYPE-B
         DISPLAY 'ITEM-CHAR-CODE is in range BAA through BZZ'
      WHEN OTHER
         DISPLAY 'ITEM-CHAR-CODE is in some other range'
   END-EVALUATE
ELSE
   DISPLAY 'ITEM-CHAR-CODE is not alphabetic'
END-IF        

请注意上面对 ALPHABETIC 的单独测试。当88级测试时为什么要这样做
可以做这份工作吗?实际上 88 还不够,因为它们
基于当前的整理序列,从aaa通过aaa
有效。在
基于EBCDIC的环境(大量的COBOL商店使用EBCDIC)捕获
诸如a} \之类的值。闭合手柄和后斜切字符是非阿尔法,但
落入中间
范围“ a”通过“ z”(#*@!这全是什么?)。还请注意这样的值
因为“ AAA”将无法满足item-type-a条件,因为较低的案例字母落在外面
定义的范围。也许是时候查看EBCDIC角色表了。

最后,您可以计算字符或字符串的出现在
具有Inspect动词的变量如下:

INSPECT ITEM-CODE TALLING DASH-COUNT FOR ALL '-'

dash-count需要是数字项目,并且将包含item> item-item-code 。 检查
如果要计算数字数量,动词并不是那么有用。为此,您需要每个数字的语句。

只需编码一个类似的循环可能会更容易:

PERFORM VARYING I FROM 1 BY 1
           UNTIL I > LENGTH OF ITEM-CODE
   EVALUATE ITEM-CODE(I:1)
      WHEN '-'
         COMPUTE DASH-COUNT = DASH-COUNT + 1
      WHEN '0' THRU '9'
         COMPUTE DIGIT-COUNT = DIGIT-COUNT + 1
      WHEN OTHER
         COMPUTE OTHER-COUNT = OTHER-COUNT + 1
   END-EVALUATE
END-PERFORM

现在问问自己为什么我可以使用零到9范围的检查感到舒服?提示:查看整理序列。

希望这有帮助。

Here are a few hints...

Use a hieratical record structure to view the data in different ways. For example:

 01 ITEM-REC.
    05 ITEM-CODE.
       10 ITEM-NUM-CODE    PIC 9(3).
       10 ITEM-CHAR-CODE   PIC A(3).
          88 ITEM-TYPE-A   VALUE 'AAA' THRU 'AZZ'.
          88 ITEM-TYPE-B   VALUE 'BAA' THRU 'BZZ'.
    05 QUANTITY            PIC 9(4).

ITEM-CODE is a 6 character group field, the first part of which is numeric (ITEM-NUM-CODE) and the last part
is alphabetic (ITEM-CHAR-CODE). You can refer to any one of these three variables in your program. When you
refer to ITEM-CODE, or any other group item, COBOL
treats the variable as if it were declared as PIC X. This means you can
MOVE just about anything into it without raising an error. For example:

MOVE 'ABCdef' TO ITEM-CODE

or

MOVE 'ABCdef0005' TO ITEM-REC

Neither one would cause an error even though the elementary data item ITEM-NUM-CODE is definitely not a number.
To verify the validity
of your data after a group move you should validate each elementary data item separately (unless
you know for certain no data type errors could have occurred). There are a variety of ways to do this. For
example if the data item has to be numeric the following would work:

IF ITEM-NUM-CODE IS NUMERIC
   CONTINUE
ELSE
   DISPLAY 'ITEM-NUM-CODE IS NOT NUMERIC'
   PERFORM BIG-BAD-ERROR
END-IF

COBOL provides various class tests which can be applied against a data item. For
example: NUMERIC, ALPHABETIC and ALPHANUMERIC are commonly used.

Another common way to test for ranges of values is by defining various 88 levels - but exercise
caution. In the above
example ITEM-TYPE-A is an 88 level that defines a data range from 'AAA' through 'AZZ' based on
the collating sequence currently in effect. To verify that ITEM-CHAR-CODE contains only alphabetic
characters and the first letter is an 'A' or a 'B', you could do something like:

IF ITEM-CHAR-CODE ALPHABETIC
   DISPLAY 'ITEM-CHAR-CODE is alphabetic.'
   EVALUATE TRUE
      WHEN ITEM-TYPE-A
         DISPLAY 'ITEM-CHAR-CODE is in range AAA through AZZ'
      WHEN ITEM-TYPE-B
         DISPLAY 'ITEM-CHAR-CODE is in range BAA through BZZ'
      WHEN OTHER
         DISPLAY 'ITEM-CHAR-CODE is in some other range'
   END-EVALUATE
ELSE
   DISPLAY 'ITEM-CHAR-CODE is not alphabetic'
END-IF        

Note the separate test for ALPHABETIC above. Why do that when the 88 level tests
could have done the job? Actually the 88's are not sufficient because they
cover the entire range from AAA through AZZ based on the collating sequence currently
in effect. In
an EBCDIC based environment (a very large number of COBOL shops use EBCDIC) this captures
values such as A}\. the close-brace and backslash characters are non-alpha but
fall into the middle of
the range 'A' through 'Z' (what the #*@! is that all about?). Also note that a value such
as 'aaa' would not satisfy the ITEM-TYPE-A condition because lower case letters fall outside
the defined range. Maybe time to check out an EBCDIC character table.

Finally, you can count the number of occurrences of a character, or string of characters, in
a variable with the INSPECT verb as follows:

INSPECT ITEM-CODE TALLING DASH-COUNT FOR ALL '-'

DASH-COUNT needs to be a numeric item and will contain the number of dash characters in ITEM-CODE. The INSPECT
verb is not so useful if you want to count the number of digits. For this you would need one statement for each digit.

It might be easier to just code a loop something like:

PERFORM VARYING I FROM 1 BY 1
           UNTIL I > LENGTH OF ITEM-CODE
   EVALUATE ITEM-CODE(I:1)
      WHEN '-'
         COMPUTE DASH-COUNT = DASH-COUNT + 1
      WHEN '0' THRU '9'
         COMPUTE DIGIT-COUNT = DIGIT-COUNT + 1
      WHEN OTHER
         COMPUTE OTHER-COUNT = OTHER-COUNT + 1
   END-EVALUATE
END-PERFORM

Now ask yourself why I was comfortable using a zero through 9 range check? Hint: look at the collating sequence.

Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文