来自文件的COBOL COMPUTE十进制值

时间:2016-09-06 00:17:39

标签: cobol gnucobol

试图理解计算。使用FSemesterTotal(这是PIC 99V99)来计算所获得的积分总和是否正确? COMPUTE FSemesterTotal =获得+获得。我认为在我的循环中应该有一个计数器来检查我是否读取了第一个赢得的值,所以我可以将它添加到第二个值,而不确定如何在COBOL中完成此操作。

目前我的输入是这样的,

CMPS161     ALGORITHM DSGN/IMPLMNT I        A    3.00
ENGL322     INTRO TO PROF/TECH WRITING      A    3.00
MATH241     ELEM STATISTICS                 B    3.00
ART 106     SURV WORLD ART HIST II          A    3.00
BIOL152     GENERAL BIOL LAB I              B    1.00
CMPS257     DISCRETE STRUCTURE              A    3.00
CMPS28O     ALGORITHM DSGN/IMPLEM II        B    3.00
CMPS290     COMPUTER ORGANIZATION           A    3.00
CMPS390     DATA STRUCTURES                 B    3.00
GBIO153     GENERAL BIOL II                 B    3.00
CMPS294     INTERNET PROGRAMMING            B    3.00
CMPS315     SYSTEM ADMINISTRATION           A    3.00
CMPS329     COMPUTER NETWORKING SECURITY    A    3.00
CMPS383     INFORMATION SYSTEMS             A    3.00
CMPS415     INTERGRATED TECH SYSTEMS        B    3.00

COBOL CODE

IDENTIFICATION DIVISION.
PROGRAM-ID.      P2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
    SELECT myInFile ASSIGN TO "P2In.dat".
    SELECT myOutFile ASSIGN TO "P2Out.dat".
DATA DIVISION.
FILE SECTION.
FD myInFile.
01 inRecord.
   02 Course   PIC X(13).
   02 Title    PIC X(32).
   02 Grade    PIC X(4).
   02 Earned   PIC 9V99.
   02 FILLER   PIC X(3).
FD myOutFile.
01 outRecord.
   02 myCourse  PIC X(13).
   02 myTitle   PIC X(32).
   02 myGrade   PIC X(4).
   02 myEarned  PIC 9.99.
   02 FILLER    PIC X(3).
WORKING-STORAGE SECTION.
01 E0F PIC X(3) VALUE "NO ".
01 NAME-HDR.
   05 FILLER    PIC X(10) VALUE "NAME HERE ".
01 SCHOOLID-HDR.
   05 FILLER    PIC X(8) VALUE "SCHOOLID"
01 COLUMN-HDR.
   05 CCourse  PIC X(6)  VALUE "COURSE".
   05 CSpace   PIC X(7)  VALUE  SPACES.
   05 HTitle   PIC X(5)  VALUE "TITLE".
   05 HSpace   PIC X(27) VALUE  SPACES.
   05 CGrade   PIC XX    VALUE "GR".
   05 CSpace   PIC XXX   VALUE SPACES.
   05 CEarned  PIC X(6)  VALUE "EARNED".
   05 QSpace   PIC X(4)  VALUE SPACES.
   05 Qpts     PIC X(4)  VALUE "Qpts".
01 FOOTER-SMS.
   05 FSemester      PIC X(28) VALUE "                    SEMESTER".
   05 FSpaces        PIC x(21) VALUE SPACES.
   05 FSemesterTotal PIC 99V99.
01 FOOTER-CUMUL.
   05 FCumulative    PIC X(30) VALUE"                    CUMULATIVE".
   05 FSpaces        PIC X(19) VALUE SPACES.
   05 FCumulTotal    PIC 99V99.
01 QPTS-VAL.
   05 QSpace    PIC X(5) VALUE SPACES.
   05 QPtsValue PIC 99V99.
01 GPA.
   05 GSpace    PIC XX VALUE SPACES.
   05 GpaScore  PIC 9.99.
PROCEDURE DIVISION.
MAIN-PROGRAM.
PERFORM HEADER.
PERFORM FILE-IO.
PERFORM CLOSING.
STOP RUN.
HEADER.
OPEN INPUT myInFile
     OUTPUT myOutFile.
WRITE outRecord FROM NAME-HDR.
WRITE outRecord FROM SCHOOLID-HDR
       AFTER ADVANCING 1 LINE.
WRITE outRecord FROM COLUMN-HDR
       AFTER ADVANCING 2 LINES.
MOVE SPACES TO outRecord.
WRITE outRecord
      AFTER ADVANCING 1 LINE.
FILE-IO.
   READ myInFile
      AT END
           MOVE "YES" TO EOF.
   DISPLAY NAME-HDR.
   DISPLAY SCHOOLID-HDR.
   DISPLAY SPACES.
   DISPLAY SPACES.
   DISPLAY "FALL 2014"
   DISPLAY COLUMN-HDR.
   PERFORM PROCESS-RECORD
   UNTIL EOF = "YES".
PROCESS-RECORD.
     MOVE Course to myCourse.
     MOVE Title to myTitle.
     MOVE Grade to myGrade.
     MOVE Earned to myEarned.
     WRITE outRecord
        AFTER ADVANCING 1 LINE.
     READ myInFile
        AT END
           MOVE "YES" TO EOF.
        NOT AT END
           IF myCourse = "ART 106      " THEN
              DISPLAY FOOTER-SMS, QPTS-VAL, GPA
              DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
              DISPLAY SPACES.
              DISPLAY "SPRING 2015"
           END-IF.
           IF myCourse = "CMPS285      " THEN
              DISPLAY FOOTER-SMS, QPTS-VAL, GPA
              DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
              DISPLAY SPACES.
              DISPLAY "FALL 2015"
           END-IF.
           IF myCourse = "CMPS294      " THEN
              DISPLAY FOOTER-SMS, QPTS-VAL, GPA
              DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
              DISPLAY SPACES.
              DISPLAY "SPRING 2016"
           END-IF.
 CLOSING.
     DISPLAY FOOTER-SMS, QPTS-VAL, GPA.
     DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA.
     CLOSE myInFile
           myOutFile.

1 个答案:

答案 0 :(得分:3)

问题是:"我可以这样使用COMPUTE吗?"

答案是: 是的,但您可能希望添加ON SIZE ERROR以满足可能的大小溢出,以防您的输入数据包含太多条目。

如果问题背后的问题是:"该计划是否有效?"

答案是否定的: 尽管Brian已经指出了这些问题:你需要进行去编辑才能将数据从9.99(4个字节,不能用于算术)更改为9v99(3个字节,可用于算术。

如果您不使用运行时验证的ISAM文件:始终验证文件输入(文件可能已损坏,您可能不想异常终止或产生错误的结果)。