grade_report.pas en Delphi
Puntajes de cinco estudiantes, promediados en calificaciones por letra, con un resumen del grupo.
program GradeReport;
uses SysUtils;
const
Names: array[0..4] of string =
('Okafor', 'Lindqvist', 'Ramos', 'Chen', 'Baptiste');
// One row of quiz scores per student, in the same order as Names.
Scores: array[0..4, 0..2] of Integer =
((88, 92, 79),
(71, 68, 80),
(95, 99, 91),
(58, 64, 72),
(83, 77, 90));
// A case on the tens digit is tidier than four chained comparisons.
function LetterFor(Mean: Double): string;
begin
case Trunc(Mean) div 10 of
10, 9: Result := 'A';
8: Result := 'B';
7: Result := 'C';
6: Result := 'D';
else
Result := 'F';
end;
end;
var
I, J, Total, Passing: Integer;
Mean, ClassSum, Best: Double;
TopName: string;
begin
ClassSum := 0;
Passing := 0;
Best := 0;
TopName := '';
WriteLn(Format('%-10s %4s %4s %4s %7s %s',
['student', 'q1', 'q2', 'q3', 'mean', 'grade']));
WriteLn(StringOfChar('-', 41));
for I := 0 to High(Names) do
begin
Total := 0;
for J := 0 to High(Scores[I]) do
Total := Total + Scores[I][J];
Mean := Total / Length(Scores[I]);
WriteLn(Format('%-10s %4d %4d %4d %7.1f %s',
[Names[I], Scores[I][0], Scores[I][1], Scores[I][2],
Mean, LetterFor(Mean)]));
ClassSum := ClassSum + Mean;
if Mean >= 70 then
Inc(Passing);
if Mean > Best then
begin
Best := Mean;
TopName := Names[I];
end;
end;
WriteLn(StringOfChar('-', 41));
WriteLn(Format('class mean %.1f, %d of %d passing',
[ClassSum / Length(Names), Passing, Length(Names)]));
WriteLn(Format('top of the class: %s at %.1f', [TopName, Best]));
end.
Cómo funciona
Scores: array[0..4, 0..2] of Integerguarda una fila por estudiante, en el orden deNames.LetterForcorrecase Trunc(Mean) div 10 ofsobre las decenas, conelsepara lo demás.- Los anchos de
%-10s %4d %4d %4d %7.1falinean las columnas yStringOfChar('-', 41)las raya.
Palabras clave y builtins usados aquí
DoubleFormatHighIncIntegerLengthMeanResultStringOfCharTruncWriteLnarraybegincaseconstdivdoelseendforfunctionifofprogramstringthentousesvar
El intento, en números
- Líneas
- 68
- Caracteres a escribir
- 1445
- Tokens
- 446
- Ritmo de tres estrellas
- 75 tpm
Al ritmo de tres estrellas de 75 tokens por minuto, este intento toma unos 357 segundos.
Paso 2 de 3 en Bis; paso 26 de 27 en Fundamentos del lenguaje.