Ferramentas de Usuário

Ferramentas de Site


dev_geral:pascal:snippet:students

Students

Objectivo: ler 20 notas de alunos, registar num ficheiro, e depois calcular as positivas, negativas e média.

{$mode objfpc}
program students;
uses sysutils, strutils;
 
const DIM = 20;
      FNAME = 'teste';
      TAB = '    ';
 
type TArrayInt = array of smallint;
     TFileInt = file of smallint;
 
var grades, negative_grades : TArrayInt;
    n : smallint;
    GradeFile : TFileInt;
 
procedure FillArray(var arr : TArrayInt; dimension : longint; const prompt : string);
begin
    setlength(arr, dimension-1);
    for dimension := low(arr) to high(arr) do begin
        write(AnsiReplaceText(prompt, '@{COUNT}', IntToStr(dimension)));
        readln(arr[dimension]);
    end;
end;
 
procedure CopyArrayToFile(const arr : TArrayInt; out f : TFileInt);
var elem : smallint;
begin
    for elem in arr do
        write(f, elem);
end;
 
procedure GetArrayFromFile(var f : TFileInt; out arr : TArrayInt);
var i : smallint = 0;
begin
    seek(f,0);
    SetLength(arr, FileSize(f));
    while not eof(f) do begin
        read(f, arr[i]);
        inc(i);
    end;
end;
 
function CountPositive(arr : TArrayInt; const minimum : smallint = 0) : smallint;
var elem : smallint;
begin
    CountPositive := 0;
    for elem in arr do
        if elem >= minimum then
            inc(CountPositive);
end;
 
function CountPositive(var f : TFileInt; const minimum : smallint = 0) : smallint; overload;
var arr : TArrayInt;
begin
    GetArrayFromFile(f, arr);
    CountPositive := CountPositive(arr, minimum);
end;
 
procedure GetNegative(arr : TArrayInt; out neg : TArrayInt; const minimum : smallint = 0);
var elem : smallint;
begin
    for elem in arr do
        if elem < minimum then begin
            SetLength(neg, length(neg)+1);
            neg[length(neg)-1] := elem;
        end;
end;
 
procedure GetNegative(var f : TFileInt; out neg : TArrayInt; const minimum : smallint = 0); overload;
var arr : TArrayInt;
begin
    GetArrayFromFile(f, arr);
    GetNegative(arr, neg, minimum);
end;
 
function Average(arr : TArrayInt) : real;
var elem : smallint;
begin
    Average := 0.0;
    for elem in arr do
        Average := Average + elem;
    Average := Average / length(arr);
end;
 
function Average(var f : TFileInt) : real; overload;
var arr : TArrayInt;
begin
    GetArrayFromFile(f, arr);
    Average := Average(arr);
end;
 
begin
    assign(GradeFile, FNAME);
    FillArray(grades, 5, 'Write the @{COUNT} grade: ');
    rewrite(GradeFile);
    CopyArrayToFile(grades, GradeFile);
    close(GradeFile);
    reset(GradeFile);
    writeln('There is/are ', CountPositive(GradeFile), ' positive grades.');
    GetNegative(GradeFile, negative_grades, 10);
    writeln('Negative grades:');
    for n in negative_grades do
        writeln(TAB, n);
    writeln('Average = ', Average(GradeFile));
    close(GradeFile);
    readln;
end.

Tópico no fórum

dev_geral/pascal/snippet/students.txt · Última modificação em: 2018/05/14 21:37 (edição externa)