How to read two records and compare them with a primary key in sequential file system?

家住魔仙堡 提交于 2019-12-25 04:48:34

问题


This is the record format for my input file:

ID NAME Purchaseamount month
1  xxx  10000           feb
1  xxx  10000            mar
1  xxx  10000           apr
2  yyy  100              jan
2  yyy  2054             mar

How can I add the purchase amount for every person? I will give you the pseudo-code of my work.

Read inputfile,
move to working storage variables,
end read,
perform until eof,
read input file,
if inputid = ws-input id,
add purchase amount,
else write to output file

回答1:


Bruce provides a good solution, but there is often more than one way to do things. Here is an alternate solution (in pseudocode) that is inspired by Michael A. Jackson's "Principles of Program Design" (I recommend it if you have not read it).

OPEN INPUT INPUT-FILE
READ INPUT-FILE 
PERFORM PROCESS-PERSON UNTIL INPUT-FILE-EOF
CLOSE INPUT-FILE
STOP RUN
.

PROCESS-PERSON.
    MOVE INPUT-ID TO LAST-INPUT-ID
    MOVE ZERO TO PURCHASE-TOTAL
    PERFORM PROCESS-RECORD UNTIL INPUT-FILE-EOF OR
        (INPUT-ID NOT = LAST-INPUT-ID)
    WRITE PERSON-TOTAL
    .

PROCESS-RECORD.
    ADD PURCHASE-AMOUNT TO PURCHASE-TOTAL
    READ INPUT-FILE
    .



回答2:


for pseudo code Try:

 move 0            to purchaseTotal
 Read InputFile
 move inputId      to lastInputId
 Perform until eof
     if inputId = lastInputId
        add purshaseAmount     to purchaseTotal
     else
        write personTotal
        move 0                 to purchaseTotal 
        move inputId      to lastInputId
     end-if  
     Read InputFile
end-Perform
write personTotal

do you have any code and the result ???




回答3:


Sometimes, the best program is one you don't have to write.

This really is a problem that begs for a utility tool solution. Syncsort will do match and sums type things, so will other tools like FileAid and FileManager. You could probably knock that out with two or three lines of config information.



来源:https://stackoverflow.com/questions/28711507/how-to-read-two-records-and-compare-them-with-a-primary-key-in-sequential-file-s

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!