Napisano: 21 Jan 2016 16:33
Nisam stigao pre, ali bolje ikad nego nikad
Evo prvog. Napravio sam ga u 2 datoteke:
square.pas
program square;
{$mode objfpc}{$H+}
uses parser;
var
Line: String;
begin
with TParser.Create do
try
repeat
ReadLn(Line);
until not Parse(Line);
WriteLn(ToString);
finally
Free;
end;
end.
parser.pas
unit parser;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FGL;
type
{ TSquare }
TSquare = class
private
FDiagonal: Extended;
FSide: Extended;
FCircumference: Extended;
procedure Calculate;
public
constructor Create(Diagonal: Extended);
property Diagonal: Extended read FDiagonal;
property Side: Extended read FSide;
property Circumference: Extended read FCircumference;
end;
TSquareList = specialize TFPGObjectList<TSquare>;
{ TParser }
TParser = class
private
FSquares: TSquareList;
public
constructor Create;
destructor Destroy; override;
function Parse(Number: String): Boolean;
function ToString: String; override;
end;
implementation
const
Sqrt2: Extended = sqrt(2);
{ TSquare }
procedure TSquare.Calculate;
begin
FSide := Sqrt2 * FDiagonal / 2;
FCircumference := FSide * 4;
end;
constructor TSquare.Create(Diagonal: Extended);
begin
FDiagonal := Diagonal;
Calculate;
end;
{ TParser }
constructor TParser.Create;
begin
FSquares := TSquareList.Create;
end;
destructor TParser.Destroy;
begin
FSquares.Free;
inherited Destroy;
end;
function TParser.Parse(Number: String): Boolean;
var
Num: Integer;
begin
Result := TryStrToInt(Number, Num) and (Num > 0) and (Num < 50);
if Result then
FSquares.Add(TSquare.Create(Num));
end;
function TParser.ToString: String;
var
Square: TSquare;
begin
Result := Format('%20s %20s %20s' + LineEnding, ['Diagonal', 'Side', 'Circumference']);
for Square in FSquares do
Result := Result + Format('%20.2f %20.2f %20.2f' + LineEnding , [Square.Diagonal, Square.Side, Square.Circumference]);
end;
end.
Možda nešto neće raditi pošto sam pisao iz glave, a Pascal odavno nisam koristio.
Dopuna: 22 Jan 2016 14:34
Evo, druže, danas sam ugrabio malo vremena da ti uradim i drugi zadatak. Da bezveze ne zatrpavam forum kodom, šaljem ti zip pa pogledaj: https://www.mycity.rs/must-login.png
Srećno
|