program field; {$apptype console} uses Sysutils, Classes; function SumValue(const Value: array of double): double; var i: integer; begin result := Value[0]; for i := 1 to High(Value) do result := result + Value[i]; end; function AverageValue(const Value: array of double): double; begin result := SumValue(Value) / (High(Value)+1); end; function MaxValueIndex(const Value: array of double): integer; var i: integer; begin result := 0; for i := 1 to High(Value) do if Value[result] < Value[i] then result := i; end; function MinValueIndex(const Value: array of double): integer; var i: integer; begin result := 0; for i := 1 to High(Value) do if Value[result] > Value[i] then result := i; end; const usage = 'usage: field [-?] [-max|-min|-sum|-avg] [-f infile] [-no|-o outfile]'; type TActionMode = (amMax, amMin, amSum, amAvg); var ActionMode: TActionMode; st, InfileName, OutFileName: string; var i, n: integer; f: textfile; ary: Array of double; SL: TStringList; begin InfileName := 'conin$'; OutFileName := 'conout$'; ActionMode := amMax; i := 1; while i <= ParamCount do begin st := ParamStr(i); if st[1] = '-' then begin if AnsiCompareStr(st, '-max') = 0 then begin ActionMode := amMax; end else if AnsiCompareStr(st, '-min') = 0 then begin ActionMode := amMin; end else if AnsiCompareStr(st, '-sum') = 0 then begin ActionMode := amSum; end else if AnsiCompareStr(st, '-avg') = 0 then begin ActionMode := amAvg; end else if AnsiCompareStr(st, '-f') = 0 then begin i := i + 1; InfileName := ParamStr(i); end else if AnsiCompareStr(st, '-o') = 0 then begin i := i + 1; OutFileName := ParamStr(i); end else if AnsiCompareStr(st, '-no') = 0 then begin OutFileName := 'nul'; end else if AnsiCompareStr(st, '-?') = 0 then begin Writeln(usage); ExitCode := -1; Exit; end else begin Writeln('error: ', st); Writeln(usage); ExitCode := -2; Exit; end; end else begin InFileName := st; end; i := i + 1; end; // Read Line AssignFile(f, InfileName); Reset(f); Readln(f, st); CloseFile(f); SL := TStringList.Create; SL.CommaText := st; n := SL.Count; if n = 0 then exit; SetLength(ary, n); for i := 0 to n-1 do ary[i] := strtofloat(SL[i]); // Calc case ActionMode of amMax: begin ExitCode := MaxValueIndex(ary) + 1; st := inttostr(ExitCode); end; amMin: begin ExitCode := MinValueIndex(ary) + 1; st := inttostr(ExitCode); end; amSum: st := floattostr(SumValue(ary)); amAvg: st := floattostr(AverageValue(ary)); end; // Write Line AssignFile(f, OutfileName); Rewrite(f); Writeln(f, st); CloseFile(f); SL.Free; end.