Program P9b(Output, Infile, Outfile); { Mel Simmons Program 9b Insertion Sort } Const MAXSCORE = 200; PAGESIZE = 60; LISTSIZE = 100; NAMESIZE = 20; Type NameType = Array[1..NAMESIZE] Of Char; StudentType = Record Name:NameType; Test1, Test2, Final:Integer; End; ListType = Array [1..LISTSIZE] of StudentType; VAR List:ListType; StuCt:Integer; Student:StudentType; Outfile, Infile:Text; 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 List:ListType; 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, list[i].Name) Do i := i + 1; findwhere := i; End; {findwhere} procedure makeroom(Var List:ListType; StuCt, i:Integer); Var j:Integer; Begin {makeroom} j := StuCt; while j >= i Do Begin list[j+1] := list[j]; j := j - 1; End; End; {makeroom} Procedure insertStudentInList(Var List:ListType; Student:StudentType; StuCt:Integer); Var i:integer; Begin {InsertNameInList} i := findwhere(List, Student.Name, StuCt); makeroom(List, StuCt, i); list[i] := Student; End; {InsertNameInList} Procedure PrintList(Var List:ListType; StuCt:Integer); Var Percent:Real; LineCt:Integer; FirstTime:Boolean; TotPercent, MinPercent, MaxPercent:Real; I:Integer; 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 PrintName(Name:NameType); Var I:Integer; Begin {PrintName} For I := 1 to NAMESIZE Do Write(Outfile, Name[I]); End; {PrintName} PROCEDURE ProcessStu( Test1, Test2, Final: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 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(List[I].Test1, List[I].Test2, List[I].Final, Percent, TotPercent, MinPercent, MaxPercent); PrintName(List[I].Name); Write(Outfile, List[I].Test1:3, List[I].Test2:10, List[I].Final: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 {P7} Assign(Infile, 'c:\scores.txt'); Reset(Infile); StuCt := 0; while not eof(Infile) Do Begin Read(Infile, Student.Test1, Student.Test2, Student.Final); ReadName(Student.Name); insertStudentInList(List, Student, StuCt); StuCt := StuCt + 1; End; printlist(List, StuCt); Close(Outfile); End. {P9b}