Add book "Занимательное программирование"

This commit is contained in:
2025-12-13 16:35:46 +01:00
parent 98329e0a3d
commit c1147629f7
78 changed files with 4530 additions and 0 deletions

View File

@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectOptions>
<Version Value="12"/>
<General>
<Flags>
<MainUnitHasCreateFormStatements Value="False"/>
<MainUnitHasScaledStatement Value="False"/>
</Flags>
<SessionStorage Value="InProjectDir"/>
<Title Value="State automat"/>
<UseAppBundle Value="False"/>
<ResourceType Value="res"/>
</General>
<BuildModes>
<Item Name="Default" Default="True"/>
</BuildModes>
<PublishOptions>
<Version Value="2"/>
<UseFileFilters Value="True"/>
</PublishOptions>
<RunParams>
<FormatVersion Value="2"/>
</RunParams>
<Units>
<Unit>
<Filename Value="Project1.pas"/>
<IsPartOfProject Value="True"/>
</Unit>
</Units>
</ProjectOptions>
<CompilerOptions>
<Version Value="11"/>
<Target>
<Filename Value="Project1"/>
</Target>
<SearchPaths>
<IncludeFiles Value="$(ProjOutDir)"/>
<UnitOutputDirectory Value="lib/$(TargetCPU)-$(TargetOS)"/>
</SearchPaths>
<Linking>
<Debugging>
<DebugInfoType Value="dsDwarf3"/>
</Debugging>
</Linking>
</CompilerOptions>
<Debugging>
<Exceptions>
<Item>
<Name Value="EAbort"/>
</Item>
<Item>
<Name Value="ECodetoolError"/>
</Item>
<Item>
<Name Value="EFOpenError"/>
</Item>
</Exceptions>
</Debugging>
</CONFIG>

View File

@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<CONFIG>
<ProjectSession>
<Version Value="12"/>
<BuildModes Active="Default"/>
<Units>
<Unit>
<Filename Value="Project1.pas"/>
<IsPartOfProject Value="True"/>
<IsVisibleTab Value="True"/>
<TopLine Value="81"/>
<CursorPos X="27" Y="105"/>
<UsageCount Value="21"/>
<Loaded Value="True"/>
</Unit>
</Units>
<JumpHistory HistoryIndex="16">
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="63" Column="31" TopLine="25"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="51" Column="31" TopLine="27"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="76" Column="47" TopLine="45"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="22" Column="37"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="14" Column="5"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="77" Column="16" TopLine="39"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="107" Column="27" TopLine="75"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="86" Column="14" TopLine="75"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="53" Column="5" TopLine="14"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="97" Column="9" TopLine="57"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="103" Column="57" TopLine="67"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="112" Column="59" TopLine="78"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="118" Column="58" TopLine="87"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="122" Column="9" TopLine="98"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="116" Column="62" TopLine="97"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="4" Column="16"/>
</Position>
<Position>
<Filename Value="Project1.pas"/>
<Caret Line="12" Column="12" TopLine="12"/>
</Position>
</JumpHistory>
<RunParams>
<FormatVersion Value="2"/>
<Modes ActiveMode=""/>
</RunParams>
</ProjectSession>
</CONFIG>

View File

@@ -0,0 +1,228 @@
program Project1;
{$mode objfpc}{$H+}
{$codepage utf8}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
Classes, SysUtils, CustApp
{ you can add units after this };
type StateType = (pay, coffee, cup, back);
type CommandType = (
pay5, pay10, pay20, pay50, pay100, pay200,
chooseblack, choosesugar, coosecappu, choosecacao,
choosecup, choosenocup, payback
);
type
{ TStateMachine }
TStateMachine = class(TCustomApplication)
protected
procedure DoRun; override;
public
constructor Create(TheOwner: TComponent); override;
destructor Destroy; override;
function GetNextCommand: CommandType;
end;
{ TStateMachine }
procedure TStateMachine.DoRun;
var
ErrorMsg: String;
State: StateType = pay;
Command: CommandType;
moneyPayed: Integer = 0;
begin
// quick check parameters
ErrorMsg := CheckOptions('h', 'help');
if ErrorMsg <> '' then begin
ShowException(Exception.Create(ErrorMsg));
Terminate;
Exit;
end;
while true do
begin
Command := GetNextCommand;
case State of
pay:
begin
case Command of
pay5:
begin
moneyPayed := moneyPayed + 5;
end;
pay10:
begin
moneyPayed := moneyPayed + 10;
end;
pay20:
begin
moneyPayed := moneyPayed + 20;
end;
pay50:
begin
moneyPayed := moneyPayed + 50;
end;
pay100:
begin
moneyPayed := moneyPayed + 100;
end;
pay200:
begin
moneyPayed := moneyPayed + 200;
end;
chooseblack, choosesugar, coosecappu, choosecacao:
begin
if moneyPayed >= 70 then
begin
State := coffee;
end
else
begin
WriteLn('Вы бросили недостаточно денег.');
end;
end
else
WriteLn('Неверная команда!');
end;
end;
coffee:
begin
case Command of
choosecup, choosenocup:
begin
State := cup;
end;
else
WriteLn('Неверная команда!');
end;
end;
cup:
begin
case Command of
payback:
begin
State := back;
WriteLn('Ваша сдача: ' + IntToStr(moneyPayed - 70));
Break;
end;
else
WriteLn('Неверная команда!');
end;
end;
back:
begin
end;
end;
end;
// stop program loop
Terminate;
end;
constructor TStateMachine.Create(TheOwner: TComponent);
begin
inherited Create(TheOwner);
StopOnException:=True;
end;
destructor TStateMachine.Destroy;
begin
inherited Destroy;
end;
function TStateMachine.GetNextCommand: CommandType;
var
Input: String;
begin
Writeln('Введите команду:');
while true do
begin
Readln(Input);
case Input of
'pay5':
begin
Result := pay5;
Exit;
end;
'pay10':
begin
Result := pay10;
Exit;
end;
'pay20':
begin
Result := pay20;
Exit;
end;
'pay50':
begin
Result := pay50;
Exit;
end;
'pay100':
begin
Result := pay100;
Exit;
end;
'pay200':
begin
Result := pay200;
Exit;
end;
'chooseblack':
begin
Result := chooseblack;
Exit;
end;
'choosesugar':
begin
Result := choosesugar;
Exit;
end;
'choosecappu':
begin
Result := coosecappu;
Exit;
end;
'choosecacao':
begin
Result := choosecacao;
Exit;
end;
'choosecup':
begin
Result := choosecup;
Exit;
end;
'choosenocup':
begin
Result := choosenocup;
Exit;
end;
'payback':
begin
Result := payback;
Exit;
end
else Writeln('Команда не существует. Попробуйте еще раз:');
end;
end;
end;
var
Application: TStateMachine;
begin
Application:=TStateMachine.Create(nil);
Application.Title:='State automat';
Application.Run;
Application.Free;
end.