Program P9a(Output, Infile, Outfile); { Combine P6 and p8 to make p9a } Const MAXSCORE = 200; PAGESIZE = 60; NAMESIZE = 20; LISTSIZE = 100; Type NameType = Array[1..NAMESIZE] Of Char; NameListType = Array [1..LISTSIZE] of NameType; TestListType = Array [1..LISTSIZE] of Integer; Var Test1, Test2, Final:Integer; Name:NameType; StuCt:Integer; NameList:NameListType; Test1List, Test2List, FinalList:TestListType; Outfile, Infile:Text; I:Integer; Procedure ReadName(Var Name:NameType); Var I:Integer; Ch:Char; Begin {ReadName} I := 1; While Not Eoln(Infile) Do Begin Read(Infile, Ch); If I <= NAMESIZE Then Begin Name[I] := Ch; I := I + 1; End; End; Readln(Infile); For I := I To NAMESIZE Do Name[I] := ' '; End; {ReadName} Function findwhere(Var NameList:NameListType; Name:NameType; StuCt:Integer):Integer; Var i:Integer; Function GE(Name1, Name2:NameType):Boolean; Var I:Integer; Begin {GE} I := 1; While (I < NAMESIZE) And (Name1[I] = Name2[I]) Do I := I + 1; GE := Name1[I] >= Name2[I]; End; {GE} Begin {findwhere} i := 1; while (i <= StuCt) AND GE(Name, Namelist[i]) Do i := i + 1; findwhere := i; End; {findwhere} procedure makeroom(Var NameList:NameListType; Var Test1List, Test2List, FinalList:TestListType; StuCt, i:Integer); Var j:Integer; Begin {makeroom} j := Stuct; while j >= i Do Begin Namelist[j+1] := Namelist[j]; Test1list[j+1] := Test1list[j]; Test2list[j+1] := Test2list[j]; Finallist[j+1] := Finallist[j]; j := j - 1; End; End; {makeroom} Procedure insertNameInList(Var NameList:NameListType; Var Test1List, Test2List, FinalList:TestListType; Name:NameType; Test1, Test2, Final:integer; StuCt:Integer); Var i:integer; Begin {InsertNameInList} i := findwhere(NameList, Name, StuCt); makeroom(NameList, Test1List, Test2List, FinalList, StuCt, i); Namelist[i] := Name; Test1List[i] := Test1; Test2List[i] := Test2; FinalList[i] := Final; End; {InsertNameInList} Procedure PrintList(Var NameList:NameListType; Var Test1List, Test2List, FinalList:TestListType; StuCt:Integer); VAR Percent:Real; LineCt:Integer; FirstTime:Boolean; TotPercent, MinPercent, MaxPercent:Real; PROCEDURE Initialize(VAR LineCt: Integer; VAR FirstTime : Boolean; VAR TotPercent, MinPercent, MaxPercent:Real); BEGIN {Initialize} LineCt := PAGESIZE + 1; FirstTime := True; TotPercent := 0.0; MinPercent := 99999.0; MaxPercent := -9E20; END; {Initialize} Procedure ReadName(Var Name:NameType); Var I:Integer; Ch:Char; Begin {ReadName} I := 1; While Not Eoln(Infile) Do Begin Read(Infile, Ch); If I <= NAMESIZE Then Begin Name[I] := Ch; I := I + 1; End; End; Readln(Infile); For I := I To NAMESIZE Do Name[I] := ' '; End; {ReadName} PROCEDURE ProcessStu( Test1, Test2, Final:Integer; StuCt:Integer; VAR Percent, TotPercent, MinPercent, Maxpercent:Real); BEGIN {ProcessStu} Percent := (Test1 + Test2 + Final) * 100 / MAXSCORE; TotPercent := TotPercent + Percent; IF Percent > MaxPercent THEN MaxPercent := Percent; IF Percent < MinPercent THEN MinPercent := Percent; END; {ProcessStu} Procedure PrintName(Name:NameType); Var I:Integer; Begin {PrintName} For I := 1 to NAMESIZE Do Write(Outfile, Name[I]); End; {PrintName} PROCEDURE PrintSummary(VAR F:Text; StuCt:Integer; TotPercent, MinPercent, MaxPercent:Real); VAR AvgPercent:Real; BEGIN {PrintSummary} IF StuCt > 0 THEN AvgPercent := TotPercent / StuCt ELSE AvgPercent := 0; Writeln(F); Writeln(F,'Number of Students: ', StuCt:1); Writeln(F,'Average Percent: ', AvgPercent:1:1); Writeln(F,'Minimum Percent: ', MinPercent:1:1); Writeln(F,'Maximum Percent: ', MaxPercent:1:1); END; {PrintSummary} BEGIN {PrintList} Assign(Outfile, 'c:\report.txt'); Rewrite(outfile); Initialize(LineCt, FirstTime, TotPercent, MinPercent, MaxPercent); For I := 1 To StuCt Do Begin IF LineCt >= PAGESIZE Then Begin IF FirstTime Then FirstTime := False Else Write(Outfile, chr(12)); Writeln(Outfile, ' ':NAMESIZE, 'Test Test'); Writeln(Outfile, ' Name':1, '1':NAMESIZE-2,' 2 Final Percent'); Writeln(Outfile); LineCt := 3; End; ProcessStu(Test1List[I], Test2List[I], FinalList[I], StuCt, Percent, TotPercent, MinPercent, MaxPercent); PrintName(NameList[I]); Write(Outfile, Test1List[I]:3, Test2List[I]:10, FinalList[I]:10, Percent:10:1); IF Percent >= 60 THEN Writeln(Outfile, 'Pass':10) ELSE Writeln(Outfile, 'Fail':10); LineCt := LineCt + 1; End; PrintSummary(Output, StuCt, TotPercent, MinPercent, MaxPercent); PrintSummary(OutFile, StuCt, TotPercent, MinPercent, MaxPercent); Close(Outfile); Readln; END; {PrintList} Begin {P9a} Assign(Infile, 'c:\scores.txt'); Reset(Infile); StuCt := 0; while not eof(Infile) Do Begin Read(Infile, Test1, Test2, Final); ReadName(Name); insertNameInList(NameList, Test1List, Test2List, FinalList, Name, Test1, Test2, Final, StuCt); StuCt := StuCt + 1; End; printlist(NameList, Test1List, Test2List, FinalList, StuCt); End. {P9a}