需要有关伪代码分配的帮助!关于阅读记录并添加它们并打印它们

时间:2011-09-02 13:38:54

标签: pseudocode

学生档案文件包含每位学生的姓名,性别(M或F),年龄(年份)和婚姻状况(单身或已婚)。设计一种算法,读取文件并计算已婚男性,单身男性和已婚女性的数量。在学生摘要报告中打印这些数字。如果任何单身男性超过30岁。在单独的符合条件的单身汉报告上打印他们的姓名和年龄。

任何人都可以告诉我,我的错在任何一行吗?谢谢!希望你能帮助我!

Set marriedMen to 0
Set singleMen to 0
Set marriedWomen to 0
Set singleWomen to 0 
READ name, sex, age, status
DOWHILE(NOT EOF)
IF (status = married) THEN  //check if status is married, if yes then check next
IF (sex = ‘F’) THEN     //check if sex is F, if yes then +1
marriedWomen = marriedWomen + 1
ELSE
IF (sex = ‘M’) THEN     //under married, and sex is M then +1
marriedMen = marriedMen + 1
ENDIF
ENDIF
ENDIF
IF (status = single) THEN   //check if status is single, if yes then check next
IF (sex = ‘F’) THEN     //check if sex is F, if yes then +1 to singleWomen
singleWomen = singleWomen + 1
ELSE
IF (sex = ‘M’) THEN     //under single, and sex is M then +1
singleMen = singleMen + 1
IF (age > 30) THEN      //under single, sex = M and age is over 30 then print the name, age
Print ‘Eligible bachelors Report’ 
Print ‘Name: ‘, name
Print ‘Age: ‘, age
ENDIF
ENDIF
ENDIF 
ENDIF
READ next record
ENDDO
Print ‘Student Summary Report’
Print ‘Married Men: ‘, marriedMen
Print ‘Single Men: ‘, singleMen
Print ‘Married Women: ‘, marriedWomen
Print ‘Single Women: ‘, singleWomen

2 个答案:

答案 0 :(得分:1)

在下面的代码中,我完成了以下操作:

  1. 添加了boolean,以防止您为每一行打印Eligible bachelors Report标题。
  2. 缩进代码以便于阅读。
  3. 将您的使用替换为',因为您显然使用了诸如MS-Word之类的文字处理程序来编写代码。 (我可以建议NotePad++吗?)
  4. 我完全保留了您的代码:我认为ElseIf不是您伪代码中的有效关键字,而您的教授可能会传递一个包含M以外的性别的文件F

    Set marriedMen to 0
    Set singleMen to 0
    Set marriedWomen to 0
    Set singleWomen to 0 
    Set hasPrintedHeader to False
    
    READ name, sex, age, status
    DOWHILE(NOT EOF)
        IF (status = married) THEN  //check if status is married, if yes then check next
            IF (sex = 'F') THEN     //check if sex is F, if yes then +1
                marriedWomen = marriedWomen + 1
            ELSE
                IF (sex = 'M') THEN     //under married, and sex is M then +1
                    marriedMen = marriedMen + 1
                ENDIF
            ENDIF
        ENDIF
    
        IF (status = single) THEN   //check if status is single, if yes then check next
            IF (sex = 'F') THEN     //check if sex is F, if yes then +1 to singleWomen
                singleWomen = singleWomen + 1
            ELSE
                IF (sex = 'M') THEN     //under single, and sex is M then +1
                    singleMen = singleMen + 1
                    IF (age > 30) THEN      //under single, sex = M and age is over 30 then print the name, age
                        IF (hasPrintedHeader = False) THEN
                            Print 'Eligible bachelors Report'
                            hasPrintedHeader = True
                        END IF
                        Print 'Name: ', name
                        Print 'Age: ', age
                    ENDIF
                ENDIF
            ENDIF 
        ENDIF
        READ next record
    ENDDO
    
    Print 'Student Summary Report'
    Print 'Married Men: ', marriedMen
    Print 'Single Men: ', singleMen
    Print 'Married Women: ', marriedWomen
    Print 'Single Women: ', singleWomen
    

答案 1 :(得分:-1)

DrawTree(n,方向,长度)

if n > 0 do

    DrawTrunk(direction, length)

    DrawTree(n-1, 3DRandomAngle(direction), length*Factor(n))

    DrawTree(n-1, direction + random % 10, length*Factor(n))

    DrawTree(n-1, 3DRandomAngle(direction), length*Factor(n))

else

    DrawLeaf()

end if

结束DrawTree