0

试图理解计算。使用像这样的 PIC 99V99 FSemesterTotal 计算获得的学分总和是否正确?计算 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 代码

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.
4

1 回答 1

3

问题是:“我可以COMPUTE这样用吗?”

答案是:是的,但您可能想要添加一个ON SIZE ERROR以应对可能的大小溢出,以防您的输入数据有太多条目。

如果问题背后的问题是:“程序会起作用吗?”

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

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

于 2016-09-06T05:04:34.090 回答